-1

I have a question about some JSON and PHP, see json below. How can I get the number string?

 {
   ”data”: [
   {
     ”numbers”: [
      {
        ”number”: "XXXX",
        ”type”: ””
      }
      ]
   }
  ]
 }

Ive tried with the following but its not working: $item['numbers'][0]->number;

Note, the $item array is ok. I just do not know what is wrong here.

EDIT:

I should point out that there was an json_decode before. So the structure looks like this:

 Array
 (
  [0] => Array
    (
        [number] => XXXX
        [type] => 
    )
 )

 print_r($item['numbers']);

This doesn't work: $item['numbers'][0]['number']; Error: Undefined offset: 0 in

Thanks!

Adrian McCool
  • 143
  • 2
  • 12

4 Answers4

1

To get the number try this :

$myJSON->data->numbers->number;
RockDaFox
  • 217
  • 2
  • 9
1

if you want to direct access then use this code

$items->data->numbers->number;

you can use php json_decode().it takes a JSON encoded string and converts it into a PHP variable.

<?php
$json_data = '{
   "data": [
   {
     "numbers": [
      {
        "number": "11",
        "Type": ""
      }
      ]
   }
  ]
 }';
$array=json_decode($json_data,true);
echo "<pre>";
print_r($array);
echo $array["data"][0]["numbers"][0]["number"];

then output

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [numbers] => Array
                        (
                            [0] => Array
                                (
                                    [number] => 11
                                    [Type] => 
                                )

                        )

                )

        )

)
Number:11
Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43
0

Use the json_decode() function of PHP.

$array = json_decode('{
   ”data”: [
   {
     ”numbers”: [
      {
        ”number”: "",
        ”Type”: ””
      }
      ]
   }
  ]
 }', true);

Then you could access it as $array["data"]["numbers"]["number"];

Nikko Madrelijos
  • 545
  • 3
  • 10
0

Hope this will help you out... You are trying to access which does not actually exists.

try this code snippet here

<?php

ini_set('display_errors', 1);
$month_options = '{
   "data": [
   {
     "numbers": [
      {
        "number": "10",
        "Type": ""
      }
      ]
   }
  ]
 }';
$array=json_decode($month_options,true);
echo $array["data"][0]["numbers"][0]["number"];
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42