我需要用php创建一个多维数组

我需要用php创建一个多维数组,php,arrays,json,cart,Php,Arrays,Json,Cart,我需要用php为产品购物车创建一个多维数组 由产品数组组成的订单数组 主数组应该类似于1阶2阶3阶等。。 在每个产品的主数组中,包含名称、产品代码 Array ( [0] => Array ( [0] => Array ( [name] => Heart Choclates [code] => LFB-P-10 [qty] => 1 [type] => main [price] => 1200 [stock] => 5 [image

我需要用php为产品购物车创建一个多维数组

由产品数组组成的订单数组

主数组应该类似于1阶2阶3阶等。。 在每个产品的主数组中,包含名称、产品代码

Array ( [0] => Array 
  ( [0] => Array 
    ( [name] => Heart Choclates [code] => LFB-P-10 [qty] => 1 [type] => main [price] => 1200 [stock] => 5 [image] => choclates-valentines-day.jpg [quantity] => 12 [expdate] => May 25th 2017 [exptime] => 08:00 AM to 09:00 AM [expdtype] => Fixed time delivery
     ) 
  )
)
如何修改此代码以获得它

Array ( [0] => Array ( [name] => Birthday Pink [code] => KB-P-5 [qty] => 1 [type] => main [price] => 600 [stock] => 7 [image] => pink-roses.jpg [quantity] => 10 [expdate] => May 25th 2017 [exptime] => 12:00 PM to 04:00 PM [expdtype] => Standard delivery ) [1] => Array ( [name] => Signature Cake [code] => KB-P-7 [qty] => 1 [type] => addon [price] => 0 [stock] => 9 [image] => signature-cake.jpg [expdate] => May 25th 2017 [exptime] => 12:00 PM to 04:00 PM [expdtype] => Standard delivery ) [2] => Array ( [name] => Truffle Cake [code] => KB-P-8 [qty] => 1 [type] => addon [price] => 10 [stock] => 7 [image] => truffle-cake.jpg [expdate] => May 25th 2017 [exptime] => 12:00 PM to 04:00 PM [expdtype] => Standard delivery ) )
试试这个

$productextra = array();
if ($product_price == $obj->price) 
{
   $productextra[] = array(
        $productslist => array(
        'name' => $obj->product_name,
        'code' => $product_code,
        'qty' => $product_qty,
        'type' => 'main',
        'price' => $obj->price,
        'stock' => $obj->stock,
        'image' => $obj->image,
        'quantity' => $obj->quantity,
        'expdate' =>$_SESSION['dateofdelivery'],
        'exptime' =>$_SESSION['timeslot'],
        'expdtype'=>$_SESSION['typeofdelivery']
        )
    );
}
上面的代码输出如下

Array ( [0] => Array ( [productslist_value] => Array ( [name] => name ) ) ) 

它将像

Array ( [0] => Array ( [0] => Array ( [name] => name ) ) )....

您可以运行下面的代码并使用所需的代码

<?php
$productextra= array(
    $productslist = array(
        'name' => "Heart Choclates",
        'code' => "LFB-P-10",
        'qty' => 1,
        'type' => 'main',
        'price' => 1200,
        'stock' => "5",
        'image' => "choclates-valentines-day.jpg",
        'quantity' => "12",
        'expdate' => "May 25th 2017",
        'exptime' => "08:00 AM to 09:00 AM",
        'expdtype'=> "Fixed time delivery"
    )
);
$productslist = array(
    'name' => "Heart Choclates",
    'code' => "LFB-P-10",
    'qty' => 1,
    'type' => 'main',
    'price' => 1200,
    'stock' => "5",
    'image' => "choclates-valentines-day.jpg",
    'quantity' => "12",
    'expdate' => "May 25th 2017",
    'exptime' => "08:00 AM to 09:00 AM",
    'expdtype'=> "Fixed time delivery"
); 
$main_array=array(
    $productextra= array(
        $productslist = array(
            'name' => "Heart Choclates",
            'code' => "LFB-P-10",
            'qty' => 1,
            'type' => 'main',
            'price' => 1200,
            'stock' => "5",
            'image' => "choclates-valentines-day.jpg",
            'quantity' => "12",
            'expdate' => "May 25th 2017",
            'exptime' => "08:00 AM to 09:00 AM",
            'expdtype'=> "Fixed time delivery"
        )
    )
);
echo "<pre>";
print_r($productextra);
echo "<br>";
print_r($productslist);
echo "<br>";
print_r($main_array);
?>

它目前在生产什么?谢谢你的回答。用当前的输出更新了问题。嗨,谢谢它工作,但我需要知道一件事。如何在子数组中添加附加产品,如此数组[0]=>array[0]=>array[name]=>name数组[1]=>array[name]=>name…如果$product\u price====$obj->price,请尝试此操作{$products=array;array_push$products,array'name'=>$obj->product_name,'code'=>$product_code,…;array_push$products,array…;//依此类推……$productextra[]=array$products;}
Array ( [0] => Array ( [0] => Array ( [name] => name ) ) )....
<?php
$productextra= array(
    $productslist = array(
        'name' => "Heart Choclates",
        'code' => "LFB-P-10",
        'qty' => 1,
        'type' => 'main',
        'price' => 1200,
        'stock' => "5",
        'image' => "choclates-valentines-day.jpg",
        'quantity' => "12",
        'expdate' => "May 25th 2017",
        'exptime' => "08:00 AM to 09:00 AM",
        'expdtype'=> "Fixed time delivery"
    )
);
$productslist = array(
    'name' => "Heart Choclates",
    'code' => "LFB-P-10",
    'qty' => 1,
    'type' => 'main',
    'price' => 1200,
    'stock' => "5",
    'image' => "choclates-valentines-day.jpg",
    'quantity' => "12",
    'expdate' => "May 25th 2017",
    'exptime' => "08:00 AM to 09:00 AM",
    'expdtype'=> "Fixed time delivery"
); 
$main_array=array(
    $productextra= array(
        $productslist = array(
            'name' => "Heart Choclates",
            'code' => "LFB-P-10",
            'qty' => 1,
            'type' => 'main',
            'price' => 1200,
            'stock' => "5",
            'image' => "choclates-valentines-day.jpg",
            'quantity' => "12",
            'expdate' => "May 25th 2017",
            'exptime' => "08:00 AM to 09:00 AM",
            'expdtype'=> "Fixed time delivery"
        )
    )
);
echo "<pre>";
print_r($productextra);
echo "<br>";
print_r($productslist);
echo "<br>";
print_r($main_array);
?>
Array
(
[0] => Array
    (
        [name] => Heart Choclates
        [code] => LFB-P-10
        [qty] => 1
        [type] => main
        [price] => 1200
        [stock] => 5
        [image] => choclates-valentines-day.jpg
        [quantity] => 12
        [expdate] => May 25th 2017
        [exptime] => 08:00 AM to 09:00 AM
        [expdtype] => Fixed time delivery
    )
)

Array
(
    [name] => Heart Choclates
    [code] => LFB-P-10
    [qty] => 1
    [type] => main
    [price] => 1200
    [stock] => 5
    [image] => choclates-valentines-day.jpg
    [quantity] => 12
    [expdate] => May 25th 2017
    [exptime] => 08:00 AM to 09:00 AM
    [expdtype] => Fixed time delivery
)

Array
(
[0] => Array
    (
        [0] => Array
            (
                [name] => Heart Choclates
                [code] => LFB-P-10
                [qty] => 1
                [type] => main
                [price] => 1200
                [stock] => 5
                [image] => choclates-valentines-day.jpg
                [quantity] => 12
                [expdate] => May 25th 2017
                [exptime] => 08:00 AM to 09:00 AM
                [expdtype] => Fixed time delivery
            )

    )
)