-1

I tried to change an object into array but seems like it doesn't work this way. Could someone tell me what to do in this case?

$blockedNumber = DB::select('SELECT COUNT(*) AS number FROM users_blocked WHERE (f_block = ? OR f_chat_block = ? OR f_hide_posts = ?)', array(1, 1, 1));
$number = (array) $blockedNumber;
var_dump($number[0]['number']);

var_dump shows me "Cannot use object of type stdClass as array"

Helloitsme
  • 23
  • 6
  • Can you `print_r($blockedNumber)` and show me the output. – Parthapratim Neog Oct 09 '15 at 10:58
  • @ParthapratimNeog Array ( [0] => stdClass Object ( [number] => 6 ) ) – Helloitsme Oct 09 '15 at 10:59
  • @b0s3 Idiots like me cannot use objects xD – Helloitsme Oct 09 '15 at 11:00
  • var_dump($number[0]->number); – statelessMind Oct 09 '15 at 11:01
  • I thought this operation changes an object into array, thats why im asking about it – Helloitsme Oct 09 '15 at 11:03
  • right after the DB::select what does `print_r($blockedNumber);` print? The cast to array is possibly superfluous because DB::select might already return an array; an array of objects... – VolkerK Oct 09 '15 at 11:03
  • 1
    casting it with (array) will not do a "deep" / recursive change. You could json_encode / json_decode to do a recursive change to array. $array = json_decode(json_encode($objectOrArrayOfObjects), true); – JimL Oct 09 '15 at 11:04
  • Possible duplicate of [stdClass to array?](http://stackoverflow.com/questions/11396033/stdclass-to-array) – Mihai Matei Oct 09 '15 at 11:06
  • 1
    Before using json_* you might want to tell us first what DB::select actually is and what it returns. It's rather unlikely that there's a deep nesting of objects involved here. The return value is likely a one-dimensional array of objects, each having just scalar properties and json_encode/decode would just be overkill. – VolkerK Oct 09 '15 at 11:07
  • @VolkerK oh well, you are right. This looks like an array of objects. I have difficulties to deal with such a things – Helloitsme Oct 09 '15 at 11:07
  • Depending on what DB::select actually is/does you might persuade it to return an array of arrays. That would be better than to convert the result in a second step, I guess. SO, what is DB::select? (and btw: to convert your 1-d array: `$numbers = array_map(function($e) { return (array)$e; }, $blockedNumber);` should do the trick ....but's it's rather suboptimal if you can bring DB::Select to return the result in a different format). – VolkerK Oct 09 '15 at 11:10

2 Answers2

0
 $obj = new stdClass();
 $objB = new stdClass();
 $objB->deepFoo = 'bar';
 $obj->bla = array(1,2,3,$objB);
 $obj->blub = new stdClass();
 $obj->blub->foo = 'bar';

function rekursiveObjectToArray($obj){
  if(is_object($obj)){
    $obj = (array)$obj;
  }
  if(is_array($obj)){
   $obj = array_map('rekursiveObjectToArray',$obj);
  }
  return $obj;
}
$obj = rekursiveObjectToArray($obj);

var_dump($obj);
0

From your reply, you can do this, and it will solve it.

$blockedNumber = DB::select('SELECT COUNT(*) AS number FROM users_blocked WHERE (f_block = ? OR f_chat_block = ? OR f_hide_posts = ?)', array(1, 1, 1));
$number = (array) $blockedNumber[0];
var_dump($number['number']);

Try it out. If you need explanation let me know, I'll explain the reason. But, first check if its working.

Parthapratim Neog
  • 4,352
  • 6
  • 43
  • 79