2013-12-16 67 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

相關問題