0

I'm using Codeigniter rest server to create an api. one of my clients is sending the following JSON array to my api

    {
     "code": "TEST",
     "store": "DBNG0024",
     "total": "50.00",
     "items": [{ "code":"121", "descr":"Pizza 1", "value":"50", "qty":"1",    "dept":"1"}]
    }

In the rest server documentation is says you can access the data like:

   function client_post()
   {
      $code = $this->post('code');
      $store_code = $this->post('store');
      $total = $this->post('total');

      $data = array('code' => $this->post('code'), 'store' => $this->post('store'), 'status' => 'invalid', 'value' => '0', 'message' => 'code is invalid');

      $this->response($data);
   } 

This works perfectly. Now the problem I'm having is that I cant access the multidimensional data "items": [{ "code":"121", "descr":"Pizza 1", "value":"50", "qty":"1", "dept":"1"}]

I have tried the following

    $items = json_decode($this->post(‘items’)); - Prints nothing
    $items = $this->post(‘items’); - prints the word Array

if I print the post data like this print_r($_POST,true); the below is what is printed

    Array ( [code] => 1234 [store] => 1234 [total] => 1234 [items] => Array )

Can anyone assist me with finding a way to access the $items array data

Thank you in advance

marc
  • 69
  • 1
  • 1
  • 6

3 Answers3

0

As you say you are getting items as Array on print, you can loop through it by accessing its key value pair

foreach($_POST['items'] as $key=>$value)
{
  echo 'Key =>'.$key.'|---| Value =>'.$value;
}

Hope this help,let me know if you are stuck somewhere.

Edited : Try this one i you might to json_decode your complete POSTED data..try below code

// $json will contain your posted data
$json = '{"code": "TEST","store": "DBNG0024","total": "50.00","items": [{ "code":"121", "descr":"Pizza 1", "value":"50", "qty":"1","dept":"1"}]}';
        $items = json_decode($json)->items;

        "qty"=>"1", "dept"=>"1"];
        foreach($items[0] as $key=>$value)
        {
            echo 'Key =>'.$key.':: Value =>'.$value;
            echo '<br/>';
        }
Zeeshan
  • 803
  • 6
  • 15
  • Hi, i did try this and I get the below error - Invalid argument supplied for foreach() – marc Apr 08 '16 at 14:28
  • How would I get the post data, its being sent to us by another company which is accessing our API Url – marc Apr 08 '16 at 15:06
  • If they are posting you can just use $this->post('json') – Zeeshan Apr 08 '16 at 15:07
  • I don't have this part - $json = '{"code": "TEST","store": "DBNG0024","total": "50.00","items": [{ "code":"121", "descr":"Pizza 1", "value":"50", "qty":"1","dept":"1"}]}'; its sent to the API url in JSON Array format – marc Apr 08 '16 at 15:08
  • as you told you have $_POST which is printing the data Then you can directly json_decode($_POST) and it should work – Zeeshan Apr 08 '16 at 15:09
  • i get this error - Invalid argument supplied for foreach() – marc Apr 08 '16 at 15:11
  • what does this gives you print_r($_POST['items']) – Zeeshan Apr 08 '16 at 15:18
  • i have a feeling its an issue with the codeigniter-restserver that I'm using. the json data is decoded by the rest controller and than I access the data using $_POST['code']...... I'm thinking that the restserver controll does not handle multidimensional json arrays... is this possibly and how would it be corrected... or I'm I completely wrong here – marc Apr 08 '16 at 15:18
  • print_r($_POST['items']) - returns the word Array – marc Apr 08 '16 at 15:19
  • print_r(json_decode($_POST['items'])) ?? Check this – Zeeshan Apr 08 '16 at 15:21
  • this returned nothing – marc Apr 08 '16 at 15:33
  • Then i guess your items variable has nothing posted...Kindly check if everything is posted correctly to you – Zeeshan Apr 08 '16 at 15:37
  • why would the items be an issue but I can access the code, store and totals fine..... you don't think it could have some to do with the restserver I'm using.... here is a link to it if that helps - https://github.com/chriskacerguis/codeigniter-restserver – marc Apr 08 '16 at 16:59
  • Those are giving you values because they have some values set. If $this->post('items') prints nothing to you then it has nothing to print. Also it has nothing to do with rest server you are using – Zeeshan Apr 09 '16 at 09:32
  • that's my problem, I have confirmed that items does have values. – marc Apr 09 '16 at 11:45
  • thanks anyways Zeeshan, will keep looking for a solution – marc Apr 09 '16 at 11:46
0

Usually, after decode JSON, it silently becames an sibling of stdClass. If you are able to retrieve the values code, store_code and total as an $_POST, than you must be able to retrieve items as well.

$code  = $this->post('code');
$items = json_decode($this->post('items'));

foreach($items as $item)
   echo $item->value;
Linesofcode
  • 5,327
  • 13
  • 62
  • 116
0

I think $items = $this->post(‘items’); is the right way to access "items", kindly check this link Send a raw json value to rest controller. Maybe you are printing the array in the wrong way, please check this link too The word “Array” gets printed instead of the values of the rows

Community
  • 1
  • 1
Amal
  • 25
  • 7