0

I have a JSON with the Following structure.

{

 "1":{"Itemname":"dtfg","unitprice":"12","Qty":"4","price":"$48.00"},
 "2":{"Itemname":"kjh","unitprice":"45","Qty":"7","price":"$315.00"},
 "3":{"Itemname":"yjk","unitprice":"76","Qty":"8","price":"$608.00"},
 "4":{"Itemname":"hgj","unitprice":"4","Qty":"45","price":"$180.00"}

}

I need the Itemname to be made into a PHP array, Unitprice into another one, Qty to another one and price to another one. How do I do that?

Ashif Shereef
  • 454
  • 1
  • 8
  • 24

6 Answers6

1
$getArray   = get_object_vars(json_decode($json));
print_r($getArray);
echo $getArray[1]->Itemname;
echo $getArray[1]->unitprice;

you require get_object_vars as well for achieving your requirement.

ameenulla0007
  • 2,663
  • 1
  • 12
  • 15
1
<?php
$json =<<<JSONLIVES
{
     "1":{"Itemname":"dtfg","unitprice":"12","Qty":"4","price":"$48.00"},
     "2":{"Itemname":"kjh","unitprice":"45","Qty":"7","price":"$315.00"},
     "3":{"Itemname":"yjk","unitprice":"76","Qty":"8","price":"$608.00"},
     "4":{"Itemname":"hgj","unitprice":"4","Qty":"45","price":"$180.00"}
}
JSONLIVES;


$items = json_decode($json, TRUE);

$item_names = array();
foreach($items as $key => $item) {
    $item_names[] = $item['Itemname'];
}

Or Php >= 5.5

print_r(array_column($items, 'Itemname'));
Progrock
  • 7,373
  • 1
  • 19
  • 25
0

You need a function called json_decode() to convert your json data into PHP array

$json =  {

 "1":{"Itemname":"dtfg","unitprice":"12","Qty":"4","price":"$48.00"},
 "2":{"Itemname":"kjh","unitprice":"45","Qty":"7","price":"$315.00"},
 "3":{"Itemname":"yjk","unitprice":"76","Qty":"8","price":"$608.00"},
 "4":{"Itemname":"hgj","unitprice":"4","Qty":"45","price":"$180.00"}

};
var_dump(json_decode($json));
Little Phild
  • 785
  • 1
  • 6
  • 17
0

You need to decode your Json by PHP's json_decode()

$decodeJson will return object then you can read Itemname and other values by using $val->Itemname in foreach loop

$json =  '{

 "1":{"Itemname":"dtfg","unitprice":"12","Qty":"4","price":"$48.00"},
 "2":{"Itemname":"kjh","unitprice":"45","Qty":"7","price":"$315.00"},
 "3":{"Itemname":"yjk","unitprice":"76","Qty":"8","price":"$608.00"},
 "4":{"Itemname":"hgj","unitprice":"4","Qty":"45","price":"$180.00"}

}';

$decodeJson = json_decode($json);

foreach($decodeJson as $key=>$val) {

      print_r($val);
}

Live Json decode

Noman
  • 4,088
  • 1
  • 21
  • 36
0

Try that:

$json = json_decode($json);

foreach($json as $obj){
   echo $obj->name;
   .....

}
Jsparo30
  • 405
  • 4
  • 10
  • 30
  • While this code fragment may answer the OP's question, this answer would be much more useful for future visitors if you explain why it solves the problem. – Gert Arnold Jan 26 '16 at 23:28
0

After some research, I found out that the most efficientt way that solves my problem here would be to do like the following.

$cash=json_decode($new_json, true);

echo $arr3[1]['Itemname'];
echo $arr3[1]['unitprice'];
:
:

and so on.

This can be put into loops easily, fetched into HTML text-fields (as I want here in this scenario) and so on.

Ashif Shereef
  • 454
  • 1
  • 8
  • 24