complex multidimensional array in php -- how to retrieve the data?
Sample of array:
$pictures = array (
"tongue" => array (
"id" => "231529i358293",
"title" => "New Year's Opinion",
"category" => "Joshua"),
"bighands" => array (
"id" => "231529i353027",
"title" => "Big Hands I Know You're The One",
"category" => "Joshua"));
What I'm looking to do is this:
Loop through the array and return the content in this format:
$pictures[$puzzle][$category]
then for each item in that category, return:
I know this will require nested while loops, but I'm not sure about how to get it to work right. The examples don't have mulitdimensional arrays this complex (is there an easier way?) and only talk about
(list ($key2, $val) = each ($array["$key1"]))
Which returns the value $val fine, but I'm looking for more than that?
I'm just confused right now -- can someone just point me in the right direction or to a good resource on the matter that has examples as complex as my current data?
Thanks!
Suzanne posted this at 20:51 — 12th May 2002.
She has: 5,507 posts
Joined: Feb 2000
I'm also a bit confused on the correct way to write an array. It looks like the above is valid, but so is:
$pictures = array (
"tongue" => array ("231529i358293",
"New Year's Opinion",
"Joshua"),
"bighands" => array ("231529i353027",
"Big Hands I Know You're The One",
"Joshua"));
?
Suzanne posted this at 22:54 — 12th May 2002.
She has: 5,507 posts
Joined: Feb 2000
okay, what I've done is rearrange the multidimensional array (re-array-nge, ha ha) so that it's like this:
<?php
$pictures = array (
\"category\" => array (
\"name\" => array (
\"id\" => \"value\",
\"title\" => \"value\"),
\"name\" => array (
\"id\" => \"value\",
\"title\" => \"value\")),
\"category\" => array (
\"name\" => array (
\"id\" => \"value\",
\"title\" => \"value\"),
\"name\" => array (
\"id\" => \"value\",
\"title\" => \"value\")));
?>
And I have this much working -- I can pull the categories and return that value, and a list of all images in each category, but I can't get the id or title values for some reason. I know I'm just not asking for them correctly.
<?php
while (list ($key1) = each ($pictures)) {
<h3> echo $key1; </h3>
<p>
while (list ($key2, $val) = each ($pictures[\"$key1\"])) {
// get the right puzzle information
$id = ; //<strong>what goes here?</strong>
$title = ; //<strong>what goes here?</strong>
$ui = $id;
<a href=\"puzzle2.php?show=textlinks&puzzle=
echo $key2; #go\"> echo $title; [link]</a>
<br />
}
</p>
}
?>
Any help at all would be appreciated. (Not all the code is here, just the part I'm having trouble with. You can see the results here: carterjackson.com/joshua/puzzle2.php
Mark Hensler posted this at 23:29 — 12th May 2002.
He has: 4,048 posts
Joined: Aug 2000
try this:
<?php
$pictures = array (
\"category\" => array (
\"name\" => array (
\"id\" => \"value\",
\"title\" => \"value\"),
\"name\" => array (
\"id\" => \"value\",
\"title\" => \"value\")),
\"category\" => array (
\"name\" => array (
\"id\" => \"value\",
\"title\" => \"value\"),
\"name\" => array (
\"id\" => \"value\",
\"title\" => \"value\")));
foreach ($pictures as $category=>$data) {
echo \"<h3>$category</h3>\n\";
echo \"<p>\n\";
foreach ($data as $name=>$subdata) {
extract($subdata);
// now you can use $id and $title
echo \"<a href=\\"puzzle2.php?show=textlinks&puzzle=$name#go\\">$title [link]</a>\n\";
echo \"<br />\n\";
} //END foreach ($data
echo \"</p>\n\";
} //END foreach ($pictures
?>
Mark Hensler
If there is no answer on Google, then there is no question.
Suzanne posted this at 23:32 — 12th May 2002.
She has: 5,507 posts
Joined: Feb 2000
that looks a lot simpler, and somewhat more direct as well. I'll go give that a shot. Thanks, Mark!
Suzanne posted this at 23:38 — 12th May 2002.
She has: 5,507 posts
Joined: Feb 2000
Sweet! It works like a charm!
So extract(); basically sets up $key/$value pairs for things in that array (the deepest array)? I think I follow it.
carterjackson.com/joshua/puzzle2.php to see that it's working. Eventually the finished version will be at puzzle.php and will have a drop-down list of the various puzzle options. Slowly but surely.
Someday I would like to add an admin section, too, lol, but one step at a time as I use this to learn how to put together php. Thanks again, Mark!
Mark Hensler posted this at 23:55 — 12th May 2002.
He has: 4,048 posts
Joined: Aug 2000
Looking good!
extact() sets up $key=value pairs for the array passed. If you gave it extract($data), I believe it would give you $name=array[] (with name being whatever the key was).
Mark Hensler
If there is no answer on Google, then there is no question.
Suzanne posted this at 00:16 — 13th May 2002.
She has: 5,507 posts
Joined: Feb 2000
oh, shoot, now I can't get the $id to pass to the actual puzzle. shoot shoot shoot. I need the name and the id to be linked. I'll have to think about how to do that.
Suzanne posted this at 07:10 — 13th May 2002.
She has: 5,507 posts
Joined: Feb 2000
http://www.carterjackson.com/joshua/puzzle2.phps
I think I'm going to have to rearrange how I get the information. As it is now, I can get ALL the puzzles to load at once, or just the last one, instead of as chosen.
Phooey. I think I need to rethink how it goes together. Suggestions?
Mark Hensler posted this at 07:28 — 13th May 2002.
He has: 4,048 posts
Joined: Aug 2000
try this:
<?php
if ($n == \"\") { Please choose a puzzle below. }
blah blah blah
foreach ($pictures as $category=>$data) {
echo \"<h3>$category</h3>\n\";
echo \"<p>\n\";
foreach ($data as $name=>$subdata) {
extract($subdata);
// now you can use $id and $title
echo \"<a href=\\"puzzle2.php?show=textlinks&c=$category&n=$name#go\\">$title [link]</a>\n\";
echo \"<br />\n\";
} //END foreach ($data
echo \"</p>\n\";
} //END foreach ($pictures
// blah blah blah
// code for the jigzone puzzle itself, and an unload link
if ($n != \"\") {
//$pictures[$c][$n][id]
//$pictures[$c][$n][title]
$name = $n;
$ui = $pictures[$c][$n][id];
echo $ui;
// blah blah blah
// removed two closing braces for the foreach's
} //END if
?>
Mark Hensler
If there is no answer on Google, then there is no question.
Suzanne posted this at 07:45 — 13th May 2002.
She has: 5,507 posts
Joined: Feb 2000
Thanks, Mark. Wow, that looks totally different. I think I got beyond my skill level rapidly on this one.
carterjackson.com/joshua/puzzle.php I did and it has two arrays -- one for the puzzle name and ID (jigzone's id number) and one for the possible cuts of the puzzle. The links, though, were hard coded.
So I thought, well, I'll just kick this up a notch... ha.
I'll see if I understand this in the morning, thanks very much, Mark, I appreciate your taking the time with this. My intention is to make it available once I get it done (if I get it done) for other people to be able to integrate multiple puzzles easily into their sites.
Too big a leap! Doh!
Suzanne posted this at 07:48 — 13th May 2002.
She has: 5,507 posts
Joined: Feb 2000
Reading it over, I think I see where I was missing the point. Too much coding tonight and learning too much. Though as a plus, I can find my way around php.net easily now. MAN there are a lot of functions and 1400 ways to do everything.
Suzanne posted this at 20:57 — 13th May 2002.
She has: 5,507 posts
Joined: Feb 2000
k, still not sure why you can just name things willynilly, but I replaced the $n with $puzzle (and n with puzzle) to see if I understood how it went together, and it works! And I sort of understand why I need to have the category in there (because of the nested array, right?), so I'm good with that.
Thanks very much Mark!
Currently viewable at carterjackson.com/joshua/puzzle2.php anyone who wants to see it in action.
Wil posted this at 21:25 — 13th May 2002.
They have: 601 posts
Joined: Nov 2001
Do you call those things arrays in PHP? Something looking like that would be a hash table in Perl :-\
Mark Hensler posted this at 22:19 — 13th May 2002.
He has: 4,048 posts
Joined: Aug 2000
I'm guessing...
PHP = associative array
PERL = hash table
Suzanne posted this at 23:25 — 13th May 2002.
She has: 5,507 posts
Joined: Feb 2000
That's a multidimensional array, I'll have you know, and according to my Big Book of Computer Languages (otherwise known as Wrox's Professional PHP), it's specifically a three-dimensional array (level of nesting).
Actually, now that I know how, roughly, to access the information, I find it easier to conceptualize than hashes. And I need to conceptualize in order to build things.
Wil posted this at 08:51 — 14th May 2002.
They have: 601 posts
Joined: Nov 2001
Yeah, I guess a multidimensional array = hash in Perl. I think, anyway. Now you've got me all confused :-\
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.