看似跳过第一阵列的foreach

时间:2016-02-03 10:29:44

标签: php arrays function loops

我正在使用带有array_column的array_search来查看我是否可以在我的两个数组之间找到匹配项,如果是这样,它应该返回找到匹配项的数组的索引,我可以使用它找到它们的帐号。

由于某些原因它完全跳过第一次迭代(或似乎)而我遇到了一些麻烦,我不明白为什么。

数组结构:

$account = Array
(
[0] => Array
    (
        [Account number] => 100607
        [Account name] => Test User 1
        [Telephone] => 71
        [Contact Email address] => Test1@email.com
    )

[1] => Array
    (
        [Account number] => 101502
        [Account name] => Test User 2
        [Telephone] => 72
        [Contact Email address] => Test2@email.com
    )

[2] => Array
    (
        [Account number] => 101514
        [Account name] => Test User 3
        [Telephone] => 73
        [Contact Email address] => Test3@email.com
    )

[3] => Array
    (
        [Account number] => 125780
        [Account name] => Test User 4
        [Telephone] => 74
        [Contact Email address] => Test4@email.com
    )

) 
$multi_array = Array
(
[0] => Array
    (
        [email] => Test1@email.com
        [name] => Test User 1 
        [telephone] => 71
    )

[1] => Array
    (
        [email] => Test2@email.com
        [name] => Test User 2 
        [telephone] => 72
    )

[2] => Array
    (
        [email] => Test3@email.com
        [name] => Test User 3 
        [telephone] => 73
    )

[3] => Array
    (
        [email] => Test4@email.com
        [name] => Test User 4 
        [telephone] => 74
    )

)

我的代码:

function get_key($key,$phone_key,$name_key){    
if ($key == !false){
  return $key;
}if($phone_key == !false){
   return $phone_key;
}if($name_key == !false){
   return $name_key;
}else{
  return "";
}
}
$complete = array();
foreach ($multi_array as $array){

$key = array_search($array["email"], array_column($account, 'Contact Email address'),true);
$phone_key = array_search($array["telephone"], array_column($account, 'Telephone'),true);
$name_key = array_search($array["name"], array_column($account, 'Account name'),true);

$array['Account Number'] = $account[get_key($key,$phone_key,$name_key)]['Account number']; 

$complete[] = $array;

}

通过print_r()的所有结果;是:

Array
(
[0] => Array
    (
        [email] => Test1@email.com
        [name] => Test User 1 
        [telephone] => 71
        [Account Number] => 
    )

[1] => Array
    (
        [email] => Test2@email.com
        [name] => Test User 2 
        [telephone] => 72
        [Account Number] => 101502
    )

[2] => Array
    (
        [email] => Test3@email.com
        [name] => Test User 3 
        [telephone] => 73
        [Account Number] => 101514
    )

[3] => Array
    (
        [email] => Test4@email.com
        [name] => Test User 4 
        [telephone] => 74
        [Account Number] => 125780
    )

)

除第一个数组['帐号']空白外,它按预期工作。

0 个答案:

没有答案