How to pull strings out of JSON object

Here is the code

$jsonString = urldecode($json);
$jsonString = str_replace("\\", "", $jsonString);
$data = JSON_decode($jsonString);
mysql_select_db($database) or die('Cannot connect to database.');


  stdClass Object ( [myObject] => Array ( [0] => stdClass Object ( [fullname] => abc [role] => ADMIN [username] => xyz ) ) ) 

my question how can i pull individual values and insert them into the database fullname, role and username


By default json_decode turns a JSON object into a PHP object, so you'll need to access it like:


When storing your data in a mysql query:

$query = "INSERT INTO table SET fullname = '{$data->myObject[0]->fullname}'";

If you prefer to use an array instead of an object, you can use:

$data = json_decode($jsonString, true); //notice the 2nd parameter

That way you can access your variables with an array like the two other posters described.


You don't really need to use a for loop, you would just set:

$data = json_decode($jsonString, true);
$users = $data['myObject'];

Since you already have a perfectly valid array in $data['myObject'].

Need Your Help

Yii multiple related model

php yii relationship

I need to make the ability to add a lot of addresses on the registration form. I used this example, but it displays an error:

Selenium: Can I set any of the attribute value of a WebElement in Selenium?

testing selenium

I have a WebElement, I want to reset its an attribute's value to some other value (for e.g. attr is the attribute, and I want to change its original value=1 to new value=10).