Page 1 of 1

Help with arrays from database result

Posted: Sun Apr 25, 2010 7:43 am
by gammaman
I have an array that holds the result from a database select. The array always changes size depending on the number of rows. What I am trying to do is take every sixth index ( [0] - [5], [6] - [11]) etc. And put those in there own array. I then want to use array_diff to compare each of them with another array
of six index. The problem is I never know how large the origninal array will be since I do not know now many rows will be returned.

Re: Help with arrays from database result

Posted: Sun Apr 25, 2010 11:38 am
by davex
Hi,

If I understand you correctly then you can just use the count() function to find out how many elements are in an array and then loop through.

Code: Select all

<?php
$first_array = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n");
$second_array = array();
for ($i=0; $i<count($first_array); $i+=6)
 {
 $second_array[] = $first_array[$i];
 }
?>
This should do elements 0, 6, 12 etc... in this case a, g, m (I think from just looking at the code).

Cheers,

Dave.

Re: Help with arrays from database result

Posted: Sun Apr 25, 2010 12:25 pm
by gammaman
Not really what I mean. I want to store elements 0-5 in second array(). Then comare second array to another array say array3 using array_diff(). Then I want to go back to first array and grab the next 6 elements 6-11 and put those in second array() and again using array3 compare using array_diff().