Convert multidimensional array into single array

后端 未结 20 1943
时光取名叫无心
时光取名叫无心 2020-11-22 10:39

I have an array which is multidimensional for no reason

/* This is how my array is currently */
Array
(
[0] => Array
    (
        [0] => Array
                


        
20条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 11:23

    Problem array:

    array:2 [▼
      0 => array:3 [▼
        0 => array:4 [▼
          "id" => 8
          "name" => "Veggie Burger"
          "image" => ""
          "Category_type" => "product"
        ]
        1 => array:4 [▼
          "id" => 9
          "name" => "Veggie Pitta"
          "image" => ""
          "Category_type" => "product"
        ]
        2 => array:4 [▼
          "id" => 10
          "name" => "Veggie Wrap"
          "image" => ""
          "Category_type" => "product"
        ]
      ]
      1 => array:2 [▼
        0 => array:4 [▼
          "id" => 18
          "name" => "Cans 330ml"
          "image" => ""
          "Category_type" => "product"
        ]
        1 => array:4 [▼
          "id" => 19
          "name" => "Bottles 1.5 Ltr"
          "image" => ""
          "Category_type" => "product"
        ]
      ]
    ]
    

    Solution array:

    array:5 [▼
      0 => array:4 [▼
        "id" => 8
        "name" => "Veggie Burger"
        "image" => ""
        "Category_type" => "product"
      ]
      1 => array:4 [▼
        "id" => 9
        "name" => "Veggie Pitta"
        "image" => ""
        "Category_type" => "product"
      ]
      2 => array:4 [▼
        "id" => 10
        "name" => "Veggie Wrap"
        "image" => ""
        "Category_type" => "product"
      ]
      3 => array:4 [▼
        "id" => 18
        "name" => "Cans 330ml"
        "image" => ""
        "Category_type" => "product"
      ]
      4 => array:4 [▼
        "id" => 19
        "name" => "Bottles 1.5 Ltr"
        "image" => ""
        "Category_type" => "product"
      ]
    ]
    

    Write this code and get your solution , $subcate is your multi dimensional array.

    $singleArrayForCategory = array_reduce($subcate, 'array_merge', array());
    

提交回复
热议问题