如何将多数组转换为json多数组

时间:2016-03-02 15:48:19

标签: php arrays json codeigniter

我有foreach循环,从数据库返回多个数组  我想将此数组转换为json中的多数组,

怎么做?

php数组示例

Array
(
    [0] => Array
        (
            [it_code] => 2894
            [it_quantity] => 300
            [it_price] => 0
            [it_notes] => 
        )

    [1] => Array
        (
            [it_code] => 2894
            [it_quantity] => 284
            [it_price] => 0
            [it_notes] => 
        )

    [2] => Array
        (
            [it_code] => 2894
            [it_quantity] => 4
            [it_price] => 0
            [it_notes] => 
        )

    [3] => Array
        (
            [it_code] => 2894
            [it_quantity] => 3
            [it_price] => 0
            [it_notes] => 
        )

)

我希望返回的json就像这种格式

 [
            ['2894', 300, 0,''],
            ['2894', 284, 0,''],
            ['2894', 4, 0,''],
            ['2894', 3, 0,''],
            ['2894', 10, 0, '']
        ]

我的代码就像这样

$this->db->where("it_parent_item", $parent_id);
$this->db->select("d_items.it_code,d_items_type.it_ty_ar_desc,d_items.it_quantity,d_items.it_price,it_notes");
$this->db->join('d_items_type','d_items_type.it_ty_id=d_items.it_type','left');
$this->db->from("d_items");
$result = $this->db->get()->result_array();


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

4 个答案:

答案 0 :(得分:1)

您可以使用array_values()array_walk_recursive()将整数转换为字符串

$newArray = array();
foreach($sourceArray as $element) {
    $newArray[] = array_values($element);
}

array_walk_recursive($newArray, 
function(&$value, $key){
    $value = (string)$value;
});

print_r (json_encode($newArray));

答案 1 :(得分:1)

请注意,其他答案将提供null而不是''

因此,在不使用array_values的情况下,此代码返回所有值,但如果有任何null,则返回''(正如问题中所预期的那样):

$arr = array();
foreach($foo as $value){
    $tmp = array();
    foreach($value as $v){
        $tmp[] = $v===null ? '' : $v;
    }
    $arr[] = $tmp;
}

echo json_encode($arr);

输出:

  

[[2894,300,0 “”],[2894,284,0 “”],[2894,4,0 “”],[2894,3,0 “”]]

[
    [2894,300,0,""],
    [2894,284,0,""],
    [2894,4,0,""],
    [2894,3,0,""]
]

这是一个可复制的数组:

$foo = array
(
    0 => array
        (
            'it_code' => 2894,
            'it_quantity' => 300,
            'it_price' => 0,
            'it_notes' => null
        ),

    1 => Array
        (
            'it_code' => 2894,
            'it_quantity' => 284,
            'it_price' => 0,
            'it_notes' => null
        ),

    2 => Array
        (
            'it_code' => 2894,
            'it_quantity' => 4,
            'it_price' => 0,
            'it_notes' => null
        ),

    3 => Array
        (
            'it_code' => 2894,
            'it_quantity' => 3,
            'it_price' => 0,
            'it_notes' => null
        ),

);

答案 2 :(得分:0)

在我看来,您希望循环遍历数组,以便在PHP中格式化您想要的格式,然后将该PHP数组转换为JSON:

$dataArray = array(); //The array containing your values
$jsonArray = array(); //The array which will be formatted for json

foreach($dataArray as $value){
    $keylessValues = array_values($value);
    $jsonArray[] = $keylessValues;
}

$jsonArray = json_encode($jsonArray); //This is now a JSON array storing your values

我们在这里做的是遍历数组,然后仅使用array_values()的值并将它们放入$jsonArray的新索引中。

一旦我们移动了整个数组,我们就可以使用json_encode()

将新格式化和填充的数组转换为JSON

值得注意的是,您设置为''的值将以null的形式出现。如果您需要''代替null这些值,请查看@FirstOne给出的答案。

答案 3 :(得分:0)

这里是初始数组(显示为PHP数组,但与帖子相同):

$initialArray = array(
    array(
        "it_code"     => 2894,
        "it_quantity" => 300,
        "it_price"    => 0,
        "it_notes"    => '',
    ),
    array(
        "it_code"     => 2894,
        "it_quantity" => 284,
        "it_price"    => 0,
        "it_notes"    => '',
    ),
    array(
        "it_code"     => 2894,
        "it_quantity" => 4,
        "it_price"    => 0,
        "it_notes"    => '',
    ),
    array(
        "it_code"     => 2894,
        "it_quantity" => 3,
        "it_price"    => 0,
        "it_notes"    => '',
    ),
);

您可以遍历每个元素,只将值分配给一组新的数组,如下所示:

$newArray = array();

foreach ($initialArray as $subArray)
{
    $newArray[] = array_values($subArray);
}

结果数组如下所示:

[[2894,300,0,""],[2894,284,0,""],[2894,4,0,""],[2894,3,0,""]]