根据搜索/过滤PHP获取所有数组值

时间:2015-08-24 17:23:13

标签: php arrays

我正在尝试根据查询字符串值搜索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';

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

但两者都以空白数组作为输出。

我需要做些什么来获得所需的输出?

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

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

我希望能让它更清晰一点!

1 个答案:

答案 0 :(得分:0)

找到答案,我没有足够的搜索,找到了答案here ......

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));