2016-10-05 83 views
0

我有下面2數組:刀片2數組值新的數組與新的密鑰

Array 1 
(
    [0] => Daughter 
    [1] => Daughter 
    [2] => Son 
) 
Array 2 
(
    [0] => Nitu 
    [1] => ritu 
    [2] => ramesh 
) 

,我想用於每個鍵的不同陣列如下:

Array(
"relation" => Daughter 
"name"  => Nitu 
) 

Array(
"relation" => Daughter 
"name"  => ritu 
) 

    Array(
"relation" => Son 
"name"  => ramesh 
) 

上述陣列1個&陣列2可以根據用戶輸入而長。所以我想動態地在循環中向新數組插入值。

+0

聽見'for'環 – Ghost

回答

-1

也許你正在試圖尋找array_combine

您可以查看文檔here

+4

'array_combine'是不會在這裏工作。 –

0

試試這個,

$Aarray1 = Array 
(
    "0" => "Daughter", 
    "1" => "Daughter", 
    "2" => "Son" 
); 
$Aarray2 = Array 
(
    "0" => "Nitu", 
    "1" => "ritu", 
    "2" => "ramesh" 
); 

    foreach($Aarray1 as $key=>$val) 
    { 
     $new_array[$key]["relation"] = $val; 
     $new_array[$key]["name"] = $Aarray2[$key]; 
    } 

DEMO

+0

也請指導我在單維上生成輸出:),就像我上面的輸出一樣。 –

+0

@SantoshBohara請檢查我的編輯現在在foreach循環中保存單維數組 –

+0

@ Rohit Ailani:這是你的代碼https://3v4l.org/4cZXv。 PLZ看到它。 – Dave

0

您只需廁所通過你的數組並將其存儲在變量$result中。

試試這個:

$array1 = array('Daughter', 'Daughter','Son'); 
$array2 = array('Nitu', 'Ritu', 'Ramesh'); 
foreach ($array1 as $k => $arr1) { 
    $result[] = array(
     'relation' => $arr1, 
     'name' => $array2[$k] 
    ); 
} 
0

試試這個:

$arrayFirst = Array("0" => "Daughter", "1" => "Daughter", "2" => "Son"); 
$arraySecond = Array("0" => "Nitu","1" => "ritu","2" => "ramesh"); 

foreach($arrayFirst as $key=>$value) 
{ 
    $new_array[$key]["relation"] = $value; 
    $new_array[$key]["name"] = $arraySecond[$key]; 
}