-3
{"2b44928ae11fb9384c4cf38708677c48":{
    "id":"115",
    "qty":3,
    "option":"{\"color\":{\"title\":\"Color\",
                          \"value\":\"\"
                         }
              }",
    "price":150,
    "name":"Nightwear",
    "shipping":"5",
    "tax":3,
    "image":"http:\/\/localhost\/plus\/uploads\/product_image\/product_115_1_thumb.jpg",
    "coupon":"",
    "rowid":"2b44928ae11fb9384c4cf38708677c48",
    "subtotal":450
    }
}

Hello everyone, This is my array and I want to echo value of only "id" i.e. I want to get value as '115' of key- "id". Please guide me how to make a foreach for this one? I have tried lots of variations but none worked :(

TIA :)

UPDATE- I have tried this but did not get any result:

foreach($res as $k=>$t)
{
  echo $t["product_details"]["id"]; 
}
user10089632
  • 5,216
  • 1
  • 26
  • 34
user2452
  • 13
  • 5
  • 1
    Why don't you put the code in of what you think your best variation is and then we'll help you out. – Difster Jul 22 '17 at 23:41
  • You say `foreach`, but the code you post is a JSON for 1 object. – Accountant م Jul 22 '17 at 23:43
  • Hello, I tried json_decode n this but its still not working. Can anyone please give me a working code? Its really urgent – user2452 Jul 22 '17 at 23:46
  • @user2452 `json_decode` [worked with me](https://eval.in/836362) . The string you posted is a valid JSON. – Accountant م Jul 22 '17 at 23:49
  • @Accountantم Can you please provide me with the working code sir? – user2452 Jul 22 '17 at 23:51
  • 2
    Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – user3942918 Jul 22 '17 at 23:54
  • The code you posted seems to be trying to get the ID of productdetails which is nowhere in the data you showed us. – takendarkk Jul 23 '17 at 00:10
  • @user2452 You have to put a reliable example of your array. You put here an object , and I'm sure you have an array , so every answer you are going to get will work on that format of the object in the question but none of them will work with your real array you have. – Accountant م Jul 23 '17 at 00:14

3 Answers3

3

Before you can use the JSON as an array you need to convert it first. use json_decode() for that.

<?php 
$json='{"2b44928ae11fb9384c4cf38708677c48":{"id":"115","qty":3,"option":"{\"color\":{\"title\":\"Color\",\"value\":\"\"}}","price":150,"name":"Nightwear","shipping":"5","tax":3,"image":"http:\/\/localhost\/plus\/uploads\/product_image\/product_115_1_thumb.jpg","coupon":"","rowid":"2b44928ae11fb9384c4cf38708677c48","subtotal":450}}';

$array = json_decode($json, true);

foreach($array as $key=>$value){
    echo $value['id'];
}
?>
Patrick Simard
  • 2,294
  • 3
  • 24
  • 38
  • Thank you so much :) Highly appreciated. – user2452 Jul 22 '17 at 23:58
  • The problem with this code is it's is looping in the outer object **properties**. And lucky you are the outer object has only 1 property, that is `2b44928ae11fb9384c4cf38708677c48`and this property is an object too that has the rest of the other data. Your code will not work if this object is an element in an array. – Accountant م Jul 23 '17 at 00:33
  • BTW, this is not your fault, this is the OP fault because he didn't provide a reliable example of the array, your code answers the question 100% – Accountant م Jul 23 '17 at 00:36
  • 1
    Yeah I was gona say that lol Anyway, he looks happy with it ;-) – Patrick Simard Jul 23 '17 at 00:38
0

Assuming you have an array of objects like you provided in your post, I have put your object in an array for testing

<?php
$json = '[{"2b44928ae11fb9384c4cf38708677c48":{"id":"115","qty":3,"option":"{\"color\":{\"title\":\"Color\",\"value\":\"\"}}","price":150,"name":"Nightwear","shipping":"5","tax":3,"image":"http:\/\/localhost\/plus\/uploads\/product_image\/product_115_1_thumb.jpg","coupon":"","rowid":"2b44928ae11fb9384c4cf38708677c48","subtotal":450}},'.
         '{"2b44928ae11fb9384c4cf38708677c48":{"id":"116","qty":3,"option":"{\"color\":{\"title\":\"Color\",\"value\":\"\"}}","price":150,"name":"Nightwear","shipping":"5","tax":3,"image":"http:\/\/localhost\/plus\/uploads\/product_image\/product_115_1_thumb.jpg","coupon":"","rowid":"2b44928ae11fb9384c4cf38708677c48","subtotal":450}}]';
$json = json_decode($json);
foreach ($json as $object){
    $propsArray = get_object_vars($object);
    reset($propsArray);
    echo $object->{key($propsArray)}->id . "<br>\n";
}
exit;

this outputs

115
116

try a live demo (https://eval.in/836364)

Accountant م
  • 6,975
  • 3
  • 41
  • 61
0
$json='{"2b44928ae11fb9384c4cf38708677c48":{"id":"115","qty":3,"option":"{\"color\":{\"title\":\"Color\",\"value\":\"\"}}","price":150,"name":"Nightwear","shipping":"5","tax":3,"image":"http:\/\/localhost\/plus\/uploads\/product_image\/product_115_1_thumb.jpg","coupon":"","rowid":"2b44928ae11fb9384c4cf38708677c48","subtotal":450}}';

$array = json_decode($json, true); // convert json string to array

$result = array_column($array, 'id'); // find matching array key and return values in array

foreach ($result as $value) { // echo each value with foreach loop
    echo $id . '<br>';
}
Michael Eugene Yuen
  • 2,470
  • 2
  • 17
  • 19