PhP howto sizeof(JSON:数组[])

PhP howto sizeof(JSON:array[])

我的代码中有一个错误,但解决方案深埋在 JSON return

https://api.instagram.com/v1/users/self/media/liked?access_token=207436640.c57b856.b0a8944d1e0c4e70a6ec581679c656b5

有数组[17]我必须循环

            foreach ($arr['data'][0]['likes']['data'] as $liker){
                    echo '<img src="'.$liker['profile_picture'].'" /></br>';
                    echo '<p> '.$liker['username'].'</p></br>';
            }

对一个人来说效果很好,但是

            for($i = 0; $i < sizeof($arr['data'][]); $i++){
                    echo '<img src="'.$arr['likes']['data']['profile_picture'].'" /></br>';
                    echo '<p> '.$liker['username'].'</p></br>';
            }

结果不尽如人意。

Fatal error: Cannot use [] for reading in /var/www/localhost/htdocs/instagram2.php on line 68

或者如果我删除 [],我会得到“undefined 'likes'”。

如何正确循环 JSON 子数组?

嗯,$arr['data'][] 是引用数组数据值的无效方法,正确的方法是 $arr['data']

for($i = 0; $i < sizeof($arr['data']); $i++){
    foreach ($arr['data'][$i]['likes']['data'] as $liker) {
        echo '<img src="'.$liker['profile_picture'].'" /></br>';
        echo '<p> '.$liker['username'].'</p></br>';
    }
}

您可以在此处阅读有关数组的更多信息http://php.net/manual/en/language.types.array.php

您将 forforeach 进行比较的方法是错误的。在 foreach 中,迭代期间的当前项自动分配给 $liker,但是在 for 中不会发生类似的情况。您只是在增加 $i.

的值
/// @shaddy has said, this is the correct way to access the array size
/// it is more optimal to calculate it outside of the loop.
$len = sizeof($arr['data']);

/// with a for loop, you have to use $i to calculate the item
for($i = 0; $i < $len; $i++){
  $item = $arr['data'][$i];
  /// and then access each of the subitems inside $item
  ...
}

但是你还有其他问题。您似乎在尝试为点赞者输出每个 profile_pictures 的图像;但这也是一组数据。在您的第一个示例中,您直接单步执行此数组,因此一切正常。然而,在你的第二个中,你只是循环容器数组,而不是喜欢者列表。要让它工作,你必须有第二个循环:

$len = sizeof($arr['data']);
/// with a for loop, you have to use $i to calculate the item
for($i = 0; $i < $len; $i++){
  $item = $arr['data'][$i];
  /// best to check for existence before accessing to avoid errors
  if ( !empty($item['likes']['data']) ) {
    $likers = $item['likes']['data'];
    $len2 = sizeof($likers);
    /// step each of the likers
    for($k = 0; $k < $len2; $k++){
      $liker = $likers[$k];
      if ( !empty($liker['profile_picture']) ) {
        echo '<img src="'.$liker['profile_picture'].'" /></br>';
      }
      /// the username field is also part of the user array inside item
      if ( !empty($liker['username']) ) {
        echo '<p> '.$liker['username'].'</p></br>';
      }
    }
  }
}

然而上面的一切看起来都很复杂,我通常不在 php 中使用 for 循环,foreach 有一个更好的界面可以使用,并且旨在使用数组:

foreach ( $arr['data'] as $i => $item ) {
  if ( !empty($item['likes']['data']) ) {
    foreach ( $item['likes']['data'] as $k => $liker ) {
      if ( !empty($liker['profile_picture']) ) {
        echo '<img src="' . $liker['profile_picture'] . '" /></br>';
      }
      if ( !empty($liker['username']) ) {
        echo '<p> ' . $liker['username'] . '</p></br>';
      }
    }
  }
}

当然,您可能只是想像在第一个示例中一样遍历 likers 数组,要使用 for 做到这一点,您只需要:

$likers = $arr['data'][0]['likes']['data'];
$len = sizeof($likers);
for( $i = 0; $i < $len; $i++ ){
  echo '<img src="' . $likers[$i]['profile_picture'] . '" /></br>';
  echo '<p> ' $likers[$i]['username'] . '</p></br>';
}

请注意:

还有一个注意事项需要注意,如果不正确,这将导致上述某些片段失败。上述所有代码始终使用数组,因此 $arr 数组必须仅包含数组。但是,您的数据集显示了数组和对象的混合。在处理 PHP 中的对象时,您需要使用不同的访问器。

因为我看不到你是如何加载你的 JSON 的,所以很难给出建议,因为对象可以转换成数组的方式取决于方式。您所有的示例都将结构的每个部分都视为一个数组,这就是为什么我在上面假设相同。

然而,标准方法是使用 json_decode,默认情况下 would/should 保留 array/object 差异。这意味着只要有一个对象,您就需要使用 $obj -> prop 而不是 $obj['prop'],所以我上面的首选示例如下所示:

foreach ( $arr['data'] as $i => $item ) {
  if ( !empty($item->likes['data']) ) {
    foreach ( $item->likes['data'] as $k => $liker ) {
      if ( !empty($liker->profile_picture) ) {
        echo '<img src="' . $liker->profile_picture . '" /></br>';
      }
      if ( !empty($liker->username) ) {
        echo '<p> ' . $liker->username . '</p></br>';
      }
    }
  }
}

此外,如果您更喜欢只使用数组,您可以使用类似这样的东西:

https://coderwall.com/p/8mmicq/php-convert-mixed-array-objects-recursively