• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

Php array intersection: how to combine two loops

 Hi all,
 
  Here a learner, with the following question:
 
  I need to do:
 
   $intersection = array_intersect($array1, $array2);

Open in new window

 
   I obtain $array1:
   
          for( $i = 0; $i < count ($myarray1['data']); $i ++)
          {
               $array1 = $myarray1['data'][$i]['place']['id']; 
               
          } 

Open in new window

         
    And I obtain $array2 from database:
   
    while($row = mysql_fetch_array( $result )) {

	$array2 = $row['name'];

} 

Open in new window



  But when doing:
    $intersection = array_intersect($array1, $array2);

Open in new window

   outside of the loops, the variables contain the last record obtained in the loop ..
   
    What do I have to do for $array1 and $array2 to contain all the data looped?
   
    Thanks a ton
0
Dada44
Asked:
Dada44
  • 5
  • 4
1 Solution
 
Beverley PortlockCommented:
This

    while($row = mysql_fetch_array( $result )) {

      $array2 = $row['name'];

    }

Means that array2 only contains the last row retrieved by mysql_fetch_array. If you wanted to keep all the data retrieved by that loop you would need to modify it like so

    while($row = mysql_fetch_array( $result )) {

      $array2 [] = $row['name'];

    }

but this causes you a second problem because the PHP array functions usually only operate on single dimension arrays, not multi dimensional arrays. I think you need to rethink your appraoch to this problem. Can you tell us more about it?
0
 
Dada44Author Commented:
Thanks bportlock.

Here's what I'm trying to do:
I have two arrays.

$array1 contains the ids of the places the user has been to.

$array2 contains the ids of the places where the user can obtain one point just by visiting them

Now I need to know if the user has visited any of the places where he/she can get one point so I can grant it to him/her. How can I do that?

Tnaks a lot
0
 
Beverley PortlockCommented:
Actually, I think that last comment of min is incorrect. Try using

      $array2 [] = $row['name'];

how does that work out?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Dada44Author Commented:
It does not work, printing array2 in the loop without []  shows the values but with the []  it does not print anything :(
0
 
Beverley PortlockCommented:
OK, I have assumed from the names that $array1 and $array2 are actually arrays. That they have been declared somewhere as

$array1 = array();
$array2 = array();

so just before this

$intersection = array_intersect($array1, $array2);

add these lines like so

echo "<pre>";
print_r( $array1 );
print_r( $array2 );
echo "</pre>";
$intersection = array_intersect($array1, $array2);

What shows up? Part of my confusion comes from the fact that you are assigning values to the variables $array1 and array2 as if they were non-array variables.
0
 
Dada44Author Commented:
that is working great when the arrays are like:
$array1 = array('1','2','3','4','5');
$array2 = array('3','4','5');

But my arrays a formed:
I obtain $array1:
   
      

          for( $i = 0; $i < count ($myarray1['data']); $i ++)
          {
               $array1 = $myarray1['data'][$i]['place']['id'];
               
          }



         
    And I obtain $array2 from database:
   

  while($row = mysql_fetch_array( $result )) {

      $array2 = $row['name'];

}

If after the loops I do:
$intersection = array_intersect($array1, $array2);

$array1 and $array2 contains the last value looped so intersection idea does not work :(
0
 
Beverley PortlockCommented:
Do the print_r statements I outlined above. My concern is this

 $array1 = $myarray1['data'][$i]['place']['id'];

and

 $array2 = $row['name'];

These are assignments and as such they will overwrite the current values. You have these in loops which means that only the last value of the loop will be in array1 and array2. This is particularly true for the second loop because I know that $row['name']; is a value not an array. That is why I want to see the output from the print_r statements. I suspect that you may need [ ] after both array1 and array2 in those loops.
0
 
Dada44Author Commented:
yes you're right!  [] is needed (sorry about before, I tried it but it seems the file was cached or something).

Thanks a lot!!!!!!!!
0
 
Beverley PortlockCommented:
Glad you're sorted.

Cheers!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now