开发者

Creating Multidimenional Arrays

开发者 https://www.devze.com 2023-01-20 21:12 出处:网络
How can I add arrays into an existing array item? For instance: $user[$user->id] = array(//values); But if that user needs another array added, I want all data to fall under that user ID in the

How can I add arrays into an existing array item?

For instance:

$user[$user->id] = array(//values);

But if that user needs another array added, I want all data to fall under that user ID in the array.

I want to store orders a user has made, so I can look up all orders on 开发者_StackOverflow社区the fly by user ID in the $user array above.


$user[$user->id]['orders'] = array();

Or

$user[$user->id] = array(
    'orders' => array(
        array(// data for order #1),
        array(// data for order #2),
        array(// data for order #3)
    );
);

// Looping over the orders
foreach($user[$user->id]['orders'] as $order) {
    // Do something with the order
}


Have you tried something like:

$user[$id] = array_merge($user[$id], array(//values));


Give the orders a key

$user[$user->id] = array( "orders" => array(values) );

0

精彩评论

暂无评论...
验证码 换一张
取 消