2013-12-16 39 views
-2
<?php 
echo '<pre>'; 

$directnames = array("LN"=>"ListingId","LAG"=>"AgentCode","ST"=>"MlsStatus","LP"=>"ListPrice","PIC"=>"PhotosCount"); 

$result = array("LN"=>"129_551453","LAG"=>"2.50","ST"=>"3.00","LP"=>"Ferndale","PIC"=>"359900.00"); 


$directnames_getkeys = array_keys($directnames); 
$result_getkeys = array_keys($result); 
$merge_keys = array_intersect($directnames_getkeys,$result_getkeys); 
$assigning = array(); 

foreach($merge_keys as $preparevalues){ 
    foreach($directnames[$preparevalues] as $keys){ 
     echo $assigning[$keys] = $result[$preparevalues]; 
    } 
} 


echo '</pre>'; 
?> 

预期输出:PHP分配一个数组的值作为一个密钥另一个数组的值,如果两个阵列密钥匹配

array(
    "ListingId"=>"129_551453", 
    "AgentCode"=>"2.50", 
    "MlsStatus"=>"3.00", 
    "ListPrice"=>"Ferndale", 
    "PhotosCount"=>"359900.00" 
    ) 
+2

是什么问题? – elibyy

+0

它说,它是一个未声明的变量或参数传递给'$ directnames'的第二个'foreach'行,您可以在其中清楚地看到它。 – Neocortex

+0

当我运行它时,我得到警告,在第16行说警告:为foreach()提供的无效参数,这是因为$ directnames [$ preparevalues]是一个字符串,而不是数组 – elibyy

回答

1

尝试array_combine()

$directnames = array("LN"=>"ListingId","LAG"=>"AgentCode","ST"=>"MlsStatus", 
     "LP"=>"ListPrice","PIC"=>"PhotosCount"); 

     $result = array("LN"=>"129_551453","LAG"=>"2.50","ST"=>"3.00", 
    "LP"=>"Ferndale","PIC"=>"359900.00"); 


      $a=array_combine($directnames,$result); 
      print_r($a); 

输出

阵列([ListingId] => 129_551453 [AgentCode] => 2.50 [MlsStatus] => 3.00 [ListPrice] => Ferndale的[PhotosCount] => 359900.00)

0

细这项工作:

$directnames = array("LN"=>"ListingId","LAG"=>"AgentCode","ST"=>"MlsStatus","LP"=>"ListPrice","PIC"=>"PhotosCount"); 

$result = array("LN"=>"129_551453","LAG"=>"2.50","ST"=>"3.00","LP"=>"Ferndale","PIC"=>"359900.00"); 


foreach($directnames as $value){ 
    $resultArray[][$value]= current($result); 
    next($result); 
} 


var_dump($resultArray); 

结果:

array(1) { 
    ["ListingId"]=> 
    string(10) "129_551453" 
    } 
    [1]=> 
    array(1) { 
    ["AgentCode"]=> 
    string(4) "2.50" 
    } 
    [2]=> 
    array(1) { 
    ["MlsStatus"]=> 
    string(4) "3.00" 
    } 
    [3]=> 
    array(1) { 
    ["ListPrice"]=> 
    string(8) "Ferndale" 
    } 
    [4]=> 
    array(1) { 
    ["PhotosCount"]=> 
    string(9) "359900.00" 
    } 
+0

在发布之前,我做了同样的事情,谈论你上面做了什么。请参阅问题中的预期输出。我感谢你的努力。 – Neocortex

+0

我不拉屎,我的脚本的结果是你问 – sergio

+0

你已经应用了一个逻辑,其中每个值都包裹在一个数组中..当我正在为一个数组中的所有值工作..你明白吗? – Neocortex

相关问题