Multidimensional Array view defined line

60 views Asked by At

I would like to see a single line with these elements.

$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',)
  );

Example :

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

    }

Transform : It does not work.

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

    }
1

There are 1 answers

1
Kevin On BEST ANSWER

Since you explicitly set the index in the parent array container to index 1 inside your foreach loop.

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

print_r result:

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
        )

)

You don't need to have that index. So pointing into:

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

Just:

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

Sample Output