PHP - get multiple columns from array

后端 未结 7 1846
星月不相逢
星月不相逢 2021-01-12 09:55

I have this array:

0 => array:3 [
    \"product_id\" => \"1138\"
    \"product_image\" => \"/resources/medias/shop/products/shop-6500720--1.png\"
           


        
7条回答
  •  一生所求
    2021-01-12 10:21

    I refactored the elegant approach from @Chayan into a function so it can be used like array_column(). Keys to be filtered can now be presented as a simple array.

    Btw this is most likely also the fastest approach, since it uses build-in functions for most of the heavy lifting.

     "1138",
            "product_image" => "/resources/medias/shop/products/shop-6500720--1.png",
            "product_sku"   => "6500722"
        ],
        [
            "product_id"    => "1144",
            "product_image" => "/resources/medias/shop/products/shop-6501041--1.png",
            "product_sku"   => "6501046"
        ],
        [
            "product_id"    => "113",
            "product_image" => "/resources/medias/shop/products/shop-6294909--1.png",
            "product_sku"   => "6294915"
        ]
    );
    
    $keysSelect = array("product_id" , "product_sku");
    $filteredArray = array_colums($arr, $keysSelect);
    
    var_dump($filteredArray);
    

提交回复
热议问题