L
L
lolrofl012018-10-17 15:14:24
Laravel
lolrofl01, 2018-10-17 15:14:24

Is there a function in php or laravel to compare names in arrays?

There are 2 associative arrays:

$arr1[] = [
'name1' => 'Имя',
'name2' => 'Имя2'
];

$arr2[] = [
'name1' => 'Имя2',
'name2' => 'Имя4',
'link' => 'href'
];

It is necessary to compare name1 and name2 in both arrays, and if there is at least one similarity (as in the example above, Name2 converges , then leave the names, otherwise, delete the array element. Is there a function in PHP or a method in Lara that would facilitate this task?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2018-10-17
@lolrofl01

<?php
$arr1 = [
'name1' => 'Имя',
'name2' => 'Имя2'
];

$arr2 = [
'name1' => 'Имя2',
'name2' => 'Имя4',
'link' => 'href'
];


function array_cross(&$arr1,&$arr2,$r=false) {
   foreach($arr1 as $k1=>$a1) {
     $f=false;
     foreach($arr2 as $k2=>$a2)
       if($a1==$a2) {$f=true;break;}
       if(!$f) unset($arr1[$k1]);
     }

    if(!$r) array_cross($arr2,$arr1,true);
}

array_cross($arr1,$arr2);
echo '$arr1[] = ';
print_r($arr1);
echo '<br>$arr2[] = ';
print_r($arr2);
/*
$arr1[] = Array ( [name2] => Имя2 ) 
$arr2[] = Array ( [name1] => Имя2 )
*/
?>

A
Alex_Zdorgor, 2018-10-17
@Alex_Zdorgor

php.net/manual/en/function.array-intersect.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question