doumouyi4039 2016-01-01 11:47
浏览 104
已采纳

php数组具有相同的键,在一个数组中合并和重新排列键

I has array when i use var_dump function to print array show me the below result

var_dump($val);

the result is

array (size=1)
0 => string '6' (length=1)

array (size=1)
0 => string '6' (length=1)

array (size=1)
0 => string '4' (length=1)

as see the $val contains three different array that have the zero index key

how i can merge them in one array and change or rearrange the zero index to 0, 1 and 2

EDITED

for more information when i use print_r($val) the result as below

Array ( [0] => 6 ) Array ( [0] => 6 ) Array ( [0] => 4 )

but the number of array in $val various not every time ins three arrays would be 4 array or 5 array etc..

how i can merge all in one array and index of them?

  • 写回答

3条回答 默认 最新

  • dpnru86024 2016-01-01 11:56
    关注

    I believe you have an array like this,

    $val = array(
        0 => array("6"),
        1 => array("6"),
        2 => array("4")
    );
    

    So to merge the array elements and rearrange the indices, you can do something like this:

    foreach($val as $key => $array){
        $val[$key] = $array[0];
    }
    
    var_dump($val);
    

    Output:

    array (size=3)
      0 => string '6' (length=1)
      1 => string '6' (length=1)
      2 => string '4' (length=1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部