0

Good morning

I seem to be catching an error in PHP, ` Notice: Array to string conversion in C:\xampp\htdocs\sample\sample_page.php on line 80

I have an example multidimensional array as shown below

Array
(
    [0] => Array
        (
            [id] => 3791671
            [name] => photos
            [permissions] => Array
                (
                    [modify] => 1
                    [view] => 1
                    [collect] => 1
                )

        )

    [1] => Array
        (
            [id] => 3791677
            [name] => vehicle_incident_report_c_vehicle_details
            [permissions] => Array
                (
                    [modify] => 1
                    [view] => 1
                    [collect] => 1
                )

        )

    [2] => Array
        (
            [id] => 3791680
            [name] => vehicle_incident_report_c_vehicle_details_c_photos
            [permissions] => Array
                (
                    [modify] => 1
                    [view] => 1
                    [collect] => 1
                )

        )

    [3] => Array
        (
            [id] => 3791689
            [name] => inspection_photos
            [permissions] => Array
                (
                    [modify] => 1
                    [view] => 1
                    [collect] => 1
                )

        )
)

I am trying to format this array into a table format using the code below, but it gives me an array to string conversion error

// Transform data
$headers = [];
$rows = [];
$rowIdx = 0;
foreach ($array_example as $sample) {
   foreach ($sample as $key => $value) {
     if (!in_array($key, $headers)) {
         $headers[] = $key;
      }
     $rows[$rowIdx][$key] = $value;
     }
     $rowIdx++;
                    }

     // Display data
     $html = "<table style='width:100%' id='table_id'><thead><tr>";
         foreach ($headers as $header) {
             $html .= "<th>$header</th>";
         }
         $html .= "</tr></thead><tbody>";
         foreach ($rows as $row){
            $html .= "<tr>";
            foreach ($headers as $header) {
                $html .= "<td>";
                $html .= $row[$header]?? '';
                $html .= "</td>";
           }
           $html .= "</tr>";
          }
         $html .= "</tr></tbody></table>";
         echo $html;

Line 80 consists of

$html .= $row[$header]?? '';

Thank you in advance

`

2
  • and where is the line 80? Commented Apr 14, 2020 at 8:41
  • $html .= $row[$header]?? ''; Commented Apr 14, 2020 at 8:44

1 Answer 1

1

the error is not in PHP , it's in how do you access your array.

inherited loops does not mean that you will loop over the sub array by default

you are populate your array to be a multi-dimensional array like in this line

$rows[$rowIdx][$key] = $value;

While you are trying to access this array like a one dimensional array like this

$html .= $row[$header];

you need to access the two-dimensional array , looping over $headers array does not mean that you are looping over the $row array

so , this may fix your issue

foreach ($rows as $index => $row) {
    // ....
    foreach ($headers as $header) {
        //....
        $html .= $row[$index][$header] . '';
        //....
    }
    // ....
}
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.