Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i want to convert a stdClass object to string and reduce an array with the max value from the stdClass object.

This is my array:

Array
(

[135] => Array
    (
        [0] => stdClass Object
            (
                [ID] => 145
            )

        [1] => stdClass Object
            (
                [ID] => 138
            )

        [2] => stdClass Object
            (
                [ID] => 139
            )

    )

[140] => Array
    (
        [0] => stdClass Object
            (
                [ID] => 163
            )

        [1] => stdClass Object
            (
                [ID] => 155
            )
)

Basically it should look like this:

 Array
 (
 [135] => 139
 [140] => 164
 )

Is this possible? I've tried various foreach loops but i don't get it with the stdClass object...

My try so far:

 foreach($ids as $k => $v) {
   for($i = 0; $i < count($v); $i++) {
        $idss[$i] = array()$v;
    }
 }

That doesn't work.

Thanks alot in advance! You would be my life saver today.

Best regards

share|improve this question
    
You got a sort of multidimensional array and you want a normal array? I don't see the "logic" behind the conversion. Please elaborate. One easy way to get rid of "objects" is to encode it using json_encode() and then decode it back with the second parameter set to true: $output = json_decode(json_encode($input), true); –  HamZa May 11 at 21:07

3 Answers 3

up vote 5 down vote accepted

This will solve your purpose. let me know if anything goes wrong.

$ids[135][0]->ID = 145;
$ids[135][1]->ID = 135;
$ids[135][2]->ID = 155;
$ids[140][0]->ID = 125;
$ids[140][1]->ID = 135;
$idss = array();
foreach($ids as $k => $v) {
   for($i = 0; $i < count($v); $i++) {
        if(!@$idss[$k] || $v[$i]->ID > $idss[$k])
        {
            $idss[$k] = $v[$i]->ID;
        }
    }
 }
 echo "<Pre>";
 print_r($idss);
 die;
share|improve this answer
1  
@ ? use isset($idss[$k]). –  Guilherme Nascimento May 11 at 21:25
    
I love you! Thank you so much! If you have time could you explane what you did in the if clause? :) –  Pascal Cloverfield May 11 at 21:26
1  
Welcome.And sure! $k has 135 value for first iteration. so i checked for $idss[$k] exists or not if it exists then condition will be false and it checks for second condition. which is $v[$i]->ID > $idss[$k] means it checks that new value in iteration is larger than existing value in $idss array then it replaces it with new value. I hope you understands. thats what i can do in text message :) –  Razor Jack May 11 at 21:34

Already answered but here is a shorter version of this

$final_array =array();
foreach($arr as $key=>$val){
  $max = max(array_keys($val));
  $final_array[$key] = $val[$max]->ID ;
}

print_r($final_array);

Here $arr is your input array.

share|improve this answer

You need to do some compariosn in your inner for loop to know which one holds the max value. Here is an example :

$new_arr = array();
foreach($elements as $index => $value){
  $max = -1;
  $foreach($value as $obj){
    if($obj->id > $max)
      $max = $obj->id;
  }
  $new_arr [$index] = $max;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.