Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将值添加到同一数组键中_Php_Arrays_Key - Fatal编程技术网

Php 将值添加到同一数组键中

Php 将值添加到同一数组键中,php,arrays,key,Php,Arrays,Key,我肯定这是个愚蠢的问题,但我不明白原因。为什么不能将值添加到同一数组键中?这些值被覆盖 $rows=array(); while($row = mysqli_fetch_array($result)) { $rows[strval($row['year'])]=array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']); } 数组索引是唯一的。下面的代码应该适用于您的案例 $rows=array()

我肯定这是个愚蠢的问题,但我不明白原因。为什么不能将值添加到同一数组键中?这些值被覆盖

$rows=array();
while($row = mysqli_fetch_array($result)) {
    $rows[strval($row['year'])]=array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']);
}

数组索引是唯一的。下面的代码应该适用于您的案例

$rows=array();
while($row = mysqli_fetch_array($result)) {
    $rows[strval($row['year'])][] = array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']);
}

现在我假设您想要一个多维数组,其中包含年份,每行中包含月份和满意度作为数组。您只需使用
$rows[strval($row['year']]][
来追加它,因为每个数组键都是唯一的,您将只在当前覆盖它

因此,您的代码如下所示:

$rows=array();
while($row = mysqli_fetch_array($result)) {
    $rows[strval($row['year'])][] = array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']);
}

希望我正确理解了您的问题。一个数组键只能存储一个值(或一个值数组),因此:

在PHP(以及几乎所有编程语言)中,数组索引都是唯一的。这意味着$arr[“天线宝宝”]不能同时是“lala”和“noonoo”。原因很简单,如果您稍后调用$arr[“天线宝宝”],PHP就不知道您是指“lala”还是“noonoo”

要解决此问题(您希望某些内容按年份排序,例如2014),建议将与键2014相关的值设置为一个新数组,从而创建一个多维数组。您似乎理解这一点,但您的实现是错误的,因为您现在只是更改$rows[strval($row['year'])]添加新值,而不是添加新值

此问题有多种解决方案:

解决方案1:

$rows=array();
while($row = mysqli_fetch_array($result)) {
    //The syntax [] means: insert after the last defined key, more information at: http://php.net/manual/en/language.types.array.php look for $arr[] = 56;
    $rows[strval($row['year'])][] = array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']);
}
$rows=array();
while($row = mysqli_fetch_array($result)) {
    //Make sure $rows[strval($row['year'])] is an array
    if (!isset($rows[strval($row['year'])])) $rows[strval($row['year'])] = Array();
    //array_push pushes a new array element to the last element of an array, documentation here: http://php.net/manual/en/function.array-push.php
    array_push($rows[strval($row['year'])],array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']));
}
解决方案2:

$rows=array();
while($row = mysqli_fetch_array($result)) {
    //The syntax [] means: insert after the last defined key, more information at: http://php.net/manual/en/language.types.array.php look for $arr[] = 56;
    $rows[strval($row['year'])][] = array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']);
}
$rows=array();
while($row = mysqli_fetch_array($result)) {
    //Make sure $rows[strval($row['year'])] is an array
    if (!isset($rows[strval($row['year'])])) $rows[strval($row['year'])] = Array();
    //array_push pushes a new array element to the last element of an array, documentation here: http://php.net/manual/en/function.array-push.php
    array_push($rows[strval($row['year'])],array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']));
}
也许还有其他几种解决方案,但你知道了

要在以后调用$row[strval($row['year'])中的一行,必须确保调用了新创建的索引!例如:

//Three indexes! 1st is the year, 2nd is the nth child you added and third is Array("month" => "..", "satisfaction" => "...");
print_r($rows["2014"][5]);


数组索引是唯一的,因此如果要在同一个索引上设置不同的值,它将始终超过原始索引的值。@Epodax,它只适用于字符串。好的,我真的很笨。谢谢。