如何从“ id”列获取搜索数组值

时间:2018-07-21 17:00:32

标签: arrays search

我想从整个数组中分别返回一个id值的数组。所以我正在使用以下代码

$key = array_search('2050', array_column($product_array, 'id'));
print_r($product_array[$key]);

在整个数组的“ id”列中找到值“ 2”时,上述代码起作用。

问题:

但是,如果在id列中找不到值,它将返回整个数组中的第一个索引数组,而不是空return ...,所以这是我的代码中的问题。

这是我整个产品系列。

Array
(
    [0] => Array
        (
            [id] => 10
            [value] => test bag
            [price] => $1,500.00
            [image] => http://127.0.0.1/m2extension/pub/media/catalog/product//c/h/chrysanthemum.jpg
            [url] => http://127.0.0.1/m2extension/test-bag.html
        )

    [1] => Array
        (
            [id] => 2
            [value] => Strive Shoulder Pack
            [price] => $32.00
            [image] => http://127.0.0.1/m2extension/pub/media/catalog/productcachea2d2345650965cd6042e53fd7d716674/m/b/mb04-black-0.jpg
            [url] => http://127.0.0.1/m2extension/strive-shoulder-pack.html
        )

    [2] => Array
        (
            [id] => 3
            [value] => Crown Summit Backpack
            [price] => $38.00
            [image] => http://127.0.0.1/m2extension/pub/media/catalog/productcachea2d2345650965cd6042e53fd7d716674/m/b/mb03-black-0.jpg
            [url] => http://127.0.0.1/m2extension/crown-summit-backpack.html
        )

    [3] => Array
        (
            [id] => 4
            [value] => Wayfarer Messenger Bag
            [price] => $45.00
            [image] => http://127.0.0.1/m2extension/pub/media/catalog/productcachea2d2345650965cd6042e53fd7d716674/m/b/mb05-black-0.jpg
            [url] => http://127.0.0.1/m2extension/wayfarer-messenger-bag.html
        )

    [4] => Array
        (
            [id] => 2045
            [value] => Rival Field Messenger
            [price] => $45.00
            [image] => http://127.0.0.1/m2extension/pub/media/catalog/productcachea2d2345650965cd6042e53fd7d716674/m/b/mb06-gray-0.jpg
            [url] => http://127.0.0.1/m2extension/rival-field-messenger.html
        )

)

0 个答案:

没有答案