PHP对每个多维数组进行特殊处理



我得到了这样的数组

$specials = array( 1 => array('word' => array('first', 'two', 'three'), 'digit' => array(1,2,3)),
                   2 => array('word' => array('four','five', 'six'), 'digit' => array(4,5,6)), 
                   3 => array('word' => array('seven', 'eight', 'nine'), 'digit' => array(7,8,9)),
                   4 => array('word' => array('ten','eleven', 'twelve'), 'digit' => array(10,11,12)) 
                   );

以及为什么我要像这样每重复3次

foreach($specials as  $val) {
    foreach($val as $valData) {
        foreach($valData as $value) {
            echo $value.'<br/>';
        }
    }
} 

但是如何正确地循环或foreach并使用它们的索引名呢?

echo $value['word'];
echo $value['digit'];

如果echo $value['digit']

我得到错误警告
Warning: Illegal string offset 'digit' in ~/public_html/test/array.php on line 58

我需要这些输出为不同的HTML和CSS每个值

<div class="digit"><?=$value['digit']?></div> <div class="word"><?=$value['word']?></div>

foreach($specials as  $val) {
    foreach($val as $key => $valData) {
        // $key is now either 'word' or 'digit'
        foreach($valData as $value) {
            echo "<div class='$key'>$value</div>";
        }
    }
} 

相关内容

  • 没有找到相关文章

最新更新