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'] : '';