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?

4

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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'));

Comments

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));

Comments

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

Comments

0

Try that:

$json = json_decode($json);

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

}

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.