Jump to content

Adding Keys to Existing Array


Diegar

Recommended Posts

If i pull data from the db like this: $getTopArray = mysql_fetch_array($getTopResults) or Die (mysql_error());...it seems to pull everything into the array without setting a key to each item. What i want, is to have the first item assigned to 0, the second item 1, and so on.Is there a way to add keys to pre-existing arrays? Even better, is there a modification i could do to that line i already have to add keys automatically?I am new to arrays, so sorry if this is a dumb question.

Link to comment
Share on other sites

Every element in an array has a key. You can't have a value without a key. If you want to see the structure of the array, you can use this:var_dump($getTopArray);Make sure to view the browser source code instead of what the browser shows.

Link to comment
Share on other sites

...it seems to pull everything into the array without setting a key to each item. What i want, is to have the first item assigned to 0, the second item 1, and so on.
This is actually the default behaviour of returned arrays.Arrays can't exist without keys. If a key is not explicitly given, PHP assigns the smallest available number, starting from 0. $getTopArray[0] should already return the first result of the $getTopResults query.If you wanted to assign a custom key to an existing array, you'd just do for example $getTopResults['stringKey'] or $getTopResults[10] (for numeric key).
Link to comment
Share on other sites

I went over arrays in the PHP manual. I am not finding my answer, i don't think, on how to truncate my array.When i use:$getTopArray = mysql_fetch_array($getTopResults) or Die (mysql_error());.. it grabs everything from 'user', including username and password and location, etc...It also grabs the data for the 'name' fields, (ie 'name1', 'name2', 'name3' and 'name4').I am looking for a way to remove the first 6 lines in the array (0 - 5) and then everything after 'name4' (10 - 29).I tried going this route:

$r=0;while ($r < 6)	{	unset($getTopArray[$r]);	$r++;	}$noTopArray = array_values($getTopArray);$r=4;while ($r < 26)	{	unset($noTopArray[$r]);	$r++;	}$getTopArray = array_values($noTopArray);$r=0;while ($r <= 3)	{	print $getTopArray[$r];	echo "<br>";	$r++;	}

But it only seems to show me the first 4 lines of the array that were already there to begin with. And.. it even truncated the middle of the array, leaving blank values for keys, and left some of the data at the end of the array, giving them larger key numbers.I know i am messing up bad, but i don't know where.

Link to comment
Share on other sites

Well, you can use the array_shift function to remove an element from the beginning of an array, and you can use array_pop to remove an element from the end. You can also use unset to remove any individual element if you know the key. But in this case it would probably be better to modify the SQL statement to only return the columns you're interested in instead of everything. Also, I prefer to use mysql_fetch_assoc instead of mysql_fetch_array because mysql_fetch_assoc leaves off the numbered indexes and only includes fields by name.

Link to comment
Share on other sites

mysql_fetch_array($result, MYSQL_NUM);should do what you want with numeric indexes.Also, for the other thing, why not just use SQL to eliminate those values?

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...