John Davidson

php - Can the array_intersect be used like this

0 comments
Message:


$length =count($k);
$name=array();
$j=$k[0];
$out =preg_grep("[".$j."]",$database);
array_push($name,$out);
for($x=1;$x<$length;$x++)
{
$j = $k[$x];
$out = preg_grep("[" . $j . "]", $name);
$name = array_intersect($out, $name);
}

i want to replace$namewith$outby computing the intersection of this two arrays,but it shows



Warning: Array to string conversion





i post all my code there now.what i want to do was found people's name
include all the alphabets the$keys lent.


<?php
$keys ='aed';
$database = file('database.txt');//there same people's name in it,
$k = str_split($keys);
$length =count($k);
$name=array();
$j=$k[0];
$out =preg_grep("[".$j."]",$database);
array_push($name,$out);
for($x=1;$x<$length;$x++)
{
$j = $k[$x];
$out = preg_grep("[" . $j . "]", $name);
$name = array_intersect($out, $name);
}
echo '<pre>';
var_dump($name);
echo '</pre>';


My Answer:
Yes, you can use `array_intersect` in PHP to find the intersection of two or more arrays. However, the code snippet you provided seems to be incomplete and does not show the usage of `array_intersect`.

If you want to find the intersection of arrays, you can use `array_intersect` like this:

php
$array1 = array(1, 2, 3, 4, 5);
$array2 = array(3, 4, 5, 6, 7);

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

print_r($intersection);


This will output:


Array
(
[2] => 3
[3] => 4
[4] => 5
)


In this example, the intersection of `$array1` and `$array2` is `[3, 4, 5]`.

Rate this post

3 of 5 based on 2053 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us