Jump to content

Adding Keys to Existing Array


Diegar
 Share

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

...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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...