Php 未指定值的数组
您是否可以帮助了解为什么没有将该值分配给数组元素 我的阵列打印如下所示:Php 未指定值的数组,php,arrays,Php,Arrays,您是否可以帮助了解为什么没有将该值分配给数组元素 我的阵列打印如下所示: Array ( [0] => Array ( [month] => May [year] => 2014 [count] => 0 ) [1] => Array ( [month] => Apr [year] => 2014 [count] => 0 ) [2] => Array ( [month] => Mar [year] => 2014 [count]
Array ( [0] => Array ( [month] => May [year] => 2014 [count] => 0 ) [1] => Array ( [month] => Apr [year] => 2014 [count] => 0 ) [2] => Array ( [month] => Mar [year] => 2014 [count] => 0 ) [3] => Array ( [month] => Feb [year] => 2014 [count] => 0 ) [4] => Array ( [month] => Jan [year] => 2014 [count] => 0 ) [5] => Array ( [month] => Dec [year] => 2013 [count] => 0 ) [6] => Array ( [month] => Nov [year] => 2013 [count] => 0 ) [7] => Array ( [month] => Oct [year] => 2013 [count] => 0 ) [8] => Array ( [month] => Sep [year] => 2013 [count] => 0 ) [9] => Array ( [month] => Aug [year] => 2013 [count] => 0 ) [10] => Array ( [month] => Jul [year] => 2013 [count] => 0 ) [11] => Array ( [month] => Jun [year] => 2013 [count] => 0 ) )
我有一些代码,应该添加一个值来计数
foreach($archives as $archive){
if ($archive['month'] == date('M', strtotime($post['timestamp']))){
echo 'hello';
$archive['count']++;
}
}
已输出“hello”,但count未接收该值。我也试过了,但没用
$archive['count'] = 'hello';
谢谢您的帮助。您应该使用
&
直接修改foreach
循环中的数组元素:
foreach($archives as &$archive){
if ($archive['month'] == date('M', strtotime($post['timestamp']))){
echo 'hello';
$archive['count']++;
}
}
在这种情况下,
$archive
将通过引用进行分配同时使用foreach
循环后,$archive
的引用和最后一个数组元素仍然存在,因此建议取消设置它。您应该使用&
直接修改foreach
循环中的数组元素:
foreach($archives as &$archive){
if ($archive['month'] == date('M', strtotime($post['timestamp']))){
echo 'hello';
$archive['count']++;
}
}
在这种情况下,$archive
将通过引用进行分配同时使用foreach
循环后,$archive
的引用和最后一个数组元素仍然存在,因此建议取消设置它。您应该使用&
直接修改foreach
循环中的数组元素:
foreach($archives as &$archive){
if ($archive['month'] == date('M', strtotime($post['timestamp']))){
echo 'hello';
$archive['count']++;
}
}
在这种情况下,$archive
将通过引用进行分配同时使用foreach
循环后,$archive
的引用和最后一个数组元素仍然存在,因此建议取消设置它。您应该使用&
直接修改foreach
循环中的数组元素:
foreach($archives as &$archive){
if ($archive['month'] == date('M', strtotime($post['timestamp']))){
echo 'hello';
$archive['count']++;
}
}
在这种情况下,$archive
将通过引用进行分配还使用foreach
循环后,$archive的引用
和最后一个数组元素仍然存在,因此建议取消设置它。这是因为foreach
迭代数组的浅层副本。使用foreach($archives as&$archive)
引用原始数组的值。这是因为foreach
迭代数组的浅层副本。使用foreach($archives as&$archive)
引用原始数组的值。这是因为foreach
迭代数组的浅层副本。使用foreach($archives as&$archive)
引用原始数组的值。这是因为foreach
迭代数组的浅层副本。使用foreach($archives as&$archive)
引用原始数组的值。您可以尝试在不使用的情况下执行此操作。
使用下一个代码
foreach($archives as $key => $archive){
if ($archive['month'] == date('M', strtotime($post['timestamp']))){
echo 'hello';
$archives[$key]['count']++;
}
}
您可以尝试在不使用的情况下执行此操作&
使用下一个代码
foreach($archives as $key => $archive){
if ($archive['month'] == date('M', strtotime($post['timestamp']))){
echo 'hello';
$archives[$key]['count']++;
}
}
您可以尝试在不使用的情况下执行此操作&
使用下一个代码
foreach($archives as $key => $archive){
if ($archive['month'] == date('M', strtotime($post['timestamp']))){
echo 'hello';
$archives[$key]['count']++;
}
}
您可以尝试在不使用的情况下执行此操作&
使用下一个代码
foreach($archives as $key => $archive){
if ($archive['month'] == date('M', strtotime($post['timestamp']))){
echo 'hello';
$archives[$key]['count']++;
}
}