Problems adding to an array?

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
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Problems adding to an array?

Post by webcan »

Jcart | Please use

Code: Select all

and

Code: Select all

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]


OK, perhaps someone can help me out and let me know what I've done wrong here.

I run a SQL query, and then want to add the results to an array which is returned as the result of this function.

Here is my code:

Code: Select all

while ($row = mysql_fetch_array($result)) {
		$returnData["weight"][] = $row["id"];
		$returnData["weight"][$row["id"]]["display"] = $row["imperial"] ." / ". $row["metric"];
	}
I am trying to create an array that basically has elements like this:

$returnData["weight"]["1"]["3 lbs / 1 kg"] ... etc.

So, in my debugging, I noticed that if I do:

echo $returnData["weight"]["1"]["display"];

I get the proper result displayed (3 lbs / 1 kg). But, if I do anything but the first value, I get some weird number (like "9" or "1") that doesn't correspond to $row["imperial"] or $row["metric"] or anything else I can see.

I'm thinking the way I add to the $returnData array might not be right, but I don't know what would be off -- and if something is off, why is the first one right!?

Hope someone can help.

Thanks!


Jcart | Please use

Code: Select all

and

Code: Select all

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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You're laying your array out all wrong ;)

You want to keep the first keys at level 1 unique, or you'll just overwrite them with new values. I don;t see what the code is really doing with all the extra dimensions you've thrown in so I'm guessing you just got a bit confused :)

Something like:

Code: Select all

$returnData = array();

while ($row = mysql_fetch_array($result)) {
      $returnData[$row['id']] = array(
            'weight' => $row["imperial"] ." / ". $row["metric"],
            'display' => $something_else
      );
      
}
Now all the keys are the ID's, which of course are unique anyway.

Maybe I've misunderstood.
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

Thanks for the reply!

Hmmm - okay, I think I understand what you're getting at, but, there is a reason why I've added weight as the first level, perhaps I should have mentioned this in the original message.

There is other code in this function which is supposed to construct an array that will basically look like this:

$returnData["weight"]["1"]["3 lbs / 1 kg"]
$returnData["weight"]["2"]["6 lbs / 2 kg"]
...
$returnData["height"]["1"]["5'10 / 177 cm"]
$returnData["height"]["2"]["5'11 / 180 cm"]
...
$returnData["eyes"]["Brown"]
$returnData["eyes"]["Blue"]
...
$returnData["hair"]["Blond"]
$returnData["hair"]["Brown"]
...

So that's why I need to first identify which element I am adding data to.

Adding to the other elements (eyes / hair in the above example) is no problem, and it seems to work well. I am just having an issue with adding an array to height / weight.

I think your solution is right though, to declare an Array() for height / weight with each item, but how would I modify what you wrote to end up with the result shown above?

Thanks!!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

:? It doesn't really make any sense. Why would you have your array like that? It's not really got a fixed structure and you can't see where anything is. It looks like you're trying to use values as keys? The values should referenced by a certain key (the ID in this case). Just looks a bit back-to-front that's all :)

You can still pull out all of that data you need using a different array structure, you're just making it more complex than it needs to be here.
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

The reason I layed it out "this way" :D is because I later refer to this array to generate drop-down boxes, and I thought it would be easier to always reference a single array that would contain all of the data for all of the drop-downs, such as:

Code: Select all

function showDropDown($fieldName, $selectedValue) {
	global $dropDownData;
	
	$returnData = "<select name=\"select_".$fieldName."\">\n";
	
	switch($fieldName) {
		case "height":
		case "weight":
			foreach ($dropDownData[$fieldName] as $thisKey => $thisValue) {
				if ($thisValue == $selectedValue) {
					$returnData .= "<option value=\"".$thisValue."\" selected>".$dropDownData[$fieldName][$thisValue]["display"]."</option>\n";
				}
				else {
					$returnData .= "<option value=\"".$thisValue."\">".$dropDownData[$fieldName][$thisValue]["display"]."</option>n";
				}
			}
		break;
		
		default:
			foreach ($dropDownData[$fieldName] as $thisValue) {
				if ($thisValue == $selectedValue) {
					$returnData .= "<option value=\"".$thisValue."\" selected>".$thisValue."</option>\n";
				}
				else {
					$returnData .= "<option value=\"".$thisValue."\">".$thisValue."</option>\n";
				}
			}
	}

	$returnData .= "</select>\n";		
	
	return $returnData;
}
The reason for the complication (the difference between weight/height and eyes/hair) is because for all of the drop-down, except height and weight, the value in the form is identical to the text to be displayed as the option.
Post Reply