0

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.

1
  • 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); Commented May 11, 2014 at 21:07

3 Answers 3

5

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

3 Comments

@ ? use isset($idss[$k]).
I love you! Thank you so much! If you have time could you explane what you did in the if clause? :)
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 :)
1

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.

Comments

0

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;
}

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.