Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKris Byrum
32,636 PointsMultidem Array
I'm attempting to make an unordered list.
The Line Items for this list are located inside an Array -
"description" => array(
"1" => "Testing level 1",
"2" => "Testing level 2",
"3" => "Testing level 3"
)
This array is an item located in another Array -
$experience[1] = array(
"employer" => "Employer",
"city" => "City, ST",
"title" => "Title",
"description" => array(
"1" => "Testing level 1",
"2" => "Testing level 2",
"3" => "Testing level 3"
)
);
I have a function what has two arguments created on my index.php page using a foreach loop -
function experience_list($exp_id, $exp) {
$output = "";
$output = $output . '<div>';
$output = $output . '<h3>' . $exp['employer'] . '</h3>';
$output = $output . '<h3>' . $exp['city'] . '</h3>';
$output = $output . '<h3>' . $exp['title'] . '</h3>';
$output = $output . '<li>' . foreach($exp['description'] as $duty){echo $duty;} . '</li>';
$output = $output . '</div>';
return $output;
}
I have attempted to use a foreach loop inside this function, create another function globally and use it inside this function, and a few other things.
Could anyone assist me please? I'm attempted to review some of the PHP lessons again as well as some searching online.
Thanks in advance!!!!
1 Answer
Robert Walker
17,146 PointsOk so hopefully this works now assuming your $exp is an array to start with like so:
$exp_id = 4;
$exp = array(
array(
"employer" => "Employer 1",
"city" => "City, SS 1",
"title" => "Title 1",
"description" => array(
"1" => "Testing level 1",
"2" => "Testing level 1",
"3" => "Testing level 1"
)
),
array(
"employer" => "Employer 2",
"city" => "City, SS 2",
"title" => "Title 2",
"description" => array(
"1" => "Testing level 2",
"2" => "Testing level 2",
"3" => "Testing level 2"
)
)
);
var_dump($exp); // testing
function experience_list($exp_id, $exp){
foreach($exp as $listing){
$output = '' . $output . '<div>';
$output = '' . $output . '<h3>' .$listing['employer'] . '</h3>';
$output = '' . $output . '<h3>' .$listing['city'] . '</h3>';
$output = '' . $output . '<h3>' .$listing['title'] . '</h3>';
$output = '' . $output . '<ul>';
foreach($listing['description'] as $duty){
$output = '' . $output . '<li>' . $duty . '</li>';
}
$output = '' . $output . '</ul>';
$output = '' . $output . '</div>';
}
return $output;
}
$result = experience_list($exp_id, $exp);
echo $result;
Which (tested) outputs:
<div><h3>Employer 1</h3><h3>City, SS 1</h3><h3>Title 1</h3><ul><li>Testing level 1</li><li>Testing level 1</li><li>Testing level 1</li></ul></div>
<div><h3>Employer 2</h3><h3>City, SS 2</h3><h3>Title 2</h3><ul><li>Testing level 2</li><li>Testing level 2</li><li>Testing level 2</li></ul></div>
Robert Walker
17,146 PointsRobert Walker
17,146 PointsTo be honest though I would just do this directly into the index page instead: