PHP - get multiple columns from array

后端 未结 7 1926
星月不相逢
星月不相逢 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:22

    If you need two columns from an array where one is SKU (which generally is unique) then you can use array_column with the third parameter.

    $new = array_column($arr, "product_id", "product_sku");
    

    This will return a flat array with the SKU as the key and ID as value making the array easy to work with also.

    Output:

    array(3) {
      [6500722]=>
      string(4) "1138"
      [6501046]=>
      string(4) "1144"
      [6294915]=>
      string(3) "113"
    }
    

    https://3v4l.org/UDGiO

提交回复
热议问题