【问题标题】:how to convert multi array to json multi array如何将多数组转换为json多数组
【发布时间】:2016-06-15 14:01:37
【问题描述】:

我有从数据库返回多数组的 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>";

【问题讨论】:

  • json_encode() 你在找什么?
  • @Henders,这不会删除keys(如果这是操作人员正在寻找的),但值得一试。那么,您是否必须删除密钥(it_..)?
  • 好点,没想过重新格式化...
  • 正如你所说,我想在将其转换为 json 之前删除密钥
  • 我只想要值

标签: php arrays json codeigniter


【解决方案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));

【讨论】:

    【解决方案2】:

    在我看来,您希望循环遍历您的数组,以便在 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 给出的答案。

    【讨论】:

    • 请注意,这里返回的不是'',而是null
    • 谢谢,我在测试中发现了,但忘记放了!
    【解决方案3】:

    这是初始数组(显示为 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,""]]
    

    【讨论】:

    • syntax error, unexpected ','on: it_notes =&gt; ,
    • 已修复,it_notes 的那些讨厌的空值。
    • 当然,取决于用户想要什么。但希望这些能让他开始重新格式化数组,也许 SQL 查询可以返回更多有用的值而不是 null。
    【解决方案4】:

    请注意,其他答案将给出null 而不是''

    因此,不使用array_values,此代码将返回所有值,但如果有任何空值,它会返回''(正如问题中所预期的那样):

    $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
            ),
    
    );
    

    【讨论】:

    • 不错的解决方案!这会比使用array_values() 更密集吗?
    • TBH,我不知道它是否会更密集,但在那种情况下,array_values 在值为null 时不会返回''... @Henders
    猜你喜欢
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2021-09-22
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多