附加到关联数组?

时间:2012-12-12 08:57:29

标签: php multidimensional-array

我有一个源数组

$A = array(
    0=> array(
        'title'=>'HTML5+CSS3',
        'teacher'=>'jonh',
        'id_post'=>1,/* link to  $post_formation */
        'formation'=>1,
        'date'=>'12/12/2112'
    )
);

和另一个数组:

$post_formation = array(
            0=>array(1,2,3),
            1=>array(3,4,5)
        )

查看$A以操纵新的restult(在这种情况下为id_post=1 1=>array(3,4,5),因此将包含更多3 elements

$result = array(
    /* from $A */
    0=> array(
        'title'=>'HTML5+CSS3',
        'teacher'=>'jonh',
        'id_post'=>1,

        'formation'=>1,
        'date'=>'12/12/2112'
    ),

    /* Append here  more 03 elements `3,4,5` */
    1=> array(
        'title'=>'(A)HTML5+CSS3',
        'teacher'=>'jonh',
        'id_post'=>1,

        'formation'=>3,
        'date'=>'--/--/---'
    ),
    2=> array(
        'title'=>'(A)HTML5+CSS3',
        'teacher'=>'jonh',
        'id_post'=>1,

        'formation'=>4,
       'date'=>'--/--/----'
    ),
    3=> array(
        'title'=>'(A)HTML5+CSS3',
        'teacher'=>'jonh',
        'id_post'=>1,

        'formation'=>5,
        'date'=>'--/--/----'
    )
);

任何人都可以告诉我这是怎么回事?

2 个答案:

答案 0 :(得分:1)

虽然有点奇怪......

$A=array(array("title"=>"HTML5+CSS3","teacher"=>"john","id_post"=>1,"formation"=>1,"date"=>"12/12/2012"));
$post_information=array(0=>array(1,2,3),1=>array(3,4,5));
print_r($A);
if(isset($post_information[$A[0]["id_post"]]))
{
    foreach($post_information[$A[0]["id_post"]] as $idx)
    {
        $cache=$A[0];
        $cache["title"]="(A)".$cache["title"];
        $cache["formation"]=$idx;
        $cache["date"]="--/--/----";
        $A[]=$cache;
    }
}
print_r($A);

结果:

第一个print_r

Array
(
    [0] => Array
        (
            [title] => HTML+CSS3
            [teacher] => john
            [id_post] => 1
            [formation] => 1
            [date] => 12/12/2012
        )

)

第二个print_r

Array
(
    [0] => Array
        (
            [title] => HTML+CSS3
            [teacher] => john
            [id_post] => 1
            [formation] => 1
            [date] => 12/12/2012
        )

    [1] => Array
        (
            [title] => (A)HTML+CSS3
            [teacher] => john
            [id_post] => 1
            [formation] => 3
            [date] => --/--/----
        )

    [2] => Array
        (
            [title] => (A)HTML+CSS3
            [teacher] => john
            [id_post] => 1
            [formation] => 4
            [date] => --/--/----
        )

    [3] => Array
        (
            [title] => (A)HTML+CSS3
            [teacher] => john
            [id_post] => 1
            [formation] => 5
            [date] => --/--/----
        )

)

您当然可以将逻辑包装在函数中,而不是明确地修改源$A

答案 1 :(得分:0)

另一种方式,可能更简单

  $ArrayPayChannel=array();

 function array_push_assoc($array, $key, $value){
  $array[$key] = $value;
 return $array;
 }

 $ArrayPayChannel = array_push_assoc($ArrayPayChannel, 'Key', 'Value');

如果你在数组中有更多的深度而不是值,你可以放另一个array_push_assoc($ ArrayPayChannel,'Key','Value');你有更多的深度