基于搜索/过滤PHP获取所有数组值


Get all array values based on search/filter PHP

我试图根据查询字符串值搜索ID。我已经设法找到相关的页面ID,但是,我需要得到父数组值以及…下面是一个正在运行的PHP数组的例子:

$data = 
Array(
[_v] => 14.2
[count] => 2
[data] => Array (
    [0] => Array (
        [brand] => Gray & Willow
        [currency] => GBP
        [id] => 218861276
        [inventory] => Array (
            [ats] => 5
            [backorderable] => 
            )
        [long_description] => Gray & Willow Marta Marble Dip Hem Tunic Dress
Shift Dress
Print
Standard length
Rounded collar
Short-sleeved
No fastening
Standard waist
100% Viscose
Hand wash only
        [manufacturer_name] => Gray & Willow
        [name] => Marta Marble Dip Hem Tunic Dress
        [price] => 89
        [type] => Array (
            [variant] => 1
            )
        [c_COMPOSITION] => 100% Viscose
        [c_Care Instructions] => Hand wash only
        [c_CodeSkuId] => 218861276
        [c_Collar/Neck] => Rounded collar
        [c_Colour] => Multi-Coloured
        [c_CountryOrigin] => IND
        [c_Exclusive] => Yes
        [c_FabCont] => 100% Viscose
        [c_Fabric] => Viscose
        [c_Gender] => Women
        [c_HarmPrdCode] => 6204440090
        [c_Length WW] => Standard length
        [c_Model Height] => N/A
        [c_Organic] => N/A
        [c_Parent Colour] => Multi Coloured
        [c_Parent Style] => DRESS
        [c_Pattern WW] => Print
        [c_Planning Range] => DRESSES
        [c_PrimaryImage] => I_218861292_00_20150710
        [c_RptTypdesc] => WW DRESSES
        [c_Size] => 8
        [c_SkuDesc] => Marta Marble Dip Hem Tunic Dress
        [c_SkuMisc] => 2102188612766
        [c_Sleeve Type WW] => Short-sleeved
        [c_Style] => Shift Dress
        [c_Style WW] => Shift dress
        [c_UnitWt] => 0.2400
        [c_WAREHOUSE] => M
        [c_Waist Type] => Standard waist
        [c_Web Categories] => Day Dresses
        [c_Web Supply Lane] => iForce Deliveries
        [c_firstOnlineDate] => 2015-08-21
        [c_qosListID] => QOS19
        [c_sellByUnit] => 
        )
    [1] => Array (
        [brand] => Gray & Willow
        [currency] => GBP
        // SEARCHING FOR THIS ID - but need to access the whole parent Array... [1] => Array
        [id] => 218861284
        [inventory] => Array (
            [ats] => 6
            [backorderable] => 
            )
        [long_description] => Gray & Willow Marta Marble Dip Hem Tunic Dress
Shift Dress
Print
Standard length
Rounded collar
Short-sleeved
No fastening
Standard waist
100% Viscose
Hand wash only
        [manufacturer_name] => Gray & Willow
        [name] => Marta Marble Dip Hem Tunic Dress
        [price] => 89
        [type] => Array (
            [variant] => 1
            )
        [c_COMPOSITION] => 100% Viscose
        [c_Care Instructions] => Hand wash only
        [c_CodeSkuId] => 218861284
        [c_Collar/Neck] => Rounded collar
        [c_Colour] => Multi-Coloured
        [c_CountryOrigin] => IND
        [c_Exclusive] => Yes
        [c_FabCont] => 100% Viscose
        [c_Fabric] => Viscose
        [c_Gender] => Women
        [c_HarmPrdCode] => 6204440090
        [c_Length WW] => Standard length
        [c_Model Height] => N/A
        [c_Organic] => N/A
        [c_Parent Colour] => Multi Coloured
        [c_Parent Style] => DRESS
        [c_Pattern WW] => Print
        [c_Planning Range] => DRESSES
        [c_PrimaryImage] => I_218861292_00_20150710
        [c_RptTypdesc] => WW DRESSES
        [c_Size] => 10
        [c_SkuDesc] => Marta Marble Dip Hem Tunic Dress
        [c_SkuMisc] => 2102188612841
        [c_Sleeve Type WW] => Short-sleeved
        [c_Style] => Shift Dress
        [c_Style WW] => Shift dress
        [c_UnitWt] => 0.2400
        [c_WAREHOUSE] => M
        [c_Waist Type] => Standard waist
        [c_Web Categories] => Day Dresses
        [c_Web Supply Lane] => iForce Deliveries
        [c_firstOnlineDate] => 2015-08-21
        [c_qosListID] => QOS19
        [c_sellByUnit] => 
        )
    )
[total] => 2);

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
                    return true;
        }
    }
    return false;
}
// 'found' successfully echoing     
echo in_array_r($pageID, $data) ? 'found' : 'not found';

我试着在这里实现一些答案,因为我知道这是一个很好的话题,包括:

  • 查找数组并返回匹配的键和值
  • 如何在PHP多维数组中按键=>值进行搜索

但是两者的输出都是空白数组。

知道我需要做什么才能得到想要的输出吗?

EDIT:一些伪代码有望更清楚地解释这个问题

IF query string EQUALS id in array
  load parent array (array that the id is in)

我希望这能让它更清楚一点!

找到了答案,我没有努力搜索,在这里找到了答案…

function search($array, $key, $value)
        {
            $results = array();
            if (is_array($array)) {
                if (isset($array[$key]) && $array[$key] == $value) {
                    $results[] = $array;
                }
                foreach ($array as $subarray) {
                    $results = array_merge($results, search($subarray, $key, $value));
                }
            }
            return $results;
        }
        // $pageID being the query string
        print_r(search($data, 'id', $pageID));