1

我在 php 中有一种方法可以像这样执行存储过程:

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
        }
    ]

那么如何在结果数组中追加元素

提前致谢

4

3 回答 3

0

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

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

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

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

于 2013-09-23T14:41:11.303 回答
0

尝试 array_push() 函数

查看文档

答案已根据您的评论修改

<?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
        }
    ]
}
于 2013-09-23T05:54:07.887 回答
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
)
于 2013-09-23T05:56:41.330 回答