如何在数组中追加元素

时间:2013-09-23 05:50:10

标签: php arrays codeigniter-2

我在php中有一个执行storedprocedure的方法,如下所示:

Function GetInTransitAndDelivery($Parameter)
    {
        $this->load->database();
        $query =  $this->db->query( " EXEC StoredProcedureName $Parameter");
        if (count($query->num_rows()) > 0)
        {
            $result = array();
            foreach ($query->result() as $row)
            {
                $result[] = array("EffectiveStatusId"=>$row->EffectiveStatusId,
            "EffectiveStatus"=>$row->EffectiveStatus,
            "RecordCount"=>$row->RecordCount);
            }
        }
        return $result;
    }

给我这样的结果:

{

    "GetInTransitAndDeliveryResponse":[
        {
            "EffectiveStatusId":1,
            "EffectiveStatus":"abc",
            "RecordCount":7
        },
        {
            "EffectiveStatusId":6,
            "EffectiveStatus":"efg",
            "RecordCount":91
        }
    ]

但我想结果就像这样:

{

    "GetInTransitAndDeliveryResponse":[
        {
            "EffectiveStatusId":1,
            "EffectiveStatus":"abc",
            "RecordCount":7
        },
         {
            "EffectiveStatusId":1,
            "EffectiveStatus":"efg",
            "RecordCount":7
        },
         {
            "EffectiveStatusId":1,
            "EffectiveStatus":"hij",
            "RecordCount":0
        },

        {
            "EffectiveStatusId":6,
            "EffectiveStatus":"klm",
            "RecordCount":0
        }
    ]

那么如何在结果数组

中追加元素

提前致谢

3 个答案:

答案 0 :(得分:0)

尝试使用array_push()函数

See Documentation

根据您的评论修改答案

<?php
//1 - I think your array element is as json string

$str = '{
    "GetInTransitAndDeliveryResponse":[
        {
            "EffectiveStatusId":1,
            "EffectiveStatus":"abc",
            "RecordCount":7
        },
        {
            "EffectiveStatusId":6,
            "EffectiveStatus":"efg",
            "RecordCount":91
        }
    ]
}';

//2 -  So make it first as associative array

$array = json_decode($str,true);

//3 - Then push array item

array_push($array["GetInTransitAndDeliveryResponse"],
    array(
        "EffectiveStatusId"=>1,
        "EffectiveStatus"=>"hij",
        "RecordCount"=>0
    ),

    array(
        "EffectiveStatusId"=>6,
        "EffectiveStatus"=>"klm",
        "RecordCount"=>0
    )
);

//4 - encode it as json string

$str = json_encode($json);

//5 - print it

echo $str;

输出:

{
    "GetInTransitAndDeliveryResponse":[
        {
            "EffectiveStatusId":1,
            "EffectiveStatus":"abc",
            "RecordCount":7
        },
        {
            "EffectiveStatusId":6,
            "EffectiveStatus":"efg",
            "RecordCount":91
        },
        {
            "EffectiveStatusId":1,
            "EffectiveStatus":"hij",
            "RecordCount":0
        },
        {
            "EffectiveStatusId":6,
            "EffectiveStatus":"klm",
            "RecordCount":0
        }
    ]
}

答案 1 :(得分:0)

使用array_push();

我的工作示例如下

    <?php
    $array = array("1", "2");
    array_push($array, "3", "4");
    print_r($array);
    ?>

// output will be
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

答案 2 :(得分:0)

在将数组转换为json之前,添加如下所示的元素:

$result[] = array(
    "EffectiveStatusId"=>1,
    "EffectiveStatus"=>"hij",
    "RecordCount"=>0
);

$result[] = array(
    "EffectiveStatusId"=>6s,
    "EffectiveStatus"=>"klm",
    "RecordCount"=>0
);

你可以追加你想要的所有元素。