0

My question is how would I take this array:

Array (
    [0] => stdClass Object (
        [item] => 0
        [size] => 2657017
        [group] => MAXAT
        [description] => 265/70R17 MAXTRAC A/T 115T 00K
        [sort4] => 115
        [sort5] => T
        [sort6] =>
        [price] => 118.91
    )
    [1] => stdClass Object (
        [item] => 8127
        [size] => 2657017
        [group] => FZSUV
        [description] => 265/70R17 FUZION SUV OWL 115T 50K
        [sort4] => 115
        [sort5] => T
        [sort6] =>
        [price] => 137.81
    )
    [2] => stdClass Object (
        [item] => 0
        [size] => 2657017
        [group] => MAXAT
        [description] => LT265/70R17 MAXTRAC A/T 118S E 00K
        [sort4] => 118
        [sort5] => S
        [sort6] => E
        [price] => 153.79
    )
    [3] => stdClass Object (
        [item] => 1237
        [size] => 2657017
        [group] => ATS
        [description] => 265/70R17 GEO AT-S OWL 113S 50K
        [sort4] => 113
        [sort5] => S
        [sort6] =>
        [price] => 167.15
    )
)

and turn it into this array (without running another query):

Array (
    [0] => stdClass Object (
        [group] => MAXAT
        [price] => 118.91
    )
    [1] => stdClass Object (
        [group] => FZSUV
        [price] => 137.81
    )
    [2] => stdClass Object (
        [group] => MAXAT
        [price] => 153.79
    )
    [3] => stdClass Object (
        [group] => ATS
        [price] => 167.15
    )
)

All that I am trying to achieve is to pull the group and price from the first array into a new array.

4
  • Can I ask why you need this data in a separate variable with data removed? Commented Oct 9, 2013 at 20:33
  • 1
    Please format that code... Commented Oct 9, 2013 at 20:33
  • A query is ran for everything in a database that changes often. Then selections are made which apply filters. Filters can be chosen by two different methods. This is the data after the query and filtering. Then some more information is pulled in based on that array to be displayed. I need to create a function for sorting before the loop to display the information. I could use the whole original array for the function, but I just thought I would make it easier with less information. Commented Oct 9, 2013 at 20:39
  • Not easier and more code and more processing. Commented Oct 9, 2013 at 20:42

2 Answers 2

2
$newEntries = array();
foreach ($originalEntries as $originalEntry) {
  $newEntry = new stdClass();
  $newEntry->group = $originalEntry->group;
  $newEntry->price = $originalEntry->price;
  $newEntries[] = $newEntry;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Could you use foreach to loop through the array and insert values into new array?!

$new_array = array();
$i = 0;
foreach($array as $k => $v){
    $new_array[$i]['group'] = $v['group'];
    $new_array[$i]['price'] = $v['price'];
    $i++;
}

1 Comment

Works but not the array of objects that they wanted and the $i is kind of redundant to $k and may change the indexes if the original didn't start with 0.

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.