多维数组视图定义的行

时间:2015-06-09 07:46:16

标签: php arrays multidimensional-array

我希望看到包含这些元素的单行。

$ArrayDocs = array (
  0 => array ('titre' => 'aaa','ref' => 'aaa','date' => 'aaa','like' => aaa,'url' => 'aaa',),
  1 => array ('titre' => 'aaa1','ref' => 'aaa1','date' => 'aaa1','like' => aaa1,'url' => 'aaa1',)
  );

示例:

foreach($ArrayDocs AS $key => $doc)
    {
      echo $key.' '.$doc['titre'].' '.$doc['ref'].' ';

    }

转换:不起作用。

 foreach($ArrayDocs[1] AS $key => $doc) // Example shows the line 1 of Table
    {
      echo $key.' '.$doc['titre'].' '.$doc['ref'].' ';

    }

1 个答案:

答案 0 :(得分:0)

由于您明确将父数组容器中的索引设置为1循环内的foreach索引。

foreach($ArrayDocs[1] AS $key => $doc) // Example shows the line 1 of Table
                 // ^

print_r结果:

Array
(
    [0] => Array
        (
            [titre] => aaa
            [ref] => aaa
            [date] => aaa
            [like] => aaa
            [url] => aaa
        )

    [1] => Array // when you set $ArrayDocs[1] inside foreach, you're already here
        (
            [titre] => aaa1
            [ref] => aaa1
            [date] => aaa1
            [like] => aaa1
            [url] => aaa1
        )

)

您不需要拥有该索引。所以指出:

echo $key.' '.$doc['titre'].' '.$doc['ref'].' ';
             //   ^              //   ^ isn't needed

只需:

foreach($ArrayDocs[1] AS $key => $doc) // Example shows the line 1 of Table
{
    echo $key.' '.$doc . '<br/>';
}

Sample Output