使用PHP循环数组键

时间:2013-06-14 04:51:10

标签: php arrays loops

我有一个数组,其中包含每个值的数组。我怎样才能遍历每个嵌套数组(或任何技术术语)。

这是结构(每个订单可以有多个order_items)

Array ( [0] => 
         Array ( 
            [order_item] => Array (
                [id] => 1
                [order_id] => 1
                [userid] => 1
                [item_number] => 3
                [itemName] => Item A
                [price] => 1.99
                [quantity] => 1
                [total] => 1.99
                [item_status] => Not Filled
                [created] => 2013-06-13 07:42:00
                [modified] => 2013-06-13 07:42:00
            ) 
            [order_item] => Array (
                [id] => 2
                [order_id] => 1
                [userid] => 1
                [item_number] => 4
                [itemName] => Item B
                [price] => 1.99
                [quantity] => 1
                [total] => 1.99
                [item_status] => Not Filled
                [created] => 2013-06-13 07:42:00
                [modified] => 2013-06-13 07:42:00
                )   
            [Order] => Array (
                [id] => 1
                [userid] => 4
                [order_status] => Not Filled
                [email] => test@gmail.com
                [total] => 1.99
                [fullName] => Test
                [address] => Test
                [city] => Test
                [state] => IA
                [zip] => 12345
                [created] => 2013-06-13 07:42:00
                [modified] => 2013-06-13 07:42:00
            ) 
        ) 
     )

每个订单可以有多个order_items。我将这些放入excel电子表格中,所以我想为订单添加标题,如此

/* new order */
//this the headings for an order
id    name     date    etc... 
 1    test      123

//this is for each of the items in the order
id    item number   quantity   price  etc..
 1     345             2         1.99
 1     456             1         1.00

/* new order */
//Another order so the headings must be added again
id    name     date    etc...
 2    test      345

//this is for each of the items in the orders
id    item number   quantity   price  etc..
 2     345             1         1.00

需要注意的一点是,我正在为CakePHP found here使用PHP excel导出插件,其中要导出的数据遵循此结构

$data = array(
        //each of these arrays are rows to be inserted into excel
        array('a', 'b', 'c'),
        array(1, 2, 3),
        array('you', 'and', 'me'),
    );

假设要插入的数据数组是$ order_values,我推送的数组是$ data

$data = array();
foreach($order_values as $order){

    //Order headings go here
    array_push($data, array('Order ID', 'Order total', etc...))

    //order data here. There is always only 1 line of order data per order
    array_push($data, $order[Order]['id'], order[Order]['item_number']..);

    //order items headings go here
    array_push($data, array('Item number', 'Item Price', etc..));

     foreach(){
          array_push($data, /*now add the order items for the specific order*/)
     }
}

我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

你向我们展示的阵列在现实中是不存在的。每个数组键必须是唯一的,否则它将被覆盖。您多次使用order_item。你能做的是:

Array ( [0] => 
     Array ( 
        [order_items] => Array (
            Array ( [id] => 1 [order_id] => 1 [userid] => 1 [item_number] => 3 [itemName] => Item A [price] => 1.99 [quantity] => 1 [total] => 1.99 [item_status] => Not Filled [created] => 2013-06-13 07:42:00 [modified] => 2013-06-13 07:42:00 ) ,
            Array ( [id] => 2 [order_id] => 1 [userid] => 1 [item_number] => 4 [itemName] => Item B [price] => 1.99 [quantity] => 1 [total] => 1.99 [item_status] => Not Filled [created] => 2013-06-13 07:42:00 [modified] => 2013-06-13 07:42:00 )
        ),
        [Order] => Array ( [id] => 1 [userid] => 4 [order_status] => Not Filled [email] => test@gmail.com [total] => 1.99 [fullName] => Test [address] => Test [city] => Test [state] => IA [zip] => 12345 [created] => 2013-06-13 07:42:00 [modified] => 2013-06-13 07:42:00 ) 
    ) 
 )

(这可能不完全是印刷品的样子,但你明白了)

然后您可以使用伪代码

$data = array();
foreach($order_values as $order){

    array_push($data, array('Order ID', 'Order total', etc...)) //Order headings go here

    array_push($data, $order[Order]['id'], order[Order]['item_number']..); //order data here. There is always only 1 line of order data per order

    array_push($data, array('Item number', 'Item Price', etc..)); //order items headings go here

     foreach($order['order_items'] as $order_item){
          array_push($data, //now add the order items for the specific order)
     }
}