Reputation:
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>";
Upvotes: 0
Views: 53
Reputation: 57121
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']);
Upvotes: 3