Multidimensional Array Help

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!

Moderator: General Moderators

Post Reply
Spot
Forum Newbie
Posts: 6
Joined: Tue Jul 18, 2006 12:30 pm

Multidimensional Array Help

Post by Spot »

Weirdan | Please use

Code: Select all

,

Code: Select all

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

Code: Select all

<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>

Weirdan | Please use

Code: Select all

,

Code: Select all

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]
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

is it outputting array() a bunch of times?

EDIT: Oh... and php bbcode tags work better for syntax highlighting... FYI
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

//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:

Code: Select all

$domestic = array('dog', 'cat', 'parrot');
$farm = array('cow', 'pig', 'sheep');
$animals = array('domestic' => $domestic, 'farm' => $farm);

// this foreach is...
foreach ($domestic as $key => $species);

// ...equivalent, in this case, to this one...
foreach ($animals['domestic'] as $key => $species);
What you can of course do is this:

Code: Select all

foreach ($animals as $animalType) {
    foreach ($animalType as $species) {
        echo $species . '<br />';
    }
}
Which will output domestic animals followed by farm animals. Would this satisfy you needs?
Hey, Im new to PHP
Well let me commend you, it is nice to see someone who understand the difference between double and single quotes.
Spot
Forum Newbie
Posts: 6
Joined: Tue Jul 18, 2006 12:30 pm

Post by Spot »

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 :?

Code: Select all

<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>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

<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:

Code: Select all

//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>';
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

FYI: you don't need to assign numerical array keys (you can if you want, but not much point). Array assignment could have looked like this:

Code: Select all

$artist_US = array(
                                'Abandoned Pools',
                                'Incubus',
                                'The Killers',
                                'Phantom Planet',
                                'The Strokes',
                                'Deathcab for Cutie'
                            );
Spot
Forum Newbie
Posts: 6
Joined: Tue Jul 18, 2006 12:30 pm

Post by Spot »

Got it working!! :lol: thanks guys! all of you were a great help! Great forms , great people!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

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.
Single quote are literal, double quotes not so.

Code: Select all

$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
Post Reply