PHP foreach跳过数组迭代 [英] PHP foreach skip iterate in an array
本文介绍了PHP foreach跳过数组迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我收到未定义的索引"时,我想随时在php中跳过迭代.到目前为止,这是我的代码:
I want to skip iterate in a php foreach anytime when I receaive "Undifined index". Here is my code so far:
<?php
$albums = $facebook->api("/me/albums");
$i=0;
foreach ($albums['data'] as $album) {
if (is_null($album['cover_photo'])) continue;
if($i==8) break;
$album_id = $album['id'];
$album_cover = $album['cover_photo'];
$album_name = $album['name'];
$album_count = $album['count'];
$covers = $facebook->api("/" . $album_cover . "");
$source = $covers['source'];
?>
如果我没有if is_null
语法,代码会中断,但会收到错误消息,cover_photo索引未定义(这是正常行为).至少如果我无法显示错误,就足够了.
If I don't have the if is_null
syntax the code breaks but I receive the error that cover_photo index is undefined (it is the normal behaviour). At least if I could not display the error it could be enough.
推荐答案
而不是尝试
if (!isset($album['cover_photo'])) continue;
或者更好地在分配变量之前检查变量isset
.
Or better check the variable isset
before assigning it.
$album_cover = (isset($album['cover_photo'])) ? $album['cover_photo'] : '';
这篇关于PHP foreach跳过数组迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文