【发布时间】:2021-05-30 17:05:46
【问题描述】:
有没有办法自动创建数字?
例如,如果我知道我想要多少个数字,我可以这样做
for($i = 0; $i == 10; $i++)
{
$i++;
}
但对于我目前拥有的代码,我不知道该怎么做。
这是我的代码
$products = [
'prodcut_1' => [
'name' => 'Product 1',
],
'prodcut_2' => [
'name' => 'Product 2',
],
'prodcut_3' => [
'name' => 'Product 3',
]
];
$arr = [];
foreach($products as $key => $prodcut)
{
$arr[$key] = [
'number' => // the item number will go here
'name' => $item['name'],
'unit' => 'unit_'.$key,
'rate' => 'rate_'.$key
];
}
【问题讨论】:
-
通过
count函数可以得到数组的元素个数。在此基础上,您可以遍历数组。