split a huge array into fixed number of values arrays

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
User avatar
Da P3s7
Forum Commoner
Posts: 30
Joined: Wed Jul 19, 2006 3:25 pm
Location: /usr/src/kernels/ 2.6.15-1.2054_FC5-i686

split a huge array into fixed number of values arrays

Post by Da P3s7 »

I have this function

Code: Select all

function mysql_fetch_all($result) {
   $i = 0;
   for ($i=0; $i<mysql_num_rows($result); $i++) {
       $return[$i] = mysql_fetch_array($result);
   }
   return $return;
}
When i do a print_r on the mysql_fetch_all($select) where $select is the result from a select query i get

Code: Select all

Array ( [0] => Array ( [0] => 1 [Index] => 1 [1] => aa [Name] => aa [2] => bb [Description] => bb [3] => cc [Path] => cc [4] => 0 [Executed] => 0 [5] => 0 [Sold] => 0 [6] => 0 [Price] => 0 ) [1] => Array ( [0] => 2 [Index] => 2 [1] => ba [Name] => ba [2] => ab [Description] => ab [3] => c [Path] => c [4] => 1 [Executed] => 1 [5] => 2 [Sold] => 2 [6] => 3 [Price] => 3 ) [2] => Array ( [0] => 3 [Index] => 3 [1] => a [Name] => a [2] => b [Description] => b [3] => c [Path] => c [4] => 0 [Executed] => 0 [5] => 0 [Sold] => 0 [6] => 0 [Price] => 0 ) [3] => Array ( [0] => 4 [Index] => 4 [1] => ab [Name] => ab [2] => cb [Description] => cb [3] => db [Path] => db [4] => 12 [Executed] => 12 [5] => 32 [Sold] => 32 [6] => 42 [Price] => 42 ) [4] => Array ( [0] => 5 [Index] => 5 [1] => 200 caractere si speci8ale #@$#ET$^%%$^^* [Name] => ab [2] => ab [Description] => ba [3] => [Path] => [4] => 123 [Executed] => 123 [5] => 0 [Sold] => 0 [6] => 12.2 [Price] => 12.2 ) )
Is there a way that i can simply split up this array into smaller arrays that i can use, no matter the size of the DB ?....
I mean something like $result1, $result2 or something and auto-incrementing.... I would then use that to display just That row.
Would implode() do the job?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Something like this might work for you..

Code: Select all

foreach ($array as $record) {
  foreach ($record as $values) {

  }
}
User avatar
Da P3s7
Forum Commoner
Posts: 30
Joined: Wed Jul 19, 2006 3:25 pm
Location: /usr/src/kernels/ 2.6.15-1.2054_FC5-i686

Post by Da P3s7 »

umm i think that i dont need an array for each value...
and second i THINK that i need the arrays extracted to be static...(i think)
I say i think becouse i still havent written the code that will actually use this....
It's like a big puzzle in my mind...i seem to have the pieces but can't seem to make them fit together. :?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: split a huge array into fixed number of values arrays

Post by Benjamin »

Da P3s7 wrote: Is there a way that i can simply split up this array into smaller arrays that i can use, no matter the size of the DB ?....
Did I not answer your question?
User avatar
Da P3s7
Forum Commoner
Posts: 30
Joined: Wed Jul 19, 2006 3:25 pm
Location: /usr/src/kernels/ 2.6.15-1.2054_FC5-i686

Post by Da P3s7 »

Sorry. It's my mistake i didnt make myself clear.... :oops: :oops:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

You can access the rows without the foreach by doing this..

Code: Select all

print_r($result[$recordNumber]);


// and specific fields with..
echo $result[$recordNumber]['fieldName'];
echo $result[$recordNumber]['fieldName2'];
Post Reply