Question

I'm trying to experiment with array_splice and I get an output like this (from $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 (this foreach creates puts in the values into $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']);        
    }
}

What I'm trying to get is bring [0],[1],[2] into the [Keep me Updated] $match array:

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
        )
}

This is what $data looks like

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

$data has array values, and $match is where I'm trying to sort the data. So if my values match, it'll consolidate.

Thanks!

Was it helpful?

Solution

Use the inner array as the argument to 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']);        
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top