-2

I have result

{"success":true,"data":{"id":6583879,"listingId":"11745/3470/OMS"}}

I need to explode it on two arrays:

$id = 6583879;

and

$listid = 11745/3470/OMS

I'd like to avoid counting characters, this response may be another in future. I was thinking about taking:

  • everything between "id": and comma
  • everything between "listingId":" and "
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42

3 Answers3

0

just decode the json data

$data = '{"success":true,"data":[{"id":6583879,"listingId":"11745/3470/OMS"}]}';

$arr = json_decode($data);
$id = array();
$listing = array();


for($i=0; $i < count($arr->data); $i++){
    $id[$i] = $arr->data[$i]->id;
    $listing[$i] = $arr->data[$i]->listingId;
}

//loop array to view data
foreach($id as $value){
   echo $value.'<br>';
}

?>
Chinito
  • 1,085
  • 1
  • 9
  • 11
0

PHP code demo

<?php
$json='{"success":true,"data":{"id":6583879,"listingId":"11745/3470/OMS"}}';
$array=  json_decode($json,true);
extract($array["data"]);
$first=array("id"=>$id);
$second=array("listingId"=>$listingId);
print_r($first);
print_r($second);

Output:

Array
(
    [id] => 6583879
)
Array
(
    [listingId] => 11745/3470/OMS
)
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
0
<?php
$json='{"success":true,"data":{"id":6583879,"listingId":"11745/3470/OMS"}}';
$decoded = json_decode($json);
$id = $decoded['data']['id'];
$listingId = $decoded['data']['listingId'];
Carlos Gonzalez
  • 389
  • 1
  • 11