0

我正在尝试使用 array_splice 并得到这样的输出(来自 $match)

Array
(
    [Keep me Updated] => Array
        (
            [winner] => winnerl.jpg
            [0] => value0.jpg
        )

    [0] => valuel.jpg //this should really be inside [Leep me Updated] array
    [1] => value2.jpg //this should really be inside [Leep me Updated] array
    [2] => value3.jpg //this should really be inside [Leep me Updated] array
}

from(这个 foreach 创建将值放入 $match 中)

foreach($data as $d)
{
    if (isset($match[$d['data']['name']])) {
        $match_loser = array($d['loser']['lrg_img']);

        array_splice($match,1,0,$match_loser);
    }else{
        $match[$d['data']['name']] = array("winner"=>$d['winner']['lrg_img'],
                                            $d['loser']['lrg_img']);        
    }
}

我想要得到的是带入[0],[1],[2]$ [Keep me Updated]match 数组:

Array
(
    [Keep me Updated] => Array
        (
            [winner] => winnerl.jpg
            [0] => value0.jpg
            [1] => value1.jpg   // old one: [0] => valuel.jpg
            [2] => value2.jpg   // old one: [1] => value2.jpg
            [3] => value3.jpg   // old one: [2] => value3.jpg
        )
}

这就是 $data 的样子

    $data[] = array(
    "data"=>array
    (
        "name"=>$name,
    ),
    "winner"=>array
    (
        "lrg_img"=>$img_url_winner
    ),      
    "loser"=>array
    (
        "lrg_img"=>$img_url_loser
    )

$data 具有数组值,而 $match 是我尝试对数据进行排序的地方。所以如果我的价值观匹配,它就会巩固。

谢谢!

4

1 回答 1

1

使用内部数组作为参数array_splice

foreach($data as $d)
{
    if (isset($match[$d['data']['name']])) {
        $match_loser = array($d['loser']['lrg_img']);

        array_splice($match[$d['data']['name']],1,0,$match_loser);
    }else{
        $match[$d['data']['name']] = array("winner"=>$d['winner']['lrg_img'],
                                            $d['loser']['lrg_img']);        
    }
}
于 2012-12-04T07:28:21.103 回答