• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

PHP using and referencing arrays

So i have my two examples below, the first one is working but what i really want is the second one but i cant seem to get it to work.
<?php
$section = array(
"first" => "its the first",
"second" => "its the second"
);

echo( $section["first"] ); /* this will print out 'its the first' What i really want is below but cant get it to work*/

?>

<?php
/* i cant get this to work but it is what i want */
$section = array(
"first" => ["its the first","im still first","3rd part of first"],
"second" => ["its the second","im still second","3rd part of second"]
);

echo( $section["first"][2] ); /* this should print out "3rd part of first"*/

?>

Open in new window

0
Brant Snow
Asked:
Brant Snow
3 Solutions
 
zappafan2k2Commented:
Try this
<?php
$section = array(
"first" => array("its the first","im still first","3rd part of first"),
"second" => array("its the second","im still second","3rd part of second")
);
?>

Open in new window

0
 
Ray PaseurCommented:
Some useful links to learn how PHP handles arrays:
http://php.net/manual/en/book.array.php
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/language.operators.array.php

You also want to know about foreach() and for() which are both useful with arrays.
http://php.net/manual/en/control-structures.foreach.php
http://php.net/manual/en/control-structures.for.php

Best of luck with it, ~Ray
0
 
Ray PaseurCommented:
I should have mentioned (but I forgot because I am drinking a very good single-malt) this idea...

An associative array can have one key pointing to one string value.  However with a little cleverness you can have an associative (or numerically indexed) array of objects.  Since the objects can carry much more information than a string can carry, this becomes a powerful programming construct.

You can also have arrays of "sub-arrays" and that appears to be what you want in this question.

Best regards from the lounge, ~Ray
0
 
Chris HarteThaumaturgeCommented:
I took your square brackets out because they were not working. This leaves you with this array

Key = [first]   -- Value = its the first
Key = [0]       -- Value = im still first
Key = [1]       -- Value = 3rd part of first
Key = [second]  -- Value = its the second
Key = [2]       -- Value = im still second
Key = [3]       -- Value = 3rd part of second

That is an array numbered 0 to 3 with two defined key values. Using zappafan's arrays will give you (see code).

Key = [first] its the first : im still first : 3rd part of first :
Key = [second] its the second : im still second : 3rd part of second :


Remember, arrays number from 0 not 1.
$section = array("first" => array("its the first","im still first","3rd part of first"),
				 "second" => array("its the second","im still second","3rd part of second"));

echo "<pre>";

foreach ($section as $key => $value)
{    
    echo "Key = [$key]";

    foreach ($value as $out)
    {
        echo " $out :";
    }
    echo "<br />";   
}

Open in new window

0
 
Brant SnowAuthor Commented:
great
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now