2

I'm sure this is a silly question, but I don't understand the reason. Why I cannot add values into the same array key? The values were overwritten.

$rows=array();
while($row = mysqli_fetch_array($result)) {
    $rows[strval($row['year'])]=array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']);
}
2
  • An array index is unique, so if you want to set different values on the same index, it will always overweite the original one. Commented Aug 12, 2015 at 8:03
  • @Epodax That only works with strings. Commented Aug 12, 2015 at 8:04

4 Answers 4

2

For now I'm assuming you want a multidimensional array with the year and inside each row with the month and satisfaction as array. You just have to append it by using $rows[strval($row['year']][], since each array key is unique and you will just overwrite it currently.

So your code would look like:

$rows=array();
while($row = mysqli_fetch_array($result)) {
    $rows[strval($row['year'])][] = array('month'=>$row['month'],'satisfaction'=>$row['sat_per_month']);
}
Sign up to request clarification or add additional context in comments.

Comments

1

In PHP (and almost all programming languages) array indexes are unique. This means that $arr["teletubbies"] cannot be "lala" and "noonoo" at the same time. This is for the simple reason that if you would call $arr["teletubbies"] at a later time PHP cannot know whether you meant "lala" or "noonoo" then.

To solve this issue (you want some things ordered by their year, e.g. 2014) it is advisable to make the value correlated to key 2014 a new array, thus creating a multidimensional array. You seem to understand that, but your implementation is wrong, as you are now just changing $rows[strval($row['year'])] to something new instead of appending the new values.

There are multiple solutions to this problem to do so:

Solution 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']);
}

Solution 2:

$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']));
}

There are probably several other solutions aswell, but you get the idea.

To call one of the $rows[strval($row['year'])] later, you must make sure you call the newly created indexes! For example:

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

or

echo $rows["2014"][3]["month"]." ".$rows["2014"][3]["satisfaction"];

Comments

1

Array index is unique. The code below should work for your case.

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

1 Comment

Ok, I'm really dumb.Thanks.
1

hope I understood your question correctly. One array key can store only one value (or one array of values) so:

$array[$key] = $value; //will overwrite what is under that key
$array[$key][] = $value //will add a value as a new sub-array node resulting in:
array(
    'key' => array (
        '0' => 'value',
    ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.