Home > Back-end >  get all similar keys in JSON object in PHP
get all similar keys in JSON object in PHP

Time:03-08

let say, i have this JSON data i want to print all key by name "id", what should i do?

$json = {"response":[
  {
    "id": 37,
    "slug": "red",        
    "stock": true,
    "name": "Red",
    "default": 0,
    "sizes": "38"
  },
  {
    "id": 38,
    "slug": "red",        
    "stock": true,
    "name": "Red",
    "default": 0,
    "sizes": "40"
  }
]}

i tried this but this one is only giving me first array object sub key(which is id=37) i want all like (id=37, id=38)

$op =  $json->{'response'}[0]->{'id'};

CodePudding user response:

You can loop over your response

        $array = [];
        
        foreach ($json->{'response'} as $reponseItem){
            $array[$reponseItem['id']] = $reponseItem;
        }

CodePudding user response:

If I assume your JSON is really JSON, and therefore a string, you could do this:

$json = '{"response":[
  {
    "id": 37,
    "slug": "red",        
    "stock": true,
    "name": "Red",
    "default": 0,
    "sizes": "38"
  },
  {
    "id": 38,
    "slug": "red",        
    "stock": true,
    "name": "Red",
    "default": 0,
    "sizes": "40"
  }
]}';

$data = json_decode($json);

$ids = array_column($data->response, 'id');

print_r($ids);

This would result in:

Array
(
    [0] => 37
    [1] => 38
)

See a PHP Fiddle.

You can turn the JSON string into a usable PHP data with json_decode().

The way to get the id's is by using array_column().

CodePudding user response:

You need to decode your json string into array 1st and then loop through data.

$json = ' {"response":[
  {
    "id": 37,
    "slug": "red",        
    "stock": true,
    "name": "Red",
    "default": 0,
    "sizes": "38"
  },
  {
    "id": 38,
    "slug": "red",        
    "stock": true,
    "name": "Red",
    "default": 0,
    "sizes": "40"
  }
]}';

$jsonArray =json_decode($json,true); //convert your json string to array.
$newJson  =  $jsonArray ['response']; 
foreach($newJson as $data)
{
  echo $data['id'];
}

OR You can directly loop through your $jsonArray['response'].

foreach($jsonArray['response'] as $data)
{
  echo $data['id'];
}

CodePudding user response:

$json = '{"response":[
{
    "id": 37,
    "slug": "red",        
    "stock": true,
    "name": "Red",
    "default": 0,
    "sizes": "38"
  },
  {
    "id": 38,
    "slug": "red",        
    "stock": true,
    "name": "Red",
    "default": 0,
    "sizes": "40"
  }
]}';

$data = json_decode($json);         // we decode the JSON

$num = count($data->response);      // we count how many response keys we have

$i=0;                               // define $1=0 to start our iteration
while ($i < $num) {                 // $i must be < than the counted array keys
    
    $op = $data->response[$i]->id;  // we tell the while loop to iterate over the array
    print $op."</br>";              // prints out 37 & 38
   
    $i  ;                           // Standard incrementation

}
  • Related