0

I have an associative array stored in a variable like so

$result = Array ( 
[0] => stdClass Object ( [user_id] => 3 [zone_id] => -1 ) 
[1] => stdClass Object ( [user_id] => 3 [zone_id] => 0 ) 
[2] => stdClass Object ( [user_id] => 3 [zone_id] => 2 ) 
[3] => stdClass Object ( [user_id] => 3 [zone_id] => 3 ) 
[4] => stdClass Object ( [user_id] => 3 [zone_id] => 4 ) 
[5] => stdClass Object ( [user_id] => 4 [zone_id] => 0 ) 
[6] => stdClass Object ( [user_id] => 4 [zone_id] => 2 ) 
[7] => stdClass Object ( [user_id] => 4 [zone_id] => 4 ) 
[8] => stdClass Object ( [user_id] => 4 [zone_id] => 5 ) 
[9] => stdClass Object ( [user_id] => 4 [zone_id] => 7 )) 

I would like to convert this to an array that looks more like this

$newResult = array( 
[0] => stdClass Object ( [user_id] => 3 [zone_id] => "-1;0;1;2;3;4" )
[1] => stdClass Object ( [user_id] => 3 [zone_id] => "0;2;4;5;7" ))

I tried using this post as a reference, but ran into trouble along the way. When I simply replaced the variables with mine I received this error

[Thu Oct 09 15:21:29 2014] [error] [client xxx.xxx.xx.xx] PHP Fatal error:  Cannot use object of type stdClass as array in index.php on line 38

When I converted $n['zone_id'] to $n->zone_id There were zero errors, but the zone_id(s) seemed to cycle through and repeat forever.

The kicker is that I also need to ignore any digits outside of the 0-9 range (like -1 and 10 for instance).

Any insights into this? I feel like it should be much easier to solve!

Thanks.

Community
  • 1
  • 1
eju1
  • 3
  • 2
  • You have an array of objects, not just simple arrays. That is why you've got that error message. – Cheery Oct 09 '14 at 21:30

2 Answers2

0

It's untested but should work if I understand what you're trying to do

$new_array = [];

for($i = 0; $i < count($result); $i++) {
    if(!in_array($result[$i]->user_id, $new_array[$i])) {
        $new_array[$result[$i]->user_id] = [
            'user_id' => $result[$i]->user_id,
            'zone_id' => $result[$i]->zone_id
        ];
    } else {
        $new_array[$result[$i]['user_id']]['zone_id'] += ';' . $result[$i]['zone_id'];
    }
}
0

As I wrote in comments - you have array of objects, which is created by something like this (I'm writing a simple example by doing reverse - casting arrays into objects, but it is totally the same as yours, you can check it)

$result = array ( 
 (object) array( 'user_id' => 3, 'zone_id' => -1 ),
 (object) array( 'user_id' => 3, 'zone_id' => 0 ),
 (object) array( 'user_id' => 3, 'zone_id' => 2 ),
 (object) array( 'user_id' => 3, 'zone_id' => 3 ),
 (object) array( 'user_id' => 3, 'zone_id' => 4 ),
 (object) array( 'user_id' => 4, 'zone_id' => 0 ),
 (object) array( 'user_id' => 4, 'zone_id' => 2 ),
 (object) array( 'user_id' => 4, 'zone_id' => 4 ),
 (object) array( 'user_id' => 4, 'zone_id' => 5 ),
 (object) array( 'user_id' => 4, 'zone_id' => 7 ),
);

You have either to loop through the array and deal with each object separately (as with object) or in the loop you can cast it into array to work with it like with associative array (example below)

$out = array();
foreach($result as $o)
{
    $oArray = (array)$o; // cast to array
    if (!isset($out[$oArray['user_id']]))
       $out[$oArray['user_id']] = $oArray;
    else
       $out[$oArray['user_id']]['zone_id'] .= ';' . $oArray['zone_id']; 
}
var_dump(array_values($out)); // output and reset keys

The result is

array(2) {
  [0]=>
  array(2) {
    ["user_id"]=>
    int(3)
    ["zone_id"]=>
    string(8) "-1;0;2;3;4"
  }
  [1]=>
  array(2) {
    ["user_id"]=>
    int(4)
    ["zone_id"]=>
    string(11) "0;2;4;5;7"
  }
}

But if you want to loop through the objects, without casting each of them into array, then your code will look like

foreach($result as $o)
{
    if (!isset($out[$o->user_id]))
       $out[$o->user_id] = (array)$o;
    else
       $out[$o->user_id]['zone_id'] .= ';' . $o->zone_id; 
}
Cheery
  • 16,063
  • 42
  • 57