0

I have seen all the questions that are relative with mine, and still didn't find any soloution to my 'simple' problem. I have a 3dimensional array and i just want to display the results. But when it's tome to echo the . "<td>".$person ['job']."</td>" i am getting errors. Any advice thnx

$people= array(

array(
    "name" => "Jennifer Kimbers",
    "age"=>"45",
    "email" => "[email protected]",
    "city" => "Seattle",
    "state" => "Washington"),
        array(
            "job"=>"web developer"
        ),

array(
    "name" => "Rodney Hutchers",
    "age"=>"55",
    "email" => "[email protected]",
    "city" => "Los Angeles",
    "state" => "California"),
        array(
             "job"=>"data developer"
         );

echo "<table>"
    ."<th>FullName</th>"
    ."<th>Age</th>"
    ."<th>Email</th>"
    ."<th>City</th>"
    ."<th>State</th>"
    ."<th>Job</th>";

 foreach ($people as $person) {
    echo "<tr>"
        . "<td>" . $person ['name'] . "</td>"
        . "<td>" . $person ['age'] . "</td>"
        . "<td>" . $person ['email'] . "</td>"
        . "<td>" . $person ['city'] . "</td>"
        . "<td>" . $person ['state'] . "</td>"
        . "<td>" . $person ['job'] . "</td>"
        . "</tr>";

}
echo "</table>";
2
  • The input array has 4 items with different structure. Is this the correct input? Commented Jun 1, 2020 at 12:14
  • I think the structure of your array is not complete. Commented Jun 1, 2020 at 12:18

1 Answer 1

3

Your array structure is slightly off, in

array(
    "name" => "Jennifer Kimbers",
    "age"=>"45",
    "email" => "[email protected]",
    "city" => "Seattle",
    "state" => "Washington"),  // Close bracket here
        array(
            "job"=>"web developer"
        ),

This indented properly is

array(
    "name" => "Jennifer Kimbers",
    "age"=>"45",
    "email" => "[email protected]",
    "city" => "Seattle",
    "state" => "Washington"),
array(
    "job"=>"web developer"),

so your loop is trying to use this as two separate bits of data and the second one doesn't contain a lot of the fields you are expecting.

You need to make sure you close the array elements in the right place / add the job to the same element as the rest of the data...

array(
    "name" => "Jennifer Kimbers",
    "age"=>"45",
    "email" => "[email protected]",
    "city" => "Seattle",
    "state" => "Washington",  // Move ) after the job
    "job" => "web developer"
),

If you need that extra level of arrays, then you could make it a list of jobs...

array(
    "name" => "Jennifer Kimbers",
    "age"=>"45",
    "email" => "[email protected]",
    "city" => "Seattle",
    "state" => "Washington",
    "jobs" => array( "title" => "web developer")
),

The to display them

foreach ($people as $person) {
    echo "<tr>"
        . "<td>" . $person ['name'] . "</td>"
        . "<td>" . $person ['age'] . "</td>"
        . "<td>" . $person ['email'] . "</td>"
        . "<td>" . $person ['city'] . "</td>"
        . "<td>" . $person ['state'] . "</td>"
        . "<td>";
    foreach ( $person['jobs'] as $job ) {
         echo $job . "/";
    }
    echo "</td>"
        . "</tr>";
}

although this way you will end up with a trailing / after the job title, it shows the principle. You could instead of the inner foreach() loop use...

echo implode("/", $person['jobs']);
Sign up to request clarification or add additional context in comments.

10 Comments

Look at the end of his entire array, does feel right?
@Hardood, it is possible that in copying the code to the question there has been a typo. If the typo was in the original code I'm not sure it would get to the output to throw an error there.
Nigel Ren, i actually want to use array( "name" => "Jennifer Kimbers", "age"=>"45", "email" => "[email protected]", "city" => "Seattle", "state" => "Washington"), array( "job"=>"web developer"), cause my project requires 3 dimensional array
Suppose that my array is ok how can i display the results with foreach statement
I've added some more examples for using an extra level of the array.
|

Your Answer

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