0

This is my array structure:

Array
(
    [Title] => Array
        (
            [0] => 
            [1] => One
            [2] => Two
            [3] => Three
            [4] => Four
            [5] => test
            [6] => fsfd
            [7] => wa

        )

)

I would like to print the title and array elements so that it is structured like this:

Title

  • One
  • Two
  • Three

etc

I am currently having trouble doing this using the conventional for each loop:

      foreach($items as  $key => $notice ){?>   

}?>

What is the best way to do this? Thanks

2
  • 2
    What kind of trouble? Where's the innards of your loop? Commented Aug 8, 2013 at 14:53
  • Here's a hint: $items['Title'] is an array of strings. Commented Aug 8, 2013 at 14:57

4 Answers 4

1

Your array is nested. Either use

foreach($items['Title'] as  $key => $noticeArr ){

Or if you wish to print the keys of the first array use:

foreach($items as  $key => $noticeArr ){
    echo $key . "\n";
    foreach($noticeArr as $notice){
        echo $notice . "\n";//Wrap in <li> tags or however you want to display.
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jim. Worked a treat.
0

You have an array "Title" inside your array.

You could use the traditional for loop:

for ($i = 0; $i < count($yourArray['Title']); $i++) {
  echo $yourArray['Title'][$i];
}

or a foreach:

foreach ($yourArray['Title'] as $item) {
  echo $item;
}

Comments

0

You just have to iterate two times, one for the first array and another for the nested one.

$data = array(
    'Title' => array('One','Two','Three'),
);

foreach ($data as $name => $results) {
    echo $name . "<br />";

    foreach ($results as $label)
    {
        echo $label . "<br />";
    }
}

Comments

0

Try this, will work

foreach($items as  $key => $noticeArr ){
   echo $key . "<br />";
    $array = array_filter($noticeArr, create_function('$a','return $a!=="";'));

    foreach($array as $notice){
        echo "<li>".$notice ."</li>"."<br />";
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.