Welcome to WebmasterWorld Guest from 54.167.102.69

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Function to separate array created by mysql fetch array

     
3:52 pm on Dec 12, 2011 (gmt 0)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 0


Hi all,

I create an array using the $row = mysql_fetch_array($result,MYSQL_BOTH)function.

It's great because i can reference an element value using either the numeric or associative key. However, I need to pass only the numeric keyed portion of the array as a json string back to the client side.

Is there a PHP function that will separate numeric array from the associative array?

I checked the php site but could not find any so i thought i'd check with you all.

thanks.
4:25 pm on Dec 12, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


MYSQL_BOTH is actually the default, as if no 2nd argument is passed. Can you not simply pass MYSQL_NUM in order to return an array with only numeric indices, or do you need the column names elsewhere in your script?
1:20 am on Dec 13, 2011 (gmt 0)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 0


I use the associative indices to easily assign values to the elements without having to count or remember which numeric index it is. However, when i construct the json object before encoding it and sending it to the client side, i want to use the numeric key array.
7:44 am on Dec 13, 2011 (gmt 0)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
posts:408
votes: 0


This will do what you want:

while ($row = mysql_fetch_array($result)) {
ksort($row, SORT_STRING); //will order $row so all numeric indices are first
$tmp = array_slice($row, 0, count($row) / 2); //all the numeric indices from $row
//do something with $tmp
}
4:38 pm on Dec 13, 2011 (gmt 0)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 0


great, thanks. I'll look at the solution asap.

Happy Holidays!