本文介绍了foreach多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Product1 date1 time1 
product2 date2 time2 $ b $ product3 date3 time3
product4 date4 time4

I一直在挣扎和挣扎,我尝试了双重的foreaches等,但不知何故,我不明白它的工作...



任何人都可以解释给我吗?数组

[product] =>数组

[


$ 0] => product1
[1] => product2
[2] => product3
[3] => product4


[date] => Array

[0] => date1
[1] => date2
[2] => date3
[3] => date4


[time] => Array

[0] => time1
[1 ] => time2
[2] =>时间3
[3] => time4




解决方案

$ p
$ b

  $ count = count($ data ['product']); 
($ i = 0; $ i <$ count; $ i ++){
echo $ data ['product'] [$ i]。 ''。 $ data ['date'] [$ i]。 ''。$ data ['time'] [$ i]。 '< br />';



$ b $要添加数据验证/完整性(停止 undefined index errors),还要检查每个索引是否存在于子数组中:

  $ count = count($ data ['product']); 
for($ i = 0; $ i <$ count; $ i ++){
$ date = isset($ data ['date'] [$ i])? ''。 $ data ['date'] [$ i]:'';
$ time = isset($ data ['time'] [$ i])? ''。 $ data ['time'] [$ i]:'';
echo $ data ['product'] [$ i]。 $日期。 $时间。 '< br />';
}


How do i loop through the following array with a foreach, so it gets printed like this:

Product1 date1 time1
product2 date2 time2
product3 date3 time3
product4 date4 time4

I have been struggling and strugling, i tried double foreaches etc, but somehow i don't get it to work...

Is Anyone able to explain it to me?

Array
(
    [product] => Array
        (
            [0] => product1
            [1] => product2
            [2] => product3
            [3] => product4
        )

    [date] => Array
        (
            [0] => date1
            [1] => date2
            [2] => date3
            [3] => date4
        )

    [time] => Array
        (
            [0] => time1
            [1] => time2
            [2] => time3
            [3] => time4
        )

)
解决方案

Something like this should work:

$count = count($data['product']);
for ($i = 0; $i < $count; $i ++) {
    echo $data['product'][$i] . ' ' . $data['date'][$i] . ' ' .$data['time'][$i] . '<br />';
}

To add data-verification/integrity to it (to stop undefined index errors), also check if each index exists in the sub-arrays:

$count = count($data['product']);
for ($i = 0; $i < $count; $i++) {
    $date = isset($data['date'][$i]) ? ' ' . $data['date'][$i] : '';
    $time = isset($data['time '][$i]) ? ' ' . $data['time '][$i] : '';
    echo $data['product'][$i] . $date . $time . '<br />';
}

这篇关于foreach多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:49