简体   繁体   中英

Get all array values based on search/filter PHP

I am trying to search for an ID based on a query string value. I have managed to find the relevant page ID, however, I need to get the parent array values as well... Below is an example of the PHP array that is in action.

$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';

I have tried implementing a few answers on here, as I am aware it is a well talked about topic, including:

But both came out with blank arrays as the output.

Any ideas what I need to do to get the desired output?

EDIT: Some pseudo code will hopefully explain the question a bit more clearly

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

I hope that makes it a little clearer!

Found an answer, I didn't search hard enough, found the answer 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM