CodeIgniter Forums

Full Version: Restfull JSON Array Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
Please can someone give me some advice, I have setup an API which is working but cant seem to get some of the json data.
this is the data being posted(cant be changed coming from out side source)
{
"code": "VET002",
"store": "DBNG0024",
"items": [
{
"code": "1",
"descr": "Yoghurt",
"value": "66",
"qty": "3",
"dept": "1"
},
{
"code": "25",
"descr": "Red Bull",
"value": "44",
"qty": "2",
"dept": "2"
},
{
"code": "23",
"descr": "Lipton Lemon",
"value": "72",
"qty": "6",
"dept": "2"
}
]
}
this is how I'm reading the data
$code = $this->post('code');
$store_code = $this->post('store');
$total = $this->post('total');
how would i read the items values.
Thank you
(04-07-2016, 12:03 AM)Try Wrote: [ -> ]
PHP Code:
$input $_REQUEST['your_request_data_name']; //Set this in your javascript (ajax request)
$data json_decode($inputtrue);

$code $data->code;
$store $data->store

"total" is not defined in your JSON.
(04-07-2016, 02:44 AM)EricP Wrote: [ -> ]
(04-07-2016, 12:03 AM)Try Wrote: [ -> ]
PHP Code:
$input $_REQUEST['your_request_data_name']; //Set this in your javascript (ajax request)
$data json_decode($inputtrue);

$code $data->code;
$store $data->store

"total" is not defined in your JSON.

thanks for your reply sorry but I'm still bit confused about this part
PHP Code:
$input $_REQUEST['your_request_data_name']; //Set this in your javascript (ajax request) 

the data is coming from an external source that is connecting to out api. the api is working I just not sure how to access the items array.

when I echo items all it prints is the word "Array"
You can't "echo" an array. Language construct "echo" is used to output one or more strings.
Use print_r instead and you could see what is in array.
(04-07-2016, 05:11 PM)Tpojka Wrote: [ -> ]You can't "echo" an array. Language construct "echo" is used to output one or more strings.
Use print_r instead and you could see what is in array.

Hi Tpojka,

Sorry it I used the wrong term i dont mean echo, I'm just trying to figure out how to get the values within the "items" array.

I can get the code and the store, but when I try get "items" all i get is the word "Array"(when using print_r)

Hope I have explained myself correctly this time. please bare with me I'm new to all this.
Hope this helps.

PHP Code:
$json '
{
    "code": "VET002",
    "store": "DBNG0024",
    "items": [{
        "code": "1",
        "descr": "Yoghurt",
        "value": "66",
        "qty": "3",
        "dept": "1"
    }, {
        "code": "25",
        "descr": "Red Bull",
        "value": "44",
        "qty": "2",
        "dept": "2"
    }, {
        "code": "23",
        "descr": "Lipton Lemon",
        "value": "72",
        "qty": "6",
        "dept": "2"
    }]
}
'
;

$arr json_decode($jsontrue);
$total 0;

?>

<p>Store: <?php echo $arr['store']; ?></p>
<p>Code: <?php echo $arr['code']; ?></p>
<p>Items:</p>
<ul>
    <?php foreach($arr['items'] as $arr2): ?>
        <li>
            <?php foreach($arr2 as $itemKey => $itemValue): ?>
                <?php echo '<b>'.$itemKey.'</b>: '.$itemValue?>
            <?php endforeach; ?>
        </li>
        <?php $total += $arr2['qty']; ?>
    <?php endforeach; ?>
</ul>
<p>Total Qty: <?php echo $total?></p> 
(04-08-2016, 01:17 AM)paulkd Wrote: [ -> ]Hope this helps.

PHP Code:
$json '
{
    "code": "VET002",
    "store": "DBNG0024",
    "items": [{
        "code": "1",
        "descr": "Yoghurt",
        "value": "66",
        "qty": "3",
        "dept": "1"
    }, {
        "code": "25",
        "descr": "Red Bull",
        "value": "44",
        "qty": "2",
        "dept": "2"
    }, {
        "code": "23",
        "descr": "Lipton Lemon",
        "value": "72",
        "qty": "6",
        "dept": "2"
    }]
}
'
;

$arr json_decode($jsontrue);
$total 0;

?>

<p>Store: <?php echo $arr['store']; ?></p>
<p>Code: <?php echo $arr['code']; ?></p>
<p>Items:</p>
<ul>
    <?php foreach($arr['items'] as $arr2): ?>
        <li>
            <?php foreach($arr2 as $itemKey => $itemValue): ?>
                <?php echo '<b>'.$itemKey.'</b>: '.$itemValue?>
            <?php endforeach; ?>
        </li>
        <?php $total += $arr2['qty']; ?>
    <?php endforeach; ?>
</ul>
<p>Total Qty: <?php echo $total?></p> 

that does help a lot problem is that I dont have the json data its coming from an external source. so not sure what I would replace $json with.

might be helpful to mention im using rest server.

thanks again really appreciate the help
Is the "external source" available publicly or is it private?
(04-08-2016, 02:11 AM)paulkd Wrote: [ -> ]Is the "external source" available publicly or is it private?

its private... basically a another company sends data in json array format to our api url