Jump to content

getting first lvl $key from multidim arrays


daemondeitie

Recommended Posts

hey (php)freaks, need a little help here...

i'm having trouble with getting the $key from my $concept variable. for instance, if i have $concept[1], i'd like the number 1 printed. for concept[2] i'd like the number 2, etc... check the code...

(please excuse the funky formating)
[code]
<?php
$concept[1] = array("blamedemand.jpg" => "take responsibility for your actions",
"inplaceof1.jpg" => "we're still only human",
"snwo2.jpg" => "concept art for a concept music project",
"headspace.jpg" => "the final solution is just that, final. think about it first.",
"sosorry.jpg" => "why does it have to be this way?",
"war.jpg" => "what is it here for?"
);
$concept[2] = array("areyouhilo.jpg" => "that perfect place",
"prognosis2.jpg" => "sometimes i see and feel too much",
"tvradio.jpg" => "stop letting tv teach our children",
"meeowRed.jpg" => "bad kitty! no!",
"indescribable.jpg" => "coffee mug. it's how i feel in the morning",
"246001.jpg" => "splatter brushes are neat"
);
$concept[3] = array("theone.jpg" => "for natalie. she's the one.",
"canvas.jpg" => "all my personalities are individuals",
"thesilence.jpg" => "i don't want to stay if you go",
"mannequin.jpg" => "aye! ball.",
"cherub.jpg" => "hugs are nice"
);


foreach ($concept as $key){
if(is_array($key)){
echo "<br />$key <br /><br />\n";

foreach ($key as $key2 => $value) {
echo "$key2 => $value <br />\n";

}
}

}

?>
[/code]

here are the results:

[quote]
Array

blamedemand.jpg => take responsibility for your actions
inplaceof1.jpg => we're still only human
snwo2.jpg => concept art for a concept music project
headspace.jpg => the final solution is just that, final. think about it first.
sosorry.jpg => why does it have to be this way?
war.jpg => what is it here for?

Array

areyouhilo.jpg => that perfect place
prognosis2.jpg => sometimes i see and feel too much
tvradio.jpg => stop letting tv teach our children
meeowRed.jpg => bad kitty! no!
indescribable.jpg => coffee mug. it's how i feel in the morning
246001.jpg => splatter brushes are neat

Array

theone.jpg => for natalie. she's the one.
canvas.jpg => all my personalities are individuals
thesilence.jpg => i don't want to stay if you go
mannequin.jpg => aye! ball.
cherub.jpg => hugs are nice
[/quote]

what i need is for where it says "Array" for it to say the array number.

does this make sense?

thank you :)
This will not work
[code]foreach ($concept as $key){[/code]

just beause you use a variable with the name key does not meant that it will be assigned to that variable. When including a single variable in a foreach loop the "value" of each index will be associated with the variable.

You need to use something like this:
[code]foreach ($concept as $foo => $bar){[/code]
And $foo will be the key for each index. There is a more direct method of accessing the keys, but I can't come up with it at the moment.

EDIT: As soon as I checked I found it
[code]foreach (array_keys($concept) as $key) {[/code]
If you plan to always go in order:

[code]
[code=php:0]
<?php
$concept[1] = array("blamedemand.jpg" => "take responsibility for your actions",
"inplaceof1.jpg" => "we're still only human",
"snwo2.jpg" => "concept art for a concept music project",
"headspace.jpg" => "the final solution is just that, final. think about it first.",
"sosorry.jpg" => "why does it have to be this way?",
"war.jpg" => "what is it here for?"
);
$concept[2] = array("areyouhilo.jpg" => "that perfect place",
"prognosis2.jpg" => "sometimes i see and feel too much",
"tvradio.jpg" => "stop letting tv teach our children",
"meeowRed.jpg" => "bad kitty! no!",
"indescribable.jpg" => "coffee mug. it's how i feel in the morning",
"246001.jpg" => "splatter brushes are neat"
);
$concept[3] = array("theone.jpg" => "for natalie. she's the one.",
"canvas.jpg" => "all my personalities are individuals",
"thesilence.jpg" => "i don't want to stay if you go",
"mannequin.jpg" => "aye! ball.",
"cherub.jpg" => "hugs are nice"
);

$keycount = 0;
foreach ($concept as $key){
if(is_array($key)){
$keycount++;
echo "<br />$keycount <br /><br />\n";

foreach ($key as $key2 => $value) {
echo "$key2 => $value <br />\n";

}
}

}

?>
[/code]
[/code]
mjdamato : i'm having a really hard time implementing the [color=blue]array_keys()[/color] solution following the my orginal format. but i'm going to keep trying. i'll post an update.

rotwyla98: thanks for the idea but i will need to change the keys to "titles" later on. ex: [color=blue]$concept[[color=maroon]'example section'[/color]];[/color].

thanks guys :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.