user3612445
user3612445

Reputation: 145

Add values into same array key

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

Upvotes: 2

Views: 2147

Answers (4)

JohannesB
JohannesB

Reputation: 2015

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"];

Upvotes: 1

Auris
Auris

Reputation: 1329

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',
    ),
)

Upvotes: 1

Charlotte Dunois
Charlotte Dunois

Reputation: 4680

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

Upvotes: 2

wkyip
wkyip

Reputation: 500

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

Upvotes: 1

Related Questions