-2

1.I would like to store the array value to this three variable, and take the intersected value of it, but it did not work!

$report =array();
$report1 =array();
$report2 =array();
while ($row=mysql_fetch_array($r_query)){
   echo 'Primary first State:' .$row['user_id'].'<br>';
    $report[]=array(
         'id'=>$row['user_id']);
}

while ($rowa=mysql_fetch_array($r_querya)){
   echo 'Primary second State:  ' .$rowa['user_id'].'<br>'; 
 $report1[]=array(
         'id'=>$rowa['user_id']);
}
while ($rowb=mysql_fetch_array($r_queryb)){
   echo 'Primary third State:  ' .$rowb['user_id'].'<br>';  
 $report2[]=array(
         'id'=>$rowb['user_id']);
}
echo var_dump($report);
   $result = array_intersect($report,$report1,$report2);
   echo 'intersect State:  ' .$result;
}
?>
tadman
  • 208,517
  • 23
  • 234
  • 262
  • **WARNING**: If you're just learning PHP, please, do not learn the obsolete `mysql_query` interface. It's awful and is being removed in future versions of PHP. A modern replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). A guide like [PHP The Right Way](http://www.phptherightway.com/) can help explain best practices. Always be absolutely **sure** your user parameters are [properly escaped](http://bobby-tables.com/php) or you will have severe [SQL injection bugs](http://bobby-tables.com/). – tadman Jun 24 '15 at 17:21
  • "Did not work" is not a diagnostic we can help you with. Do you have any errors to report? What query are you running? Are you even checking for errors when making SQL calls? – tadman Jun 24 '15 at 17:22
  • echo var_dump($report); The 3 array store the value like this array(2) { [0]=> string(1) “5” [1]=> string(1) “7” } but the intersect only output 'Array'! – Bing Chen Jun 24 '15 at 17:37

1 Answers1

0

The result of an array_intersect is an array itself, thus replace the last line of code with something like:

echo 'intersect State:  ' . PHP_EOL;
var_dump($result);

In addition:

echo var_dump($report);

doesn't make much sense, use only the latter (var_dump) for arrays... .

saulnier
  • 83
  • 1
  • 4
  • Yes, it output the intersect value! thank you so much. In addition, can I directly use this variable to here //Country output $sqli = "SELECT meta_value FROM search WHERE user_id LIKE '%".$result."%' and meta_key='pmpro_bcountry'"; $r_queryi = mysql_query($sqli); – Bing Chen Jun 24 '15 at 18:32
  • //Country output $sqli = "SELECT meta_value FROM search WHERE user_id LIKE '%".$result."%' and meta_key='pmpro_bcountry'"; $r_queryi = mysql_query($sqli); while($a=mysql_fetch_array($r_queryi)){ echo '
    County: '.$a[meta_value]; }
    – Bing Chen Jun 24 '15 at 18:51
  • If your result returns an array of user_id -s you can accomplish it by: `$search_list = '(' . explode(',',$result) . ')'` and placing that in your sql like: `... WHERE user_id IN " . $search_list`; – saulnier Jun 24 '15 at 19:00
  • The result is like this: @saulnier array(1) { [0]=> array(2) { [0]=> string(1) “5” [1]=> string(1) “7” } } – Bing Chen Jun 24 '15 at 19:04
  • This is the error warning: explode() expects parameter 2 to be string, array given in – Bing Chen Jun 24 '15 at 19:12
  • Sorry, I meant `implode` – saulnier Jun 24 '15 at 19:32
  • If this helped you, please indicate this as the answer. Thank you. – saulnier Jun 24 '15 at 20:03
  • yes, I fix it, by changing the code: $result = array_values(array_intersect($report1,$report2,$report)); – Bing Chen Jun 24 '15 at 21:47