Array to multidimensional array … based on foo.bar.baz-dots in array key name?

Php $arr = array('foo.bar. Baz' => 1, 'qux' => 1); function array_dotkey(array $arr) { // Loop through each key/value pairs. Foreach ( $arr as $key => $value ) { if ( strpos($key, '.

')! == FALSE ) { // Reference to the array. $tmparr =& $arr; // Split the key by ".

" and loop through each value. Foreach ( explode('.', $key) as $tmpkey ) { // Add it to the array. $tmparr$tmpkey = array(); // So that we can recursively continue adding values, change $tmparr to a reference of the most recent key we've added.

$tmparr =& $tmparr$tmpkey; } // Set the value. $tmparr = $value; // Remove the key that contains ". " characters now that we've added the multi-dimensional version.

Unset($arr$key); } } return $arr; } $arr = array_dotkey($arr); print_r($arr) Outputs: Array ( qux => 1 foo => Array ( bar => Array ( baz => 1 ) ) ).

Solution: 1, 'qux' => 1); function array_dotkey(array $arr) { // Loop through each key/value pairs. Foreach ( $arr as $key => $value ) { if ( strpos($key, '. ')!

== FALSE ) { // Reference to the array. $tmparr =& $arr; // Split the key by ". " and loop through each value.

Foreach ( explode('. ', $key) as $tmpkey ) { // Add it to the array. $tmparr$tmpkey = array(); // So that we can recursively continue adding values, change $tmparr to a reference of the most recent key we've added.

$tmparr =& $tmparr$tmpkey; } // Set the value. $tmparr = $value; // Remove the key that contains ". " characters now that we've added the multi-dimensional version.

Unset($arr$key); } } return $arr; } $arr = array_dotkey($arr); print_r($arr); Outputs: Array ( qux => 1 foo => Array ( bar => Array ( baz => 1 ) ) ).

1 I couldn't figure that out :) – AlienWebguy Jul 10 at 8:10 Nice. Can you put it into a function? Something like array_dotkey($value,&$tmparr) { ... } – Kristoffer Bohmann Jul 10 at 8:19 @Kristoffer Bohmann - You certainly can but I would just recommend create a function that accepts an array.

I'll update the answer. – Francois Deschenes Jul 10 at 8:20 love the comment on every line ( :D joke ) – yes123 Jul 10 at 16:55.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions