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

3 Answers 3

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/>';
        }
Sign up to request clarification or add additional context in comments.

16 Comments

Hi, i did try this and I get the below error - Invalid argument supplied for foreach()
How would I get the post data, its being sent to us by another company which is accessing our API Url
If they are posting you can just use $this->post('json')
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
as you told you have $_POST which is printing the data Then you can directly json_decode($_POST) and it should work
|
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;

Comments

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

1 Comment

thank you for your comment, will check out the links.

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.