php - deep search array of arrays and return only matching elements

后端 未结 5 920
忘掉有多难
忘掉有多难 2020-12-18 16:31

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 s

5条回答
  •  有刺的猬
    2020-12-18 17:04

    This should do the trick.

    function getParentStackComplete($search, $stack)
    {
        $results = [];
        foreach ($stack as $i => $item) {
            $results[] = $item;
            $cache = $item['asset_info'];
            $results[$i]['asset_info'] = [];
            $found = false;
    
            foreach ($cache as $asset) {
                if (array_search($search, $asset) !== false) {
                    print('here' . "\n");
                    $found = true;
                    $results[$i]['asset_info'][] = $asset;
                }
            }
    
            if (!$found) {
                unset($results[$i]);
            }
        }
        return $results;
    }
    

    Edit: This assumes you're calling like this getParentStackComplete('Oracle', $items['tableData'])

提交回复
热议问题