如何在php中过滤只有有效对象的数组

如何在php中过滤只有有效对象的数组,php,json,Php,Json,我有一个这样的数组,是从解析html源代码中得到的: Array ( [0] => 12345 [1] => 54321 [2] => 32546 [3] => 98754 [4] => 15867 [5] => 75612 ) 如果我把它放在一个变量中,然后运行一个foreach循环,在url前面加前缀,如下所示: $x = Array; foreach ($x as $y){ $r = 'http://ex

我有一个这样的数组,是从解析html源代码中得到的:

Array
(
    [0] => 12345
    [1] => 54321
    [2] => 32546
    [3] => 98754
    [4] => 15867
    [5] => 75612
)
如果我把它放在一个变量中,然后运行一个foreach循环,在url前面加前缀,如下所示:

$x = Array;
foreach ($x as $y){
$r = 'http://example.com/' . $y;
echo $r . '<br>';
}
如果在浏览器中运行,则每个输出url都将输出一个对象,如下所示:

http://example.com/12345
http://example.com/54321
http://example.com/32546
http://example.com/98754
http://example.com/15867
http://example.com/75612
{
   "key1": "value1",
   "key2": "value2",
   "key3": "value3"
}
{
   "error": {
      "errorMessage": "errorMessageValue",
      "errorType": "errorTypeValue"
   }
}
或者像这样:

http://example.com/12345
http://example.com/54321
http://example.com/32546
http://example.com/98754
http://example.com/15867
http://example.com/75612
{
   "key1": "value1",
   "key2": "value2",
   "key3": "value3"
}
{
   "error": {
      "errorMessage": "errorMessageValue",
      "errorType": "errorTypeValue"
   }
}
所以我的问题是。。。如何在php中过滤数组,使其只提供一个具有有效键/值对的数组,而不是错误对象

$x = Array;
foreach ($x as $y){
  $link = 'http://example.com' . $y;
  $json = file_get_contents($link);
  $returned_data = json_decode($json, true);

  // If returned data contains an "error" entry, just move to next one
  if(isset($returned_data['error'])) {
    continue;
  }
  echo '<pre>';
  echo json_encode($returned_data) . '<br>';
  echo '</pre>';
}
根据建议,我尝试了以下方法:

$x = Array;
foreach ($x as $y){
$link = 'http://example.com' . $y;
$json = file_get_contents($link);
$array = array_filter((array)json_decode($json), "is_scalar");
echo '<pre>';
echo json_encode($array) . '<br>';
echo '</pre>';
}
$x=数组;
外汇($x为$y){
$link='1http://example.com“.$y;
$json=file\u get\u contents($link);
$array=array_filter((array)json_decode($json),“is_scalar”);
回声';
echo json_encode($array)。“
”; 回声'; }
但它仍然输出相同的数组,并且不排除错误对象

$x = Array;
foreach ($x as $y){
  $link = 'http://example.com' . $y;
  $json = file_get_contents($link);
  $returned_data = json_decode($json, true);

  // If returned data contains an "error" entry, just move to next one
  if(isset($returned_data['error'])) {
    continue;
  }
  echo '<pre>';
  echo json_encode($returned_data) . '<br>';
  echo '</pre>';
}
这将从数组中排除所有对象,并且只有key=>value对,其中value既不是对象也不是数组


这将从数组中排除所有对象,并且只有key=>value对,其中value既不是对象也不是数组。

可能我遗漏了什么,但您不能检查返回的对象是否包含“error”条目,如果包含,则跳过它吗

$x=数组;
外汇($x为$y){
$link='1http://example.com“.$y;
$json=file\u get\u contents($link);
$returned\u data=json\u decode($json,true);
//如果返回的数据包含“错误”条目,只需移动到下一个条目
if(isset($returned_data['error'])){
继续;
}
回声';
echo json_encode($returned_data)。“
”; 回声'; }
也许我遗漏了什么,但您不能检查返回的对象是否包含“错误”条目,如果包含,则跳过它吗

$x=数组;
外汇($x为$y){
$link='1http://example.com“.$y;
$json=file\u get\u contents($link);
$returned\u data=json\u decode($json,true);
//如果返回的数据包含“错误”条目,只需移动到下一个条目
if(isset($returned_data['error'])){
继续;
}
回声';
echo json_encode($returned_data)。“
”; 回声'; }
请添加添加信息,在何处生成该数组?请添加添加信息,在何处生成该数组?除非第二个参数设置为true@Martin好的,增加了阵型;-)@bwoebi使用您的代码,我得到
注意:数组到字符串的转换
当尝试使用
文件内容
获得
$json
工作时,然后回显
$Array
如果您想将我的数组输出/存储在某处,您必须使用
json\u encode($Array)对其重新编码
@bwoebi它仍然输出与我在原始版本中相同的数组。它没有排除错误object.json_decode返回一个对象,除非第二个参数设置为true@Martin好的,增加了阵型;-)@bwoebi使用您的代码,我得到
注意:数组到字符串的转换
当尝试使用
文件内容
获得
$json
工作时,然后回显
$Array
如果您想将我的数组输出/存储在某处,您必须使用
json\u encode($Array)对其重新编码
@bwoebi它仍然输出与我在原始版本中相同的数组。它没有排除错误对象。