PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hey, Im new to PHP so my question should be pretty straight forward. Im trying to get a Multidimensional Array to show both Canadian and American Artist in a single table. But im having no such luck. Could someone point me in the right direction. Thanks
<html>
<head>
<title>Multidimensional Array"s</title>
</head>
<body>
<?php # Testing Multidimensional Arrays
//Creating the array loop that holds all the needed information
$artist_CDN = array (
1=> 'The Stills',
2=> 'Sam Roberts',
3=> 'Metric',
4=> 'Matthew Good',
5=> 'David Usher',
6=> 'Billy Talent'
);
$artist_US = array(
1=> 'Abandoned Pools',
2=> 'Incubus',
3=> 'The Killers',
4=> 'Phantom Planet',
5=> 'The Strokes',
6=> 'Deathcab for Cutie'
);
//Combining US and CDN
$bands = array ('CDN' => $artist_CDN, 'US' => $artist_US);
// Here I out put the the CDN artist Array.
echo '<p><pre><b>Canadian Bands:</b><br>';
asort($artist_CDN);
foreach ($artist_CDN as $key => $value) {
echo "<i>$value\n</i>";
}
echo '</pre></p>';
// Here I out put the the US artist Array.
echo '<p><pre><b>American Bands:</b><br>';
asort($artist_US);
foreach ($artist_US as $key => $value) {
echo "<i>$value\n</i>";
}
echo '</pre></p>';
//Here I create an Array and add both Countries together
echo '<p><pre><b>Both Canadian and American Bands:</b><br>';
asort($bands);
foreach ($bands['CDN'] as $key => $value) {
echo "<i>$value\n</i>";
}
echo '</pre></p>';
?>
</body>
</html>
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
//Here I create an Array and add both Countries together
echo '<p><pre><b>Both Canadian and American Bands:</b><br>';
asort($bands);
foreach ($bands['CDN'] as $key => $value) {
echo "<i>$value\n</i>";
}
echo '</pre></p>';
You aren't creating an array there nor are you adding the countries together.
You are simply accessing the CDN element of $bands and iterating the contents of that:
That worked out great, thank you. Im really not to sure how to use quotes i know there are a couple different ways but I guess im doing it right, so whatever im doing I'll keep at it. Thanks for the comment
One more quick question. Im trying to get my last grouping to alphabetically categorize itself. I can get one group to do it but not both, they always seem to be grouped together. Is there a way around that?
thanks again
<b>Also</b> Im VERY sorry how this is coming out... I used the [ php ] tags but they seem to format it different. Is it because I have the tabbed spaced in it? again sorry - I dont mean for this to look so bad
<html>
<head>
<title>Cody's Multidimensional Array's</title>
</head>
<body>
<?php # Testing Multidimensional Arrays
//Creating the array loop that holds all the needed information
$artist_CDN = array (
1=> 'The Stills',
2=> 'Sam Roberts',
3=> 'Metric',
4=> 'Matthew Good',
5=> 'David Usher',
6=> 'Billy Talent'
);
$artist_US = array(
1=> 'Abandoned Pools',
2=> 'Incubus',
3=> 'The Killers',
4=> 'Phantom Planet',
5=> 'The Strokes',
6=> 'Deathcab for Cutie'
);
//Combining US and CDN
$bands = array ('CDN' => $artist_CDN, 'US' => $artist_US);
// Here I out put the the CDN artist Array.
echo '<p><pre><b>Canadian Bands:</b><br>';
asort($artist_CDN);
foreach ($artist_CDN as $key => $value) {
echo "<i>$value\n</i>";
}
echo '</pre></p>';
// Here I out put the the US artist Array.
echo '<p><pre><b>American Bands:</b><br>';
asort($artist_US);
foreach ($artist_US as $key => $value) {
echo "<i>$value\n</i>";
}
echo '</pre></p>';
//Here I create an Array and add both Countries together
echo '<p><pre><b>Both Canadian and American Bands:</b><br>';
asort($bands);
foreach ($bands as $key => $group) {
foreach ($group as $key => $music) {
echo "<i>$music\n</i>";
}
}
echo '</pre></p>';
?>
</body>
</html>
<b>Also</b> Im VERY sorry how this is coming out... I used the [ php ] tags but they seem to format it different. Is it because I have the tabbed spaced in it? again sorry - I dont mean for this to look so bad
Fixed that for ya. I find it's best to replace each \t (tab) with 4 spaces for posting on these boards.
One more quick question. Im trying to get my last grouping to alphabetically categorize itself. I can get one group to do it but not both, they always seem to be grouped together. Is there a way around that?
The easiest way would be to combine your arrays into one 2d array and then sort it:
//Here I create an Array and add both Countries together
echo '<p><pre><b>Both Canadian and American Bands:</b><br>';
$bands = array_merge($artist_CDN, $artist_US);
asort($bands);
foreach ($bands as $key => $music) {
echo "<i>$music\n</i>";
}
echo '</pre></p>';
That worked out great, thank you. Im really not to sure how to use quotes i know there are a couple different ways but I guess im doing it right, so whatever im doing I'll keep at it. Thanks for the comment.
$foo = 4;
echo 'i have $foo'; // i have $foo
echo "i have $foo"; // i have 4
echo 'i have ' . $foo; // i have 4
// more escaping in double quotes too
echo 'hello\nworld'; // hello\nworld
echo "hello\nworld"; // hello {newline} world
// of course you can still escape single quotes in single quotes
echo 'i\'ve a big banana'; // i've a big banana