0

I am using this to create an array:

foreach ($results as $tire) {
    $group_price[] = array($tire->group, $tire->price);
}

My results are:

Array ( 
    [0] => Array (
        [0] => MAXAT 
        [1] => 118.91 
        ) 
    [1] => Array (
        [0] => FZSUV 
        [1] => 137.81 
    ) 
    [2] => Array ( 
        [0] => MAXAT 
        [1] => 153.79 
    )
)

What I would like my results to be:

Array (
    [0] => Array (
        [group] => MAXAT 
        [price] => 118.91
    )
    [1] => Array (
        [group] => FZSUV 
        [price] => 137.81
    )
    [2] => Array (
        [group] => MAXAT 
        [price] => 153.79
    )
)

I am just not sure how to change my foreach in a way that would change the output.

6
  • 1
    Change it to $group_price[] = array('group' => $tire->group, 'price' => $tire->price); inb4 incoming repster Commented Oct 21, 2013 at 19:55
  • 4 answers in less than 30 secounds, we good or what ;-) Commented Oct 21, 2013 at 19:56
  • 1
    It's a very basic beginner's question, Dagon. Easy points... Commented Oct 21, 2013 at 19:56
  • (array)$tire may also work :) Commented Oct 21, 2013 at 19:57
  • some of us don't do this for the points :-) Commented Oct 21, 2013 at 19:58

5 Answers 5

2

Like so:

foreach($results as $tire){
    $group_price[] = array(
        'group' => $tire->group,
        'price' => $tire->price
    );
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try:

foreach($results as $tire){
            $group_price[] = array('group' => $tire->group,'price' => $tire->price);
        }

Comments

0

Just like so:

foreach($results as $tire){
     $group_price[] = array(
                            'group' => $tire->group,
                            'price' => $tire->price
                           );
}

Add keys to your array elements, that's all.

Comments

0
foreach($results as $tire){
            $group_price[] = array('group'=>$tire->group, 'price'=>$tire->price);
        }

Comments

0

Try

array('group' => $tire->group, 'price' => $tire->price);

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.