views:

67

answers:

4

Let's say I would like to manage a multidimensional array like (pseudo-code):

Array $colors
* wine,red
* cheese,yellow
* apple, green
* pear,brown

What code could be used to avoid the following notation, to initialize the array (assuming there will be a hard-coded list of elements=?:

$colors[x][y] = 'something';
+1  A: 

Just take a look : http://php.net/manual/en/language.types.array.php ;)

MatTheCat
I did mate.... I need more practice! ;-)
Riccardo
+2  A: 
Alexander.Plutov
Alexander I need to access both items from each element... By coding $array['wine'] it would be possible to access 'red'.... how would you access 'wine', in a foreach loop?
Riccardo
Riccardo
Sorry. See my changes.
Alexander.Plutov
`foreach($array AS $left => $right) echo $left . ' - ' . $right`
faileN
Great! Will use that! Smart!
Riccardo
@faileN. Your code for my previous version.
Alexander.Plutov
Alexander, thanks! faileN syntax seems smarter! Kudos to everyone
Riccardo
This was my original answer, but then I changed it =(
Rocket
I had no intention to steal anything, of anybody :P
faileN
A: 
$colors = array(
    array('wine' => 'red',
        'cheese' => 'yellow',
        'apple' => 'green',
        'pear' => 'brown'
    )
);
Rocket
The first array is superfluous.
+3  A: 

Assuming you don't want an associative array as your question doesn't mention it.

This is the elegant syntax PHP makes available:

<?php
$colors = array(array("wine","red"),
                array("cheese","yellow"),
                array("apple", "green"),
                array("pear", "brown"));

print_r($arr); // Prints out an array as shown in output
?>

Output:

    Array
(
    [0] => Array
        (
            [0] => wine
            [1] => red
        )

    [1] => Array
        (
            [0] => cheese
            [1] => yellow
        )

    [2] => Array
        (
            [0] => apple
            [1] => green
        )

    [3] => Array
        (
            [0] => pear
            [1] => brown
        )

)

To loop over access all the 0's:

for($x = 0; $x < count($colors); $x++){
    echo $colors[$x][0];
}

Alternatively

for($colors as $couple){
   echo $couple[0];
}

EDIT: It seems like you actually could be better of with an associative though..

$colors = array("wine"   => "red",
                "cheese" => "yellow",
                "apple"  => "green",
                "pear"   => "brown");

Cause you can still access the keys, as such:

 for($colors as $key => $value){
       echo $key . " is " . $value;
    }
Alexander Sagen
Nice sample, it shows how to initialize the array, however I'm still missing how to access the "left column" in a foreach loop, once the array has been initialized...
Riccardo
Updated with a loop example..
Alexander Sagen
I hoped I could avoid the for loop and just use the foreach... Good anyhow! Thanks – Riccardo 0 secs ago
Riccardo
Added some more, you can access key using associative arrays, as shown.
Alexander Sagen
Thanks again! Great thread for rookies!
Riccardo
I gave the accepted answer to Plutov and faileN just because they have answered before you did complete your answer, although your answer seems more detailed... thanks! :-)
Riccardo
No worries, glad to help :)
Alexander Sagen