add another array data in array php

♀尐吖头ヾ 提交于 2021-01-07 02:48:51

问题


hello i am trying to add another array data to an array, i have array like this,

    array:4 [▼
  "data" => array:19 [▼
    0 => array:2 [▼
      0 => array:1 [▼
        "filename" => "a"
      ]
      1 => array:1 [▼
        "filename" => "b"
      ]
    ]
    1 => array:2 [▼
      0 => array:1 [▼
        "filename" => "c"
      ]
      1 => array:1 [▼
        "filename" => "d"
      ]
    ]
    2 => array:2 [▼
      0 => array:1 [▼
        "filename" => "e"
      ]
      1 => array:1 [▼
        "filename" => "f"
      ]
    ]
    
  ]
  "video" => array:2 [▼
    0 => array:1 [▼
      "url" => "x"
    ]
    1 => array:1 [▼
      "url" => "y"
    ]
  ]
  
]

i want array like this

array:4 [▼
  "data" => array:19 [▼
    0 => array:2 [▼
      0 => array:1 [▼
        "filename" => "a",
      ]
      1 => array:1 [▼
        "filename" => "b"
      ],
      2 => array:1 [▼
        "url" => "x"
      ]
    ]
    1 => array:2 [▼
      0 => array:1 [▼
        "filename" => "c"
      ]
      1 => array:1 [▼
        "filename" => "d"
      ],
      2 => array:1 [▼
        "url" => "y"
      ]
    ]
    2 => array:2 [▼
      0 => array:1 [▼
        "filename" => "e"
      ]
      1 => array:1 [▼
        "filename" => "f"
      ],
      2 => array:1 [▼
        "url" => "x"
      ]
    ] 
  ]
]

i am trying to add array but still no result, is any way to solve this issue in php. please suggest me.

"video" data have only 2 , it will repeated , 1-2-3-1-2-3 on that array.

please suggest me how can i solve this


回答1:


You should be able to do this with array_map:

$urls = $data['video'];

end($urls);

$result = array_map(function ($i) use (&$urls) {
    $i[] = next($urls) ?: reset($urls);
    return $i;
}, $array['data']);


来源:https://stackoverflow.com/questions/65403434/add-another-array-data-in-array-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!