如何从变量中的数组中获取所有数据?

I have a dynamic array like

Array
(
[company_name] => a
[address] => b
[country_id] => 1
[email] => c@c.com
[currency_id] => 1
)

and i want to create a index variable like:

`$var = "company_name,address,country_id,email,currency_id";`

and value variable like:

$value = "a,b,1,c@c.com,1";

remember that array index and value not fixed. thanks in advance.

You can try this.

$var = implode(',', array_keys($myArray));

$value = implode(',', array_values($myArray));

Where $myArray is the array you showed in the question.

Use array_keys() and array_values() functions.


Consider you have an array like this:

$arr = array(
    'company_name' => 'a',
    'address' => 'b',
    'country_id' => 1,
    'email' => 'c@c.com',
    'currency_id' => 1
);

Use array_keys() function to get all keys:

$keys = array_keys($arr);
print_r($keys);

// output
// Array
// (
//     [0] => company_name
//     [1] => address
//     [2] => country_id
//     [3] => email
//     [4] => currency_id
// )

Then you can use implode to convert array to string:

$k = implode($keys, ',');
echo $k;
// company_name,address,country_id,email,currency_id

And for values such that, instead we use array_values() function:

$vals = array_values($arr);
print_r($vals);

// output
// Array
// (
//     [0] => a
//     [1] => b
//     [2] => 1
//     [3] => c@c.com
//     [4] => 1
// )

And then implode it:

$v = implode($vals, ',');
echo $v;
// a,b,1,c@c.com,1

Is this what you mean?

$keys=implode(',',array_keys($a));
$vals=implode(',',array_values($a));