Reputation: 37
I'm looking for a solution to get an array from a multidimensional array
For example; This is my array:
Array
(
[1] => Array
(
[id] => 2
[name] => Laïla Mertens
[text] => Laïla Mertens
[parent_id] =>
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 1
[name] => Erwin Dubois
[text] => Erwin Dubois
[parent_id] => 2
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 3
[name] => Test Gebruiker
[text] => Test Gebruiker
[parent_id] => 1
[href] => http://google.com
)
[1] => Array
(
[id] => 4
[name] => Catharina
[text] => Catharina
[parent_id] => 1
[href] => http://google.com
)
)
)
[1] => Array
(
[id] => 5
[name] => Araxanta
[text] => Araxanta
[parent_id] => 2
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 15
[name] => Kevin
[text] => Kevin
[parent_id] => 5
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 16
[name] => Petra
[text] => Petra
[parent_id] => 15
[href] => http://google.com
)
[1] => Array
(
[id] => 17
[name] => Shannah
[text] => Shannah
[parent_id] => 15
[href] => http://google.com
)
)
)
)
)
[2] => Array
(
[id] => 6
[name] => Christian
[text] => Christian
[parent_id] => 2
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 13
[name] => Macy VanMaele
[text] => Macy VanMaele
[parent_id] => 6
[href] => http://google.com
)
[1] => Array
(
[id] => 14
[name] => April Poizat
[text] => April Poizat
[parent_id] => 6
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 7
[name] => Sigrid
[text] => Sigrid
[parent_id] => 14
[href] => http://google.com
)
)
)
)
)
[3] => Array
(
[id] => 8
[name] => Nikita
[text] => Nikita
[parent_id] => 2
[href] => http://google.com
)
[4] => Array
(
[id] => 9
[name] => Salina Lemmens
[text] => Salina Lemmens
[parent_id] => 2
[href] => http://google.com
)
[5] => Array
(
[id] => 10
[name] => Emmily Polen
[text] => Emmily Polen
[parent_id] => 2
[href] => http://google.com
)
[6] => Array
(
[id] => 11
[name] => Jitka Symus
[text] => Jitka Symus
[parent_id] => 2
[href] => http://google.com
)
[7] => Array
(
[id] => 12
[name] => Amina Gigovic
[text] => Amina Gigovic
[parent_id] => 2
[href] => http://google.com
)
)
)
)
I'd like to get the array where the id equals 1
So in this example, where [id] => 1 I would like to capture the entire array of id 1, thus:
Array
(
[0] => Array
(
[id] => 1
[name] => Erwin Dubois
[text] => Erwin Dubois
[parent_id] => 2
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 3
[name] => Test Gebruiker
[text] => Test Gebruiker
[parent_id] => 1
[href] => http://google.com
)
[1] => Array
(
[id] => 4
[name] => Catharina
[text] => Catharina
[parent_id] => 1
[href] => http://google.com
)
)
)
Is there a way in php where I can have a function like;
function get_subarray($id) {
// Select array where id = $id
}
Any point towards the direction is greatly appreciated! It would be needed into a function since I would parse different userlevels depending on their unique id
array:
$json = file_get_contents("https://innerbeauthe.be/cp/_data/fetch.php");
$data = json_decode($json,true);
Upvotes: 1
Views: 65
Reputation: 270607
You can define a recursive function which traverses the array in search of the desired array key.
Although PHP includes a function array_walk_recursive()
, it will traverse into leaf nodes and makes it difficult to return a parent array. Likewise, using RecursiveArrayIterator
actually complicates getting at the array you want instead of the leaf notes. Instead it is not too complicated to create your own:
// Function accepts an array and a search value
function walk_for_array(array $a, $search) {
// Loop over the array
foreach ($a as $k => $v) {
// Find sub-arrays (rather than scalar values)
if (is_array($v)) {
// Check if there is an 'id' key and if it matches your search value
if (isset($v['id']) && $v['id'] == $search) {
// return it
return $v;
}
else {
// Otherwise continue drilling into the array for more arrays
$sub = walk_for_array($v, $search);
// Return the recursive value only if the inner call
// was non-null
if ($sub) return $sub;
}
}
}
}
// Call it, searching for id == 1
$found = walk_for_array($your_big_array, 1);
print_r($found);
// Call it, searching for id == 13
$found = walk_for_array($your_big_array, 13);
print_r($found);
Array
(
[id] => 1
[name] => Erwin Dubois
[text] => Erwin Dubois
[parent_id] => 2
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 3
[name] => Test Gebruiker
[text] => Test Gebruiker
[parent_id] => 1
[href] => http://google.com
)
[1] => Array
(
[id] => 4
[name] => Catharina
[text] => Catharina
[parent_id] => 1
[href] => http://google.com
)
)
)
Array
(
[id] => 13
[name] => Macy VanMaele
[text] => Macy VanMaele
[parent_id] => 6
[href] => http://google.com
)
Upvotes: 1