0

默认情况下,$arrDummy其中已经有一些现有数据。

我想在现有的家庭中添加额外的家庭$arrDummy

通过调用getnewfamily(),我会将多个小值添加到同一个姓氏中,然后返回主函数并添加到$arrDummy.

我已经使用array_mergeand array_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;
}
4

1 回答 1

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)
于 2012-09-29T16:42:55.703 回答