ctarimli
ctarimli

Reputation: 322

how to get the wanted value from json with php?

I have an array like this:

$cookie_array = array( 'username' => $row['username'], 'name' => $row['name'], ... );
$encodedcookie = json_encode($cookie_array);

And I set this as cookie :

setcookie("cookiedata", $encodedcookie, time()+150, '/');

I decoded it like that:

if (isset($_COOKIE['cookiedata'])){
$decodedcookie = json_decode($_COOKIE['cookiedata'], true);

Now what I wanted to do is to get the 'nth' value from this array. ( n = first or second or third... So what I want to explain with 'nth' value is ; first value or second value ...)

For example I want to get the value and define it like this:

$blabla = (second value of the array) ;

But I dont know how to do that and I didn't find it even in http://www.php.net/ or I couldn't.

Upvotes: 0

Views: 72

Answers (2)

kafsoksilo
kafsoksilo

Reputation: 259

You can use foreach to loop through array elements:

$count = 0;
foreach ($decodedcookie AS $key => $value)
    echo ($count++) . ': ' . $key . ' => ' . $value;

Or you can use array_values:

$values = array_values($decodedcookie);

echo $values[0];
echo $values[1];

Upvotes: 1

AbraCadaver
AbraCadaver

Reputation: 79024

$blabla = $decodedcookie['username']; //etc

Upvotes: 1

Related Questions