duanlvxi8652 2017-07-16 03:40
浏览 44

跳过整行,如果一个单元格在MySQL表中使用PHP foreach为Null

This is my code:

$cast_name_list = explode(',',$result[$x]["cast_name"]);
$cast_profile_path = explode(',',$result[$x]["cast_profile_path"]);
$cast_character = explode(',',$result[$x]["cast_character"]);

foreach( $cast_name_list as $index => $cast_name ) 
{
echo 'name = '.$cast_name.'<br/> ';
echo ' character name = '.$cast_character[$index].' <br/>';
echo ' profile path = '.$cast_profile_path[$index].' <br/><br/><br/>';
}

This is, how my data in MySQL table looks like:

cast_name       cast_character    cast_profile_path
Robert             Iron man       example.com/sdfsd.jpg
Rahul               Mohit         example.com/aadfsd.jpg
Tom                 Josh             Null
Tripp               James        example.com/zzz.jpg

What I want:, If there is a NULL cell in the table, then the whole row will not echo.

i.e: Tom Josh Null

But, this is what my code echo

name = robert
character name = Iron Man
profile path = example.com/sdfsd.jpg

name = Rahul
character name = Mohit
profile path = example.com/aadfsd.jpg


name = Tom
character name = Josh
profile path = example.com/zzz.jpg


name = Tripp
character name = James
profile path = Notice: Undefined offset: 8 

Expected Output:

name = robert
character name = Iron Man
profile path = example.com/sdfsd.jpg

name = Rahul
character name = Mohit
profile path = example.com/aadfsd.jpg


name = Tripp
character name = James
profile path = Notice: example.com/zzz.jpg

Edit: Since one value is null in 3rd row, so $cast_profile_path array will contain only 4 values, while $cast_name & $cast_character contain 5 values.

  • 写回答

1条回答 默认 最新

  • dongpan8928 2017-07-16 04:20
    关注

    Try this:

    foreach ($cast_name_list as $index => $cast_name) {
        if (!array_key_exists($index, $cast_profile_path)) {
            continue;
        }
    
        echo 'name = '.$cast_name.'<br/> ';
        echo ' character name = '.$cast_character[$index].' <br/>';
        echo ' profile path = '.$cast_profile_path[$index].' <br/><br/><br/>';
    }
    

    For reference, see:

    Note Not sure if I understand correctly, do you store values separated by comma in table columns?

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行