how can add item to array? i tried this:
for($i = 0; $i < sizeof($results); $i++)
{
$results[$i][] = array('test' => 'sdsa');
}
print_r($results);
but the result is this for each element ->
Array(
[0] => Array(
[playerid] => 0
[nickname] => Jeffrey_Westh
[score] => 2
[ping] => 123
[0] => Array (
[test] => sdsa
)
)
i and need make this:
Array(
[0] => Array(
[playerid] => 0
[nickname] => Jeffrey_Westh
[score] => 2
[ping] => 123
[test] => sdsa
)
Using the []
would always add a new element into the array. In your case a new unindexed key is created and given the value of array('test' => 'sdsa')
. Then the 0
index is given to the element.
You actually need to use test
key. And please move sizeof
out of for
loop, this gives performance issues.
$size = sizeof($results);
for($i = 0; $i < $size; $i++)
{
$results[$i]['test'] = 'sdsa';
}
print_r($results);
Simply put:
for($i = 0; $i < sizeof($results); $i++)
{
$results[$i]["test"] = "sdsa";
}
print_r($results);