homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Function to separate array created by mysql fetch array

 3:52 pm on Dec 12, 2011 (gmt 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.




 4:25 pm on Dec 12, 2011 (gmt 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)

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)

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)

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

Happy Holidays!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved