Help with arrays from database result

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
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Help with arrays from database result

Post 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.
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: Help with arrays from database result

Post 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.
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

Re: Help with arrays from database result

Post 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().
Post Reply