/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP Expanding 2 dimensional array to 3 dimensional array.

Hi, I’ve watched several videos on PHP multi-dimensional arrays hoping to find one similar to what I have so I can see how to expand it from a 2D to a 3D but none of the examples in the videos were coded like this one. I want to change this one from 2D to 3D. Thank you for taking a look.

[code=php]<?php
$meat = array (‘beef’, ‘mutton’, ‘poultry’, ‘venison’);
$fruits = array (‘apples’, ‘oranges’, ‘peaches’, ‘pears’);
$veggies = array (‘potatoes’, ‘carrots’, ‘turnips’, ‘beets’);
$dairy = array (‘yogurt’, ‘curds’, ‘whey’, ‘ice cream’);
$grain = array (‘barley’, ‘rice’, ‘oats’, ‘maize’);

$food = array ($meat, $fruits, $veggies, $dairy, $grain);

echo “<table border=”1″>n”;
echo ” <tr><th>Meats</th><th>Fruits</th><th>Veggies</th><th>Dairy</th><th>Grains</th></tr>n”;
for ($i = 0; $i <= 3; $i++) { // rows
echo ” <tr>”;
for ($j = 0; $j <= 4; $j++) { // columns
echo “n <td>”;
echo $food[$j][$i]; // cells
echo “</td>”;
}
echo “n </tr>n”;
}
echo “</table>n”;
?>[/code]

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 19.2016 — I might try to do it something along these lines:
[code=php]
<?php

$food = array (
'Meats' => $meat,
'Fruits' => $fruits,
'Veggies' => $veggies,
'Dairy' => $dairy,
'Grains' => $grain
);
?>
<table>
<tr>
<?php
foreach(array_keys($food) as $type) {
echo "<th>$type</th>";
}
?>
</tr>
<?php
foreach($food as $type) {
echo "<tr><td>".implode("n", $type)."</td></tr>n";
}
?>
</table>
[/code]

(untested)
Copy linkTweet thisAlerts:
@BlondieCauthorFeb 20.2016 — Thank you for your help. I tested it and it didn't work so I'm still working on it.
Copy linkTweet thisAlerts:
@NogDogFeb 21.2016 — It might be nice to tell explain "didn't work" (error, no output, wrong output, etc.).

Anyway, here's a tested version, adding the original array definitions that I left out under the assumption it was understood they were still needed, along with moving the <tr> tags for the data row:
[code=php]
<?php
$meat = array ('beef', 'mutton', 'poultry', 'venison');
$fruits = array ('apples', 'oranges', 'peaches', 'pears');
$veggies = array ('potatoes', 'carrots', 'turnips', 'beets');
$dairy = array ('yogurt', 'curds', 'whey', 'ice cream');
$grain = array ('barley', 'rice', 'oats', 'maize');

$food = array (
'Meats' => $meat,
'Fruits' => $fruits,
'Veggies' => $veggies,
'Dairy' => $dairy,
'Grains' => $grain
);
?>
<table>
<tr>
<?php
foreach(array_keys($food) as $type) {
echo "<th>$type</th>";
}
?>
</tr>
<tr>
<?php
foreach($food as $type) {
echo "<td>".implode("<br>n", $type)."</td>n";
}
?>
</tr>
</table>
[/code]
Copy linkTweet thisAlerts:
@BlondieCauthorFeb 26.2016 — Hi NogDog,

You are right - I should have indicated or pasted the returned output instead of just saying it didn't work. I think I was beyond tired when I came back on here. I've tried your above code and it does run fine and gives error-free output.

What I am trying to do to make it 3D is for example with the Fruits add three different types of each fruit i.e. Apples - delicious, green, mac, Oranges - blood, mandarin, navel etc. What I can't get right is the loop, within the loop within the loop.
Copy linkTweet thisAlerts:
@NogDogFeb 26.2016 — Well...the looping part doesn't have to be too bad, but the display can get tricky: 3 dimensions on a 2-dimensional screen. ?

Putting that aside, it comes down to a foreach within a foreach (within a foreach...). However, even that gets messy if not every member of the first dimension goes as many dimensions deep; in which case you start talking about some sort of recursive function to deal with it. With that in mind, here's an approach that does not need to know ahead of time the depth of the array, or even if the depth is consistent throughout:
[code=php]
<?php

$food = array(
'meat' => array(
'pork' => array(
'ham' => 3.55,
'bacon' => 4.99,
'loin' => 5.95
),
'beef' => array(
'sirloin' => 6.99,
'tbone' => 7.95,
'chuck' => 6.49
)
),
'fruit' => array(
'apple' => array(
'delicious' => .59,
'granny smith' => .67
),
'pineapple' => 1.49,
'grape' => array(
'seedless' => array(
'green' => 2.50,
'red' => 2.30
),
'seeded' => array(
'green' => 2.30,
'red' => 2.25,
'concord' => 3.00
)
)
)
);

function arrayToList($array) {
$output = "<ul>n";
foreach($array as $key => $value) {
$output .= "<li>$keyn";
if(is_array($value)) {
$output .= arrayToList($value);
}
else {
$output .= ": $$valuen";
}
$output .= "</li>";
}
$output.= "</ul>n";
return $output;
}

echo arrayToList($food);
[/code]
Copy linkTweet thisAlerts:
@jedaisoulFeb 26.2016 — What I am trying to do to make it 3D is for example with the Fruits add three different types of each fruit i.e. Apples - delicious, green, mac, Oranges - blood, mandarin, navel etc. What I can't get right is the loop, within the loop within the loop.[/QUOTE]
Don't you think you should have explained this in the first post? Leaving out key info does not help people to help you... ?
Copy linkTweet thisAlerts:
@BlondieCauthorFeb 26.2016 — I absolutely should have. I shouldn't have posted when I could hardly keep my eyes open and not think straight. I need to learn when to call it a night and sleep.
Copy linkTweet thisAlerts:
@NogDogFeb 26.2016 — I absolutely should have. I shouldn't have posted when I could hardly keep my eyes open and not think straight. I need to learn when to call it a night and sleep.[/QUOTE]

Or more energy drinks and less sleep. ?
Copy linkTweet thisAlerts:
@BlondieCauthorMar 02.2016 — Well...the looping part doesn't have to be too bad, but the display can get tricky: 3 dimensions on a 2-dimensional screen. ?

Putting that aside, it comes down to a foreach within a foreach (within a foreach...). However, even that gets messy if not every member of the first dimension goes as many dimensions deep; in which case you start talking about some sort of recursive function to deal with it. With that in mind, here's an approach that does not need to know ahead of time the depth of the array, or even if the depth is consistent throughout:
[code=php]
<?php

$food = array(
'meat' => array(
'pork' => array(
'ham' => 3.55,
'bacon' => 4.99,
'loin' => 5.95
),
'beef' => array(
'sirloin' => 6.99,
'tbone' => 7.95,
'chuck' => 6.49
)
),
'fruit' => array(
'apple' => array(
'delicious' => .59,
'granny smith' => .67
),
'pineapple' => 1.49,
'grape' => array(
'seedless' => array(
'green' => 2.50,
'red' => 2.30
),
'seeded' => array(
'green' => 2.30,
'red' => 2.25,
'concord' => 3.00
)
)
)
);

function arrayToList($array) {
$output = "<ul>n";
foreach($array as $key => $value) {
$output .= "<li>$keyn";
if(is_array($value)) {
$output .= arrayToList($value);
}
else {
$output .= ": $$valuen";
}
$output .= "</li>";
}
$output.= "</ul>n";
return $output;
}

echo arrayToList($food);
[/code]
[/QUOTE]


That looks great and is what I was trying to do. I understand it better the way you have it coded, compared to my original posted code. In the output when I look at it on my browser, there are three types of bullets. Is it the arrayToList that defaults the bullets? Thank you!
Copy linkTweet thisAlerts:
@NogDogMar 02.2016 — It's actually the browser's rendering of nested <ul> lists, using different bullet characters for different levels' <li> elements. If you don't like them, you can use CSS styling to eliminate them, and possibly change them (not sure, I'm not a CSS guru ? ).

PS: To elaborate a bit to what you might actually have been asking, it is that arrayToList() function that is rendering the array into a nested list via HTML mark-up (and then the browser that renders the different bullet styles and indents for different levels of nesting).
Copy linkTweet thisAlerts:
@BlondieCauthorMar 02.2016 — It's actually the browser's rendering of nested <ul> lists, using different bullet characters for different levels' <li> elements. If you don't like them, you can use CSS styling to eliminate them, and possibly change them (not sure, I'm not a CSS guru ? ).

PS: To elaborate a bit to what you might actually have been asking, it is that arrayToList() function that is rendering the array into a nested list via HTML mark-up (and then the browser that renders the different bullet styles and indents for different levels of nesting).[/QUOTE]


Thank you for explaining. I like the bullets showing as it makes it very clear to see the arrays.
×

Success!

Help @BlondieC spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...