仅通过键名来获取JSON中的特定键/值,而不是全部

时间:2021-02-14 23:48:47

I have JSON that shows multiple key-value pairs. I'd like to loop through and create a new array with only the key-value pairs I want in order to display them. This is my attempt, but of course, my code is re-writing my array ($new_array) to have only one element.

我有JSON显示多个键值对。我想循环创建一个只包含我想要的键值对的新数组,以便显示它们。这是我的尝试,但当然,我的代码重写了我的数组($ new_array)只有一个元素。

 //SAMPLE JSON
 [{"PropertyId":"555","FloorplanId":"555","FloorplanName":"Studio","Beds":"0","Baths":"1.00","AvailabilityURL","UnitTypeMapping":".058500"},{"PropertyId":"666","FloorplanId":"666","FloorplanName":"Studio","Beds":"0","Baths":"1.00","AvailabilityURL","UnitTypeMapping":".058500"}]

 //GET ALL JSON FROM URL
 $json = file_get_contents('<URL>');
 $data = json_decode($json);
 // print_r($data); //ALL keys

 //GET JUST THE KEYS I WANT
 $new_array = array("FloorplanName"=>"","Beds"=>"","Baths"=>"");

 // Create new array
 foreach($data as $item) {
   $new_array['FloorplanName'] = $item->{'FloorplanName'};
   $new_array['Beds'] = $item->{'Beds'};
   $new_array['Baths'] = $item->{'Baths'};
 }

 //loop over $new_array
 foreach($new_array as $item) {
   $item->{'FloorplanName'};
   $item->{'Beds'};
   $item->{'Baths'};
 }

1 个解决方案

#1


1  

Right now, you set associative keys on $new_array and overwrite your data each time through your foreach loop. Instead, you need to add an item (a sub-array) to $new_array and assign the data to the sub-array.

现在,您在$ new_array上设置关联键,并且每次通过foreach循环覆盖您的数据。相反,您需要将项(子数组)添加到$ new_array并将数据分配给子数组。

Instead of this:

而不是这个:

//GET JUST THE KEYS I WANT
$new_array = array("FloorplanName"=>"","Beds"=>"","Baths"=>"");

// access property of object in array
 foreach($data as $item) {
   $new_array['FloorplanName'] = $item->{'FloorplanName'};
   $new_array['Beds'] = $item->{'Beds'};
   $new_array['Baths'] = $item->{'Baths'};
 }

You need

$new_array = array(); // this array should be empty

// access property of object in array
foreach($data as $item) {
  $new_array[] = array(
    'FloorplanName' => $item->{'FloorplanName'},
    'Beds' => $item->{'Beds'},
    'Baths' => $item->{'Baths'},
  );
}

Also, to loop through the new array, you need to change your final loop:

此外,要循环遍历新数组,您需要更改最终循环:

//loop over $new_array
foreach($new_array as $item) {
  echo $item['FloorplanName'];
  echo $item['Beds'];
  echo $item['Baths'];
}

#1


1  

Right now, you set associative keys on $new_array and overwrite your data each time through your foreach loop. Instead, you need to add an item (a sub-array) to $new_array and assign the data to the sub-array.

现在,您在$ new_array上设置关联键,并且每次通过foreach循环覆盖您的数据。相反,您需要将项(子数组)添加到$ new_array并将数据分配给子数组。

Instead of this:

而不是这个:

//GET JUST THE KEYS I WANT
$new_array = array("FloorplanName"=>"","Beds"=>"","Baths"=>"");

// access property of object in array
 foreach($data as $item) {
   $new_array['FloorplanName'] = $item->{'FloorplanName'};
   $new_array['Beds'] = $item->{'Beds'};
   $new_array['Baths'] = $item->{'Baths'};
 }

You need

$new_array = array(); // this array should be empty

// access property of object in array
foreach($data as $item) {
  $new_array[] = array(
    'FloorplanName' => $item->{'FloorplanName'},
    'Beds' => $item->{'Beds'},
    'Baths' => $item->{'Baths'},
  );
}

Also, to loop through the new array, you need to change your final loop:

此外,要循环遍历新数组,您需要更改最终循环:

//loop over $new_array
foreach($new_array as $item) {
  echo $item['FloorplanName'];
  echo $item['Beds'];
  echo $item['Baths'];
}