I am looking for a solution in php
as mentioned in the accepted answer of this question:
javascript - return parent with only child that matches given search string in array of objects with nested object
Please find below code:
<?php
$items = array(
'tableData' => array
(
array
(
'booking_name' => 'abc/xyz/123',
'pdg' => 'assure',
'user_area' => 'es st1',
'release' => 'oss72',
'start_date' => '2017-06-20 00:00:00',
'end_date' => '2017-06-23 00:00:00',
'asset_info' => array
(
array
(
'status' => 10,
'manufacturer' => 'Oracle',
'model' => 'HP BL460C GEN8',
'hardware_color' => '#0066b3',
),
array
(
'status' => 11,
'manufacturer' => 'HP',
'model' => 'HP BL460C GEN81',
'hardware_color' => '#0066b3',
)
),
'full_name' => 'Valay Desai',
'email_address' => '[email protected]',
),
array
(
'booking_name' => 'abc/xyz/123',
'pdg' => 'enm',
'user_area' => 'es st',
'release' => 'oss72',
'start_date' => '2017-06-20 00:00:00',
'end_date' => '2017-06-23 00:00:00',
'asset_info' => array
(
array
(
'status' => 10,
'manufacturer' => 'HP',
'model' => 'HP BL460C GEN8',
'hardware_color' => '#0066b3',
)
),
'full_name' => 'Valay Desai',
'email_address' => '[email protected]',
)
)
);
function getParentStackComplete($child, $stack) {
$return = array();
foreach ($stack as $k => $v) {
if (is_array($v)) {
// If the current element of the array is an array, recurse it
// and capture the return stack
$stack = getParentStackComplete($child, $v);
// If the return stack is an array, add it to the return
if (is_array($stack) && !empty($stack)) {
$return[] = $v;
}
} else {
// Since we are not on an array, compare directly
if(strpos($v, $child) !== false){
// And if we match, stack it and return it
$return[] = $v;
}
}
}
// Return the stack
return empty($return) ? false: $return;
}
echo "<pre>";
print_r(getParentStackComplete('Oracle', $items['tableData']));
echo "</pre>";
?>
This code works fine. I found the function getParentStackComplete
online, modified it to return the whole matching element. It search the array recursively and return matching items.
For example, as given in the code, If I search for a string 'Oracle', it should return an array with one item which has only one child(matching element) in asset_info
. The output I am looking for is:
Array
(
[0] => Array
(
[booking_name] => abc/xyz/123
[pdg] => assure
[user_area] => es st1
[release] => oss72
[start_date] => 2017-06-20 00:00:00
[end_date] => 2017-06-23 00:00:00
[asset_info] => Array
(
[0] => Array
(
[status] => 10
[manufacturer] => Oracle
[model] => HP BL460C GEN8
[hardware_color] => #0066b3
)
)
[full_name] => Valay Desai
[email_address] => [email protected]
)
)
If I search for a string HP BL460C GEN8
, it should return as below:
Array
(
[0] => Array
(
[booking_name] => abc/xyz/123
[pdg] => assure
[user_area] => es st1
[release] => oss72
[start_date] => 2017-06-20 00:00:00
[end_date] => 2017-06-23 00:00:00
[asset_info] => Array
(
[0] => Array
(
[status] => 10
[manufacturer] => Oracle
[model] => HP BL460C GEN8
[hardware_color] => #0066b3
)
)
[full_name] => Valay Desai
[email_address] => [email protected]
)
[1] => Array
(
'booking_name' => 'abc/xyz/123',
'pdg' => 'enm',
'user_area' => 'es st',
'release' => 'oss72',
'start_date' => '2017-06-20 00:00:00',
'end_date' => '2017-06-23 00:00:00',
'asset_info' => array
(
array
(
'status' => 10,
'manufacturer' => 'HP',
'model' => 'HP BL460C GEN8',
'hardware_color' => '#0066b3',
)
),
'full_name' => 'Valay Desai',
'email_address' => '[email protected]'
)
)
How do I return matching child with parent in nested array search ?
See Question&Answers more detail:
os