LearningROR LearningROR - 2 months ago 5
PHP Question

How to get complete array block if element is found in whole array PHP

array (
'125100 - 33166' =>
array (
'ReferenceNumber' => '125100',
'ShipDate' => '9/12/2016',
'ShipToName' => 'APOLLO EXPORT WAREHOUSE',
'ShipToCompany' => 'c/o Dry Non Bonded Consolidator',
'ShipToAddress1' => '6950 N.W. 77th Court',
'ShipToAddress2' => 'Attn: Alejandro Garcia 305-592-8790 x80',
'ShipToCity' => 'Miami',
'ShipToState' => 'FL',
'ShipToZip' => '33166',
'ShipToContact' => '',
'ShipCarrier' => 'UPS',
'PurchaseOrderNumber' => '10020822001',
'Data' =>
array (
0 =>
array (
0 => 'HBX43C',
1 => 'HBX43C - Norwegian Custom Nespresso Tray
Dimensions: 10.75”x14.75”x Front 2”hx Back 3”h
Leather: Black Faux Leather
Thread: Black
Custom Details: removable compartments; acrylic pod & creamer holders
No velcro on rubber mat
Case pack: 6
UPC: 697182429431',
2 => '12',
3 => '',
),
),
),
'125101 - 33166' =>
array (
'ReferenceNumber' => '125101',
'ShipDate' => '9/12/2016',
'ShipToName' => 'APOLLO EXPORT WAREHOUSE',
'ShipToCompany' => 'c/o Dry Non Bonded Consolidator',
'ShipToAddress1' => '6950 N.W. 77th Court',
'ShipToAddress2' => 'Attn: Alejandro Garcia 305-592-8790 x80',
'ShipToCity' => 'Miami',
'ShipToState' => 'FL',
'ShipToZip' => '33166',
'ShipToContact' => '',
'ShipCarrier' => 'UPS',
'PurchaseOrderNumber' => '10020823001',
'Data' =>
array (
0 =>
array (
0 => 'HBX43C',
1 => 'HBX43C - Norwegian Custom Nespresso Tray
Dimensions: 10.75”x14.75”x Front 2”hx Back 3”h
Leather: Black Faux Leather
Thread: Black
Custom Details: removable compartments; acrylic pod & creamer holders
No velcro on rubber mat
Case pack: 6
UPC: 697182429431',
2 => '18',
3 => '',
),
),
),
'125102 - 33166' =>
array (
'ReferenceNumber' => '125102',
'ShipDate' => '9/12/2016',
'ShipToName' => 'APOLLO EXPORT WAREHOUSE',
'ShipToCompany' => 'c/o Dry Non Bonded Consolidator',
'ShipToAddress1' => '6950 N.W. 77th Court',
'ShipToAddress2' => 'Attn: Alejandro Garcia 305-592-8790 x80',
'ShipToCity' => 'Miami',
'ShipToState' => 'FL',
'ShipToZip' => '33166',
'ShipToContact' => '',
'ShipCarrier' => 'UPS',
'PurchaseOrderNumber' => '1.30E+11',
'Data' =>
array (
0 =>
array (
0 => 'HBX43C',
1 => 'HBX43C - Norwegian Custom Nespresso Tray
Dimensions: 10.75”x14.75”x Front 2”hx Back 3”h
Leather: Black Faux Leather
Thread: Black
Custom Details: removable compartments; acrylic pod & creamer holders
No velcro on rubber mat
Case pack: 6
UPC: 697182429431',
2 => '6',
3 => '',
),
),
),
'11WW111 - 81623' =>
array (
'ReferenceNumber' => '11WW111',
'ShipDate' => '9/12/2016',
'ShipToName' => 'test',
'ShipToCompany' => 'test',
'ShipToAddress1' => 'test',
'ShipToAddress2' => 'test',
'ShipToCity' => 'test',
'ShipToState' => 'CO',
'ShipToZip' => '81623',
'ShipToContact' => '',
'ShipCarrier' => 'TKN',
'PurchaseOrderNumber' => 'test',
'Data' =>
array (
0 =>
array (
0 => '1STTTTS4',
1 => 'test',
2 => '11',
3 => '',
),
),
),
'125103 - 81623' =>
array (
'ReferenceNumber' => '125103',
'ShipDate' => '9/12/2016',
'ShipToName' => 'Old Towne Moving & Storage',
'ShipToCompany' => '',
'ShipToAddress1' => '62 CR 113 Bldg H',
'ShipToAddress2' => 'Attn: Cody Pace 970-384-4444',
'ShipToCity' => 'Carbondale',
'ShipToState' => 'CO',
'ShipToZip' => '81623',
'ShipToContact' => '',
'ShipCarrier' => 'TKN',
'PurchaseOrderNumber' => 'LIMO-053',
'Data' =>
array (
0 =>
array (
0 => '39906B-LIME',
1 => '39906B-LIME - Matte Nickel Ice Tongs
*** Length: 6" ***
Casepack: 48',
2 => '144',
3 => '',
),
),
),
)


How I can find
HBX43C
from whole array and also after finding that element how I can get whole block of that array in which
HBX43C
found.

So, if
HBX43C
found I need to have:

Array
(
[ReferenceNumber] => 125102
[ShipDate] => 9/12/2016
[ShipToName] => APOLLO EXPORT WAREHOUSE
[ShipToCompany] => c/o Dry Non Bonded Consolidator
[ShipToAddress1] => 6950 N.W. 77th Court
[ShipToAddress2] => Attn: Alejandro Garcia 305-592-8790 x80
[ShipToCity] => Miami
[ShipToState] => FL
[ShipToZip] => 33166
[ShipToContact] =>
[ShipCarrier] => UPS
[PurchaseOrderNumber] => 1.30E+11
[Data] => Array
(
[0] => Array
(
[0] => HBX43C
[1] => HBX43C - Norwegian Custom Nespresso Tray
Dimensions: 10.75”x14.75”x Front 2”hx Back 3”h
Leather: Black Faux Leather
Thread: Black
Custom Details: removable compartments; acrylic pod & creamer holders
No velcro on rubber mat
Case pack: 6
UPC: 697182429431
[2] => 6
[3] =>
)

)

)


as
HBX43C
contains in this array block.

The code I am trying is:

$url = "https://www/www/contracts.asmx?wsdl";

$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($soap_do, CURLOPT_URL, $url);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap_do, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_POST, true);
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"http://www.www.com/www/www.WMS/wwww\""
)
);

$response = curl_exec($soap_do);

if (strpos($response, 'Server was unable to process request') !== false) {

$full_response = explode(': ', $response, 2);
if (strpos($response, 'The following sku is not in the item list') !== false) {
$body .= $response;
foreach ($result as $item) {
foreach ($item['Data'] as $sub_item) {
if (in_array(trim($full_response[1]), $sub_item)) {
$body .= "SKU Number: " . $full_response[1];
print_r($sub_item);
break 2;
}
}
}
}

$body .= "<br /><br />";
$body .= "Used File: " . $files[2];
$body .= "<br /><br />";
echo $body;
echo "<br /><br />";
} else {
$body .= "Operation completed without any errors";
$body .= "<br /><br />";
$body .= "Used File: " . $files[2];
$body .= "<br /><br />";
echo $body;
echo "<br /><br />";
}
}


It seems not working as it does not return whole block to me.

$full_response[1]
is :
HBX43C


Where
$result
is whole array.

Answer

Required:

Return the parent array entry where:

  • the 'Data' property, which is a 'nested' array - contains a required value ($needle).

How:

  • For each entry: search the 'Data' array for a value match with a supplied $needle.
  • The 'Data' array will be required to be searched recursively.

Demonstration at eval.in

Note: the code uses variables to store intermediate results to make it easier to follow.

Code:

o Search all the entries in the source list

/**
* Search all the entries trying to find the needle in the 'nested' Data array
* 
* @param array  $srcArray
* @param string $needle
* 
* @return array 
*/
function searchHaystacks($srcArray, $needle)
{
    $allMatches = array();

    foreach ($srcArray as $key => $haystack) {
        if (searchDataArray($haystack['Data'], $needle)) {
            $allMatches[$key] = $haystack;
        }
    }

    return $allMatches;    
}

o The 'Data Array' search function:

 /**
 * Give a Data array check if a given value is anywhere in it
 * this is recursive.
 * 
 * @param  array  $dataArray  - Array to recursively search 
 * @param  string $needle     - value to match
 * @return boolean            - true if needle found in any of the nested arrays
 */
function searchDataArray($dataArray, $needle)
{
    $matched = in_array($needle, $dataArray);
    if ($matched) {
        return true;
    }

    // any nested arrays?
    foreach ($dataArray as $haystack) {
        if (is_array($haystack)) { // another array to check
            $matched = searchDataArray($haystack, $needle);
            if ($matched) {
                return true;
            }
        }
    }
    return false; // no array contains the needle
}

Run a search:

$needle = 'HBX43C'; 
$allNeedles = searchHaystacks($srcArray, $needle); 

Output from the given test data:

Matched Needles...
Array
(
    [125100 - 33166] => Array
        (
            [ReferenceNumber] => 125100
            [ShipDate] => 9/12/2016
            [ShipToName] => APOLLO EXPORT WAREHOUSE
            [ShipToCompany] => c/o Dry Non Bonded Consolidator
            [ShipToAddress1] => 6950 N.W. 77th Court
            [ShipToAddress2] => Attn: Alejandro Garcia 305-592-8790 x80
            [ShipToCity] => Miami
            [ShipToState] => FL
            [ShipToZip] => 33166
            [ShipToContact] => 
            [ShipCarrier] => UPS
            [PurchaseOrderNumber] => 10020822001
            [Data] => Array
                (
                    [0] => Array
                        (
                            [0] => HBX43C
                            [1] => HBX43C - Norwegian Custom Nespresso Tray
Dimensions: 10.75â€x14.75â€x Front 2â€hx Back 3â€h 
Leather: Black Faux Leather
Thread: Black
Custom Details: removable compartments; acrylic pod & creamer holders
No velcro on rubber mat
Case pack: 6
UPC: 697182429431
                            [2] => 12
                            [3] => 
                        )
                )
        )

    [125101 - 33166] => Array
        (
            [ReferenceNumber] => 125101
            [ShipDate] => 9/12/2016
            [ShipToName] => APOLLO EXPORT WAREHOUSE
            [ShipToCompany] => c/o Dry Non Bonded Consolidator
            [ShipToAddress1] => 6950 N.W. 77th Court
            [ShipToAddress2] => Attn: Alejandro Garcia 305-592-8790 x80
            [ShipToCity] => Miami
            [ShipToState] => FL
            [ShipToZip] => 33166
            [ShipToContact] => 
            [ShipCarrier] => UPS
            [PurchaseOrderNumber] => 10020823001
            [Data] => Array
                (
                    [0] => Array
                        (
                            [0] => HBX43C
                            [1] => HBX43C - Norwegian Custom Nespresso Tray
Dimensions: 10.75â€x14.75â€x Front 2â€hx Back 3â€h 
Leather: Black Faux Leather
Thread: Black
Custom Details: removable compartments; acrylic pod & creamer holders
No velcro on rubber mat
Case pack: 6
UPC: 697182429431
                            [2] => 18
                            [3] => 
                        )
                ) 
        )

    [125102 - 33166] => Array
        (
            [ReferenceNumber] => 125102
            [ShipDate] => 9/12/2016
            [ShipToName] => APOLLO EXPORT WAREHOUSE
            [ShipToCompany] => c/o Dry Non Bonded Consolidator
            [ShipToAddress1] => 6950 N.W. 77th Court
            [ShipToAddress2] => Attn: Alejandro Garcia 305-592-8790 x80
            [ShipToCity] => Miami
            [ShipToState] => FL
            [ShipToZip] => 33166
            [ShipToContact] => 
            [ShipCarrier] => UPS
            [PurchaseOrderNumber] => 1.30E+11
            [Data] => Array
                (
                    [0] => Array
                        (
                            [0] => HBX43C
                            [1] => HBX43C - Norwegian Custom Nespresso Tray
Dimensions: 10.75â€x14.75â€x Front 2â€hx Back 3â€h 
Leather: Black Faux Leather
Thread: Black
Custom Details: removable compartments; acrylic pod & creamer holders
No velcro on rubber mat
Case pack: 6
UPC: 697182429431
                            [2] => 6
                            [3] => 
                        )
                )
        )
)
Comments