1

I hava a php page where i have a function which does not print or echo to the page it returns a variable called content which is rendered on an html page. Throughout out this function i am appending to the content variable.

i have an array ($myArray) which looks like

Array ( [0] => Array ( [0] => 2011-11-12 [1] => 1963-02-29 [3] => 2029-05-14 [9] => 1812-08-12 [11] => 1537-05-17  [16] => 2005-17-04  [30] => 3000-42-99  ) )

I am trying to loop through each item in $myArray and have it returned to the screen as a h1.

  foreach($myArray as $item  ){
    $content .= '<h1>'.$item.'</h1>';
    }

This results in me getting an

Notice: Array to string conversion

error.

1
  • $item is an array, you either need to change how you are building $myArray, or you need to iterate once more (or implode cleverly). Commented Jul 10, 2017 at 3:14

1 Answer 1

2

You have nested array , so do like below

$arr=array ('0' => array ('0' => '2011-11-12' ,'1' => '1963-02-29' ,'3' => '2029-05-14' ,'9' => '1812-08-12' ,'11' => '1537-05-17'  ,'16' => '2005-17-04'  ,'30' => '3000-42-99')) ;
    foreach($arr as $myArray){
        foreach($myArray as $item  ){
                $content .= '<h1>'.$item.'</h1>';
        }
    }
    echo $content;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.