使用键和值将多维数组转换为单个数组?

时间:2018-02-08 10:03:33

标签: php arrays multidimensional-array

这是我的数组,

 array
           (
            [0] => Array
                (
                    [Invoice_id] => 1
                    [Customer_Name] => Abcd Ltd
                    [Order_Created] => 2018-02-07
                    [Order_Delivery_Date] => 2018-02-17
                    [State_Code] => 35
                    [CGST] => 212.5
                    [SGST] => 212.5
                    [IGST] => 0
                    [Total_Amount] => 8925
                )

        [1] => Array
            (
                [Invoice_id] => 2
                [Customer_Name] => Johnson and Sons
                [Order_Created] => 2018-02-07
                [Order_Delivery_Date] => 2018-02-17
                [State_Code] => 35
                [CGST] => 2975
                [SGST] => 2975
                [IGST] => 0
                [Total_Amount] => 124950
            )
    )

如何转换此数组,如下所示

array
    (
    array("invoice_id" => "1", "customer_name" => "Abcd Ltd", "order_created" => 2018-02-07, "delivery_date" => 2018-02-17, "state_code" => 35, "cgst" =>212.5, "sgst" =>212.5, "igst" =>0, "total_amount" =>8925),
    array("invoice_id" => "2", "customer_name" => "Johnson and Sons", "order_created" => 2018-02-07, "delivery_date" => 2018-02-17, "state_code" => 35, "cgst" =>2975, "sgst" =>2975, "igst" =>512.5, "total_amount" =>124950)
    );

2 个答案:

答案 0 :(得分:0)

你已经拥有了你想要的格式,它们只是以不同的方式显示数组,除了你的数组被键索引,如果你真的想要取消索引它使用这个$newArray = array_values($array)

答案 1 :(得分:0)

您可以尝试更改阵列的键:

foreach ($array as $k => $item) {
    foreach ($item as $key => $value) {
        unset($array[$k][$key]) ; // remove old key
        $array[$k][strtolower($key)] = $value ; // add new one
    }
}

然后"Invoice_id"键将为"invoice_id",依此类推。