How do I use the multidimensional array in foreach?

后端 未结 3 1684
庸人自扰
庸人自扰 2021-01-23 02:26

I have an array $cart:

array:1 [
  "product" => array:5 [
      "product_id" => array:2 [
      0 => 2
      1 =>         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-23 03:29

    if product_id is a consistent field for your product array, you can use its length as an iterator count to perform lookups on the other keys. WARNING this assumes that ALL keys you're using to output into the template have the same length as product_id, and that each index within each key refers to the same product.

    You dont appear to have a key for quantity so havent populated that field

    
        

    $ x


    PS. unfortunately i am unfamiliar with Laravel syntax, so this is in standard PHP


    EDIT

    Because it didnt sit right with me to be using that format i have created a converter function that should turn your array into something more manageable

    
        

    $ x


提交回复
热议问题