简体   繁体   English

如果在整个数组PHP中找到元素,如何获取完整的数组块

[英]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. 如何从整个阵列中找到HBX43C ,以及找到该元素后如何获得在其中找到HBX43C的那个阵列的整个块。

So, if HBX43C found I need to have: 因此,如果发现HBX43C我需要具备:

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. 因为HBX43C包含在此阵列模块中。

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 $full_response[1]是: HBX43C

Where $result is whole array. 其中$result是整个数组。

If the $result is whole array, then in_array won't find anything there. 如果$result是整个数组,则in_array在那里找不到任何内容。 Because in_array doesn't inspect inner arrays. 因为in_array 不检查内部数组。

What you need to do is search ['Data'] items for your value: 您需要做的是在['Data']项目中搜索您的值:

foreach ($result as $item) {
    foreach ($item['Data'] as $sub_item) {
        if (in_array($full_response[1], $sub_item)) {
            echo sprintf("'xxx' is in '%s'", implode(', ', $subitem));
            // optionally to stop checking other subarrays - use `break` 
            break 2;
        }
    }
}

Required: 需要:

Return the parent array entry where: 返回父数组条目,其中:

  • the 'Data' property, which is a 'nested' array - contains a required value ( $needle ). “数据”属性是一个“嵌套”数组-包含必需的值( $ needle )。

How: 怎么样:

  • For each entry: search the 'Data' array for a value match with a supplied $needle. 对于每个条目:在“数据”数组中搜索与提供的$ 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 o搜索源列表中的所有条目

/**
* 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: o“数据数组”搜索功能:

 /**
 * 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] => 
                        )
                )
        )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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