0

I'm trying to loop through a JSON that I post to my PHP backend. The JSON looks like this:

[
    {
        "number":"5613106"
    },
    {
        "number":"56131064" 
    },
    {
        "number":"56131063" 
    }
]

I post it from Postman like so:

enter image description here

I want to be able to print each number that I've posted individually using

echo $number;

Right now when I use the following it just prints the last number:

$number = $json['number'];
echo $number;

My function:

public function check_users_post()
{
    $json = $this->request->body;
    print_r($json);
    $this->response($json, REST_Controller::HTTP_OK);
}

The output:

Array
(
    [0] => Array
        (
            [number] => 5613106
        )

    [1] => Array
        (
            [number] => 56131064
        )

    [2] => Array
        (
            [number] => 56131063
        )

)
Rutger Huijsmans
  • 2,330
  • 2
  • 30
  • 67

4 Answers4

2

Hope this generic iterator will help you.

$jsonIterator = new RecursiveIteratorIterator(
        new RecursiveArrayIterator(json_decode($json, TRUE)),
        RecursiveIteratorIterator::SELF_FIRST);


foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key => $val\n";
    }
}
Rohith K N
  • 845
  • 6
  • 17
1

Use

json_decode()

PHP built in function for decoding json back to PHP variable or array and then loop through each index and print number.

Sagar
  • 642
  • 3
  • 14
  • When I try json_decode($json) I receive: Message: json_decode() expects parameter 1 to be string, array given. Am I not sending out a json through Postman already? – Rutger Huijsmans Oct 03 '16 at 04:52
  • @RutgerHuijsmans if you are doing that, you are already reading the JSON decoded, so post your complete code, and do `print_r($json);` to see what is inside `$json` because it seems is already an array... – matiaslauriti Oct 03 '16 at 04:55
  • @P0lT10n added my complete function – Rutger Huijsmans Oct 03 '16 at 04:59
  • 1
    Ok. Here is tric: Please do not use json_decode(), just loop through array and access elements by using index. foreach($json as $item=> $val) { echo $val; } – Sagar Oct 03 '16 at 05:04
1

Got it done like this:

public function check_users_post()
{
    $json = $this->request->body;

    $jsonIterator = new RecursiveIteratorIterator(
        new RecursiveArrayIterator($json),
        RecursiveIteratorIterator::SELF_FIRST);

    $phone_numbers = "";

    foreach ($jsonIterator as $key => $val) {
        if(is_array($val)) {

        } else {
            $phone_numbers = "$phone_numbers" . ", " . "$val";
        }
    }

    $phone_numbers = substr($phone_numbers, 2);
    $phone_numbers = "(" . $phone_numbers . ")";

    $query = $this->db->query("SELECT * FROM users WHERE user_number in $phone_numbers;");
    $result = $query->result();
    $this->response($result, REST_Controller::HTTP_OK);

    if (mysql_num_rows($result)==0) {
        $data = [ 'message' => 'No users returned'];
        $this->response($data, REST_Controller::HTTP_BAD_REQUEST);
    } else {
        $this->response($result, REST_Controller::HTTP_OK);
    }
}
Rutger Huijsmans
  • 2,330
  • 2
  • 30
  • 67
0

Description

First stringify your JSON, than use JSON_DECODE() function which gives you the array afterwards iterate on this array using foreach loop which will give you the each key which you want to echo.

Code

             $str = '[
                  {
                   "number":"5613106"
                  },
                  {
                   "number":"56131064"  
                  },
                  {
                   "number":"56131063"  
                  }
               ]';
            $array = json_decode($str, true);
            echo "<pre>";
            foreach ($array as $key => $key_value) { // then loop through it
                echo "<br>";
                echo $array[$key]['number'];
                echo "<br>";
           }

Output

5613106

56131064

56131063
Ahmer Saeed
  • 576
  • 5
  • 13