Insert values into array based on key
I have a function producing this kind of array:
Array ( [0] => 1 [id] => 1 [1] => home [slug] => home [2] => [parent_id] => [3] => 1 [publish] => 1 [4] => content [type] => content
[id_2] => Array ( [0] => 2 [id] => 2 [1] => 404_error [slug] => 404_error [2] => 1 [parent_id] => 1 [3] => 1 [publish] => 1 [4] => content [type] => content ) )
I need a function that will allow me to find id_x in this array and append a new array in the array it occurs in, regardless of the deph where the desired id_x key is.
e.g like this
Array ( [0] => 1 [id] => 1 [1] => home [slug] => home [2] => [parent_id] => [3] => 1 [publish] => 1 [4] => content [type] => content
[id_2] => Array ( [0] => 2 [id] => 2 [1] => 404_error [slug] => 404_error [2] => 1 [parent_id] => 1 [3] => 1 [publish] => 1 [4] => content [type] => content
[id_3] => Array ( [0] => 3 [id] => 3 [1] => generic [slug] => generic [2] => 2 [parent_id] => 2 [3] => 1 [publish] => 1 [4] => forms [type] => forms ) ) ) )
I.E Where [parent_id] matches id_x, insert a new array.
I've looked in the manual and nothing seems entirely appropiate.
var_export on the current array:
array ( 'id_1' => array ( 0 => '1', 'id' => '1', 1 => 'home', 'slug' => 'home', 2 => '', 'parent_id' => '', 3 => '1', 'publish' => '1', 4 => 'content', 'type' => 'content', 'id_2' => array ( 0 => '2', 'id' => '2', 1 => '404_error', 'slug' => '404_error', 2 => '1', 'parent_id' => '1', 3 => '1', 'publish' => '1', 4 => 'content', 'type' => 'content', ), ), 'id_7' => array ( 0 => '7', 'id' => '7', 1 => 'login_expiry', 'slug' => 'login_expiry', 2 => '', 'parent_id' => '', 3 => '1', 'publish' => '1', 4 => 'content', 'type' => 'content', 'id_4' => array ( 0 => '4', 'id' => '4', 1 => 'login_expiry', 'slug' => 'login_expiry', 2 => '7', 'parent_id' => '7', 3 => '0', 'publish' => '0', 4 => 'content', 'type' => 'content', ), ), 'id_2' => array ( 'id_3' => array ( 0 => '3', 'id' => '3', 1 => 'generic', 'slug' => 'generic', 2 => '2', 'parent_id' => '2', 3 => '1', 'publish' => '1', 4 => 'forms', 'type' => 'forms', ), ), 'id_4' => array ( 'id_5' => array ( 0 => '5', 'id' => '5', 1 => '404_error', 'slug' => '404_error', 2 => '4', 'parent_id' => '4', 3 => '1', 'publish' => '1', 4 => 'content', 'type' => 'content', ), ), 'id_5' => array ( 'id_6' => array ( 0 => '6', 'id' => '6', 1 => 'generic', 'slug' => 'generic', 2 => '5', 'parent_id' => '5', 3 => '1', 'publish' => '1', 4 => 'forms', 'type' => 'forms', ), ), )
Maybe array_walk_recursive will help you.
精彩评论