PHP array_search通过变量在多维数组中

时间:2018-07-17 20:42:31

标签: php arrays search multidimensional-array

我有一个数组,希望在其中过滤掉有关OrderId的所有信息。因此,我想通过其中具有我的OrderId的变量来array_search我的数组。 这样,我可以获得数组键,然后使用array_slice对该数组进行切片。

Array
( 
    [Order] => Array
        ( 
            [0] => Array
                ( 
                    [OrderId] => 4832413060 
                    [DateTimeCustomer] => 2018-07-17T12:55:07.000+02:00 
                    [DateTimeDropShipper] => 2018-07-17T12:55:07.000+02:00 
                    [CustomerDetails] => Array 
                        ( 
                            [ShipmentDetails] => Array 
                                ( 
                                    [SalutationCode] => 01 
                                    [Firstname] => name 
                                    [Surname] => sirname 
                                    [Streetname] => streetname 
                                    [Housenumber] => number 
                                    [ZipCode] => zipcode 
                                    [City] => city 
                                    [CountryCode] => countryCode
                                    [Email] => email 
                                ) 
                            [BillingDetails] => Array 
                                ( 
                                    [SalutationCode] => 01 
                                    [Firstname] => name
                                    [Surname] => sirname
                                    [Streetname] => streetname 
                                            [Housenumber] => number 
                                    [ZipCode] => zipcode 
                                    [City] => city 
                                    [CountryCode] => countryCode 
                                    [Email] => email
                                ) 
                        ) 
                    [OrderItems] => Array
                        ( 
                            [OrderItem] => Array 
                                ( 
                                    [OrderItemId] => orderitemid 
                                    [OfferReference] => Array 
                                        ( 

                                        ) 
                                    [EAN] => ean 
                                    [Title] => title 
                                    [Quantity] => 1 
                                    [OfferPrice] => 14.95 
                                    [TransactionFee] => 3.24 
                                    [LatestDeliveryDate] => 2018-07-18+02:00 
                                )
                        )
                ) 
            [1] => Array
                ( 
                    [OrderId] => 4832413070
                    [DateTimeCustomer] => 2018-07-17T12:55:07.000+02:00 
                    [DateTimeDropShipper] => 2018-07-17T12:55:07.000+02:00 
                    [CustomerDetails] => Array 
                        ( 
                            [ShipmentDetails] => Array 
                                ( 
                                    [SalutationCode] => 01 
                                    [Firstname] => name 
                                    [Surname] => sirname 
                                    [Streetname] => streetname 
                                    [Housenumber] => number 
                                    [ZipCode] => zipcode 
                                    [City] => city 
                                    [CountryCode] => countryCode
                                    [Email] => email 
                                ) 
                            [BillingDetails] => Array 
                                ( 
                                    [SalutationCode] => 01 
                                    [Firstname] => name
                                    [Surname] => sirname
                                    [Streetname] => streetname 
                                    [Housenumber] => number 
                                    [ZipCode] => zipcode 
                                    [City] => city 
                                    [CountryCode] => countryCode 
                                    [Email] => email
                                ) 
                        ) 
                    [OrderItems] => Array
                        ( 
                            [OrderItem] => Array 
                                ( 
                                    [OrderItemId] => orderitemid 
                                    [OfferReference] => Array 
                                        ( 

                                        ) 
                                    [EAN] => ean 
                                    [Title] => title 
                                    [Quantity] => 1 
                                    [OfferPrice] => 14.95 
                                    [TransactionFee] => 3.24 
                                    [LatestDeliveryDate] => 2018-07-18+02:00 
                                )
                        )
                ) 

我尝试的代码:

foreach ($items as $item) {
$key = array_search($item, $array1);

$ item与OrderId相同。

我从互联网上尝试了许多不同的示例,但不幸的是,似乎没有什么适合我。

2 个答案:

答案 0 :(得分:0)

array_search()不适合多维数组。使用array_filter()

foreach ($items as $item) {
    $found = array_filter($array, function($element) use($item) {
        return $element['OrderId'] == $item;
    }
    // $found now contains all the elements in the original array for $item
    ...
}

答案 1 :(得分:0)

我认为您可以使用array_column使数组与以orderid为键的关联,从而非常容易地找到orderid。 但这假设您在数组中只有唯一的订单ID。

$find = 4832413060;
$arr = array_column($arr, NULL, 'orderid');

$findOrder = $arr[$find];

Var_dump($findOrder);

我做了一个小数组来演示代码。
https://3v4l.org/jEus3