• 7
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Get a PHP object property that is a number [duplicate]

Another possibility is to use the 2nd parameter to json_decode:

$obj = json_decode(str, true);

You get an array instead of a PHP object, which you can then index as usual:

$obj['content'][5]
  • 18
Reply Report

JSON encode, and then decode your object passing true as the second param in the decode function. This will return an associative array.

$array = json_decode(json_encode($object), true);

Now you can use your new array

echo $array['content']['5'];

Using $object->content->{'5'} will not work if the object was created by casting an array to an object.

A more detailed description can be found here: https://stackoverflow.com/a/10333200/58795

  • 1
Reply Report

Another aproach is casting the object to array.

$array = (array) $object;
$array['content'][5];
  • 0
Reply Report
    • This is incorrect. You've only casted the $object object to an array. The $content property is still an object. You would need to also add this: $array['content'] = (array) $array['content'];. That's why the JSON encode/decode solution is simpler. It converts all of the children objects too.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions