0

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!

5
  • 1
    What is your actual output? Commented Dec 4, 2012 at 6:57
  • @Asad sorry didn't make myself clear. The actual output is the first code, the values are going outside of the array I want to put into Commented Dec 4, 2012 at 6:59
  • Alright, but what is the purpose of having a single array inside an array? Commented Dec 4, 2012 at 7:00
  • @Asad there's more to it, but this is just the premise of the code I'm trying to figure out how to fix. Commented Dec 4, 2012 at 7:02
  • Try turning $match[$d['data']['name']] into $match['Keep me Updated'][$d['data']['name']]. Also, please post the values for $name etcetera Commented Dec 4, 2012 at 7:04

1 Answer 1

1

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']);        
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.