user1690835 user1690835 - 1 month ago 6
PHP Question

Split associative array based on keys

I have the following array

array (
[customer_name] =>{customer1,customer2}
[phone] => {123456,123456}
[email] => {test@example.com,test2@example.com}
[project] => {"Project A","Project B"}
[property] => {A1,A2}
[amount] => {100.00,2000.00}
)


I want to split the array based on keys and assign to different variables. For example

$customer_name=array(customer1,customer2)
$phone=array(123456,123456)
// and so on


Please help. Thanks in advance.

Answer

You could use foreach to do that and reassign variables inside the loop:

foreach($array as $key => $values) {
    $values = str_replace(array('{', '}'), '', $values);
    ${$key} = explode(',', $values);
}
Comments