我有两个数组
$array1 = array('Visa Inc', 'Microsoft Corporation', 'Apple Inc');
$array2 = array('visa', 'microsoft', 'apple', 'amazon');
现在我想通过比较两个数组的常见字符来获得输出,例如:
$result = array('Visa Inc', 'Microsoft Corporation', 'Apple Inc');
如果有任何可能的方法来获得我所需的输出,请帮帮我吗?请建议我获得结果的程序。
答案 0 :(得分:0)
<?php
$array1 = array('Visa Inc', 'Microsoft Corporation', 'Apple Inc');
$array2 = array('visa', 'microsoft', 'apple', 'amazon');
$newArray = [];
$total = count($array1);
$sec = count($array2);
for($i=0; $i <= $sec; $i++)
{
for($j=0; $j <= $total; $j++)
{
$name = $array1[$j];
$com = explode(" ",$name);
$newName = strtolower($com[0]);
if($array2[$i] == $newName)
{
$newArray[] = $array1[$j];
}
}
}
$result = array_filter($newArray, function($var){return !is_null($var);} ); // remove empty fields
echo "<pre>";
print_r($result);