Mohan
Mohan

Reputation: 289

Json decode is not working?

Actually I want convert json array to string for that I used json_decode but it is returning nothing How to solve this?

Below is my array,

[{"Product":"Fantasy Brown","productimage":"images/fantasy-brown.jpg"},{"Product":"Bruno White","productimage":"images/bruno-white.jpg"},{"Product":"Barcunda Black","productimage":"images/barcunda-black.jpg"},{"Product":"Iceberg","productimage":"images/iceberg.jpg"},{"Product":"Mercury White","productimage":"images/mercury-white.jpg"},{"Product":"Desert Brown","productimage":"images/desert-brown.jpg"},{"Product":"Blue Venatino","productimage":"images/blue-venatino-marble.jpg"}]

the above array should be converted to string and also I want display product and productimage in string format from that array.

Below is my code,

$cart_items = "<script>document.write(localStorage.getItem('cart'));</script>";
echo "<pre>";
print_r($cart_items);
die;
$details = json_decode($cart_items);
// $x = $cart_items[];
echo $details;die;

Upvotes: 0

Views: 299

Answers (2)

Siddhesh
Siddhesh

Reputation: 1173

you all need to set json_decode() 'true' i.e

$details = json_decode($cart_items,true);

Upvotes: 0

Koala Yeung
Koala Yeung

Reputation: 7843

You code wouldn't work. You're mixing client side and server side programming in a wrong way.

From the server point of view, $cart_items is only a string containing:

<script>document.write(localStorage.getItem('cart'));</script>

Nothing more.

It is your browser that parse the server output, i.e. the Javascript, to the JSON string. Since the conversion only happens on browser side, the server side (i.e. your PHP script) doesn't get it.

You need to reconsider your code logic. Maybe you need to have javascript that submit the localStorage content to server. Or maybe have your problem solved only with Javascript.

Upvotes: 1

Related Questions