简体   繁体   English

PHP / Laravel多维数组

[英]PHP / Laravel multidimensional array

what I would like to ask is - I'm working on some stock application where you can have more than one stock and at the same time, you can have one item in 2 or more different stocks. 我想问的是-我正在某个库存应用程序上工作,在该应用程序中,您可以拥有多个库存,同时您可以在2个或更多不同的库存中拥有一件商品。 So there is the problem. 因此存在问题。 I'm saving my items data to arrays like this one 我将项目数据保存到像这样的数组中

 $cart[$data->resource_id] = array
 (
 'resID' => $data->resource_id,
 'stoID' => $data->stock_id,
 'stoName' => $data->sto_name,
 'resName' => $data->res_name,
 'actQuantity' => $data->quantity,
 'minimum' => $data->minimum,
 'quantity' => $quant
 );

but what I need to do now is to save my data to array with something with 2 indexes(?). 但是我现在要做的是用2个索引(?)将数据保存到数组中。 First index - resource_id, secound stock_id - so I know with item it is and on with stock it is. 第一个索引-resource_id,secound stock_id-所以我知道它是项目,然后是股票。 So I think something like this 所以我觉得这样

 $cart[$data->resource_id, $data->stock_id] = array
 (
 'resID' => $data->resource_id,
 'stoID' => $data->stock_id,
 'stoName' => $data->sto_name,
 'resName' => $data->res_name,
 'actQuantity' => $data->quantity,
 'minimum' => $data->minimum,
 'quantity' => $quant
 );

but its obviously not correct code. 但其代码显然不正确。

Do you have guys any ideas on how to do this? 你们对如何执行此操作有任何想法吗?

Thank you so, so much. 非常感谢。

You select it by $cart[$data->resource_id][$data->stock_id] , so without comma. 您可以通过$cart[$data->resource_id][$data->stock_id] ,因此无需使用逗号。 To set the value you can type 要设置值,您可以输入

$cart[$data->resource_id][$data->stock_id] = [
   'stoName' => $data->sto_name,
   'resName' => $data->res_name
];

php multidimensional array: define: PHP多维数组:定义:

$cars =
    [
        ["Volvo", 22, 18,[2018,19,5]],
        ["BMW", 15, 13,[2018,10,2]],
        ["Saab", 5, 2,[2018,20,5]],
        ["Land Rover", 17, 1,[2018,19,6]]
    ];

use: 采用:

echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].", manufactured(year): ".$cars[0][3][0].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].", manufactured(year): ".$cars[0][3][0].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].", manufactured(year): ".$cars[0][3][0].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].", manufactured(year): ".$cars[0][3][0].".<br>";

in your code: 在您的代码中:

 $cart[$data->resource_id][$data->stock_id]

You can create array like below. 您可以如下创建数组。

$cart[$data->resource_id] = [ 
    $data->stock_id => [

        'resID' => $data->resource_id,
        'stoID' => $data->stock_id,
        'stoName' => $data->sto_name,
        'resName' => $data->res_name,
        'actQuantity' => $data->quantity,
        'minimum' => $data->minimum,
        'quantity' => $quant
    ] 
]

PHP do not support multi dimensional arrays, at least not as you want. PHP不支持多维数组,至少不如您所愿。

To do multi dimensional arrays on PHP, you must use nested arrays: 要在PHP上创建多维数组,必须使用嵌套数组:

$a = [
    "keyA" => [
        "keyB" => 'somedata';
    ]
]

So you need to initialize the array: 因此,您需要初始化数组:

$cart = [];

$cart[$data->resource_id] = []; // Do this just in the first time that you insert something inside that `resource_id`, otherwise you will resetting the array.

$cart[$data->resource_id][$data->stock_id] = [
    //... your data here ...
]

For example, imagine that you want to get this data: 例如,假设您要获取以下数据:

$input = [
    [ 'id' => 1, 'stock_id' => 1, 'name' => 'foo' ],
    [ 'id' => 1, 'stock_id' => 2, 'name' => 'bar' ],
    [ 'id' => 1, 'stock_id' => 3, 'name' => 'biz' ],
]

And organize it by id and stock_id . 并通过idstock_id对其进行组织。

You should do a script like this: 您应该执行以下脚本:

$output =  [];

foreach ($input as $item) {
    if (!array_key_exists($item['id'], $output)) {
        $output[$item['id']] = []; // Initializing the 2nd level array
    }

    $output[$item['id']][$item['stock_id']] = $item;
}

The $output array will look look like this: $output数组看起来像这样:

[
    1 => [
        1 => [ 'id' => 1, 'stock_id' => 1, 'name' => 'foo' ],
        2 => [ 'id' => 1, 'stock_id' => 2, 'name' => 'bar' ],
        3 => [ 'id' => 1, 'stock_id' => 3, 'name' => 'biz' ],
    ]
]

So you can access the data like this: 因此,您可以像这样访问数据:

echo $output[1][3]['name']
// Will output `biz`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM