0

I have this array $data['lists']

Array
(
    [0] => stdClass Object
        (
            [NIK] => 00001
            [NAME] => Name 1
        )

    [1] => stdClass Object
        (
            [NIK] => 00002
            [NAME] => Name 2
        )
)

For some condition the array will be changed so i create a function to get the keys. Here is what i do

foreach($data['lists'] as $key => $val)
   {
        foreach( $val as $keyItem => $valKey)
          {
                $data['column'][] =  $keyItem;
          }
   }   
$data['kolom'] = array_unique($data['column']);

then in HTML I do this

<?php
$no = 0;
for ($y = 0; $y < count($lists); $y++) {
    $no++;
    echo "<tr>";
    echo "<td>" . $no . "</td>";
    for ($x = 0; $x < count($kolom); $x++) {
        echo "<td>" . $lists[$x]->$kolom[$x] . "</td>";
    }
    echo "</tr>";
}

but when i run it, i get this error Message: Array to string conversion. How can i fix it ? thanks in advance

3
  • 1
    As error said somewhere you are trying to convert array to string. You need to find out where is trying to do it and make a check to make sure that printout is a string not an array object Commented Oct 24, 2019 at 9:36
  • What's the purpose of all this? Commented Oct 24, 2019 at 9:36
  • 1
    Looks like here (echo "<td>" . $lists[$x]->$kolom[$x] . "</td>"; ) you try print array, that is why you coaught this error. Commented Oct 24, 2019 at 9:38

1 Answer 1

1

Why not just:

// iterate over `$lists`
foreach ($lists as $val) {
    $no++;
    echo "<tr>";
    echo "<td>" . $no . "</td>";
    // output every value from each `$lists` item
    foreach ($val as $valKey) {
        echo "<td>" . $valKey . "</td>";
    }
    echo "</tr>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

ohh, never think this way. I make it so hard -_-

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.