2012-09-29 93 views
0

默認情況下,$arrDummy裏面已經有一些現有的數據。在現有的php關聯數組中添加更多數組

我想在更多的家庭加入到現有的$arrDummy

通過調用getnewfamily(),我會添加多個小的值到同一個家族的名字,然後回到返回主功能,並添加到$arrDummy

我已經使用array_mergearray_push,在這裏和那裏測試,但事情沒有按預期工作。

$arrDummy= array(

    "Family name aa" => 
        array(
        "123" => "some value", 
        "112223" => "some value", 
        ), 

    "Family Jacky" => 
        array(
        "7856" => "some savalue", 
        "2354" => "some vcx", 
        ) 
); 


$arrDummy= getnewfamily($arrDummy); 

    echo "<pre>"; 
    print_r($arrDummy); 
    echo "</pre>"; 

function getnewfamily($arrDummy) 
{ 

    $familyname="my current family"; 



    if($condition1==true) 
    { 
     $arrTest1= array($familyname => 
          array(
          "123" => "some value" 
          ) 
       ); 

     array_push($arrDummy[$familyname], $arrTest1); 

    } 

    if($condition2==true) 
    { 

     $arrTest2= array($familyname => 
          array(
          "456" => "some other value", 
          "88888" => "somes other value", 
          ) 
       ); 

     array_push($arrDummy[$familyname], $arrTest2);  
    } 

    if($condition3==true) 
    { 
     $arrTest3= array($familyname => 
          array(
          "756" => "further more" 
          ) 
       ); 

     array_push($arrDummy[$familyname], $arrTest3); 
    } 

    //How to merge or push to this very specific datas into $arrTest subarray? 


    return $arrDummy; 
} 
+0

這是錯誤的'$ arrTest = array($ family =>());' – Baba

回答

0

在腳本

這麼多的錯誤嘗試

$arrDummy = array(
"Family name aa" => array("123" => "some value","112223" => "some value"), 
"Family Jacky" => array("7856" => "some savalue","2354" => "some vcx")); 

$arrFamilytoAddtoDummy = getnewfamily($arrDummy,2); 

var_dump($arrFamilytoAddtoDummy); 

功能

function getnewfamily($arrDummy, $condition) { 
    switch ($condition) { 
     case 1 : 
      $arrDummy["my current family"] = array("123" => "some value"); 
      break; 
     case 2 : 
      $arrDummy["my current family"] = array("456" => "some other value","88888" => "somes other value"); 
      break; 
     case 3 : 
      $arrDummy["my current family"] = array("756" => "further more"); 
      break; 
    } 

    return $arrDummy; 
} 

輸出

array 
    'Family name aa' => 
    array 
     123 => string 'some value' (length=10) 
     112223 => string 'some value' (length=10) 
    'Family Jacky' => 
    array 
     7856 => string 'some savalue' (length=12) 
     2354 => string 'some vcx' (length=8) 
    'my current family' => 
    array 
     456 => string 'some other value' (length=16) 
     88888 => string 'somes other value' (length=17) 
+0

hmm,不能使用switch語句,因爲當條件爲真時,$ familyname =>中的值會隨着時間增長而變大 –

+1

它只是一個指導,因爲你的腳本包含這麼多的錯誤 – Baba