Parameters. array. The array to iterate over callback. The callback function to use If no callback is supplied, all empty entries of array will be removed. See empty() for how PHP defines empty in this case.. mode. Flag determining what arguments are sent to callback: . ARRAY_FILTER_USE_KEY - pass key as the only argument to callback instead of the value
Return Values. The current() function simply returns the value of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, current() returns false.
You can use array_uintersect in conjunction with spl_object_hash, see an example:
array_uintersect($a, $b, function($a, $b) {
return strcmp(spl_object_hash($a), spl_object_hash($b));
});
If you want to get 'nature' first, because 'walk' comes before 'horse', you need to iterate over the words first, not over the tags.
$tags = [
'animals' => ['cat', 'dog', 'horse', 'ferret'],
'nature' => ['walk', 'outdoor', 'tree', 'plant'],
];
function getTags(string $text, array $tags): array
{
$lowerC = strtolower($text);
$str = preg_replace("/[^A-Za-z\'\- ]/", '', $lowerC);
$arrayT = explode(" ", $str);
$tagArray = [];
foreach ($arrayT as $word) {
// find tag for this word
foreach ($tags as $cat => $values) {
if (in_array($word, $values)) {
// append the tag to the list
$tagArray[] = $cat;
}
}
}
// remove duplicates
return array_unique($tagArray);
}
$res = getTags('During my walk, I met a white horse', $tags);
var_dump($res);
Output :
array(2) {
[0]=>
string(6) "nature"
[1]=>
string(7) "animals"
}
EDIT
As @GeorgeGarchagudashvili mentionned, the code could be optimized by preparing an array for comparison. Here is a way :
function getTags(string $text, array $tags): array
{
$lowerC = strtolower($text);
$str = preg_replace("/[^A-Za-z\'\- ]/", '', $lowerC);
$arrayT = explode(" ", $str);
// Prepare tags for searching
$searchTags = [];
foreach ($tags as $cat => $values) {
foreach ($values as $word) {
$searchTags[$word] = $cat;
}
}
$tagArray = [];
foreach ($arrayT as $word)
{
// find tag for this word
if (isset($searchTags[$word]))
{
// append the tag to the list
$tagArray[] = $searchTags[$word];
}
}
// remove duplicates
return array_unique($tagArray);
}
You can do that using function and defining manually your callback comparison function :array_uintersect
$arr1 = json_decode('[{"id":2,"influencer_id":2,"follower_id":1}]');
$arr2 = json_decode('[{"id":2,"influencer_id":2,"follower_id":1},{"id":3,"influencer_id":3,"follower_id":2}]');
$arr3 = array_uintersect($arr1, $arr2, function ($e1, $e2) {
if($e1->id == $e2->id && $e1->influencer_id == $e2->influencer_id && $e1->follower_id == $e2->follower_id) {
return 0;
} else {
return 1;
}
});
var_dump($arr3);
Yes, it's the expected behavior, because the comparison is done using string representations, and the function does not recurse down nested arrays. From the manual:
The two values from the key => value pairs are considered equal only if
(string) elem2. In other words a strict type check is executed so the string representation must be the same.
If you tried to intersect with an array with , you'd get the same result because the string representation of an array is always 'key1' => 'Array'.'Array'
One of the user-contributed notes, by nleippe, contains a recursive implementation that looks promising (I modified the third line to do string comparison on any non-array values):
function array_intersect_assoc_recursive(&$arr1, &$arr2) {
if (!is_array($arr1) || !is_array($arr2)) {
// return $arr1 == $arr2; // Original line
return (string) $arr1 == (string) $arr2;
}
$commonkeys = array_intersect(array_keys($arr1), array_keys($arr2));
$ret = array();
foreach ($commonkeys as $key) {
$ret[$key] =& array_intersect_assoc_recursive($arr1[$key], $arr2[$key]);
}
return $ret;
}
$ cat > test.php
<?php
$arrayOne = array('0003', '0004', '0005');
$arrayTwo = array(4, 5, 6);
$intersect = array_intersect($arrayOne, $arrayTwo);
print_r($intersect );
?>
$ php test.php
Array ( )
$
So no, it will not. But if you add
foreach($arrayOne as $key => $value)
{
$arrayOne[$key] = intval($value);
}
you will get
$ php test.php
Array ( [1] => 4 [2] => 5 )
How about this?
foreach ($arr1 as $key => $val) {
if (in_array($val, $arr2)){
// do something, maybe return so you wouldn't need break
break;
}
}