I am new to JSON. I have a JSON String. I want to access the data and print it to a DIV which i get working like this:
document.getElementById("product_content").innerHTML=javascript_array[5].name;
The JSON looks like this:
$data[] = array(
"id" => $product->virtuemart_product_id,
"name" => $product->product_name
);
which has already been encoded and works.
How would i get a specific value using a id to retrieve the name eg:
document.getElementById("product_content").innerHTML=javascript_array["id" == 3].name;
instead of using the row number i say where "id" == 3 like "id" == 3
Thanks for the help
If you can rework the PHP to this you'll be in a better position
$data[$product->virtuemart_product_id] = array(
"name" => $product->product_name
);
This will set the ID as the key, rather than as additional element of the contained array. You should then be able to do
javascript_array[5]
If you are expecting strings too you should do
javascript_array["a_string"]
Or
javascript_array.a_string
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.