0

I´ve already researched this, but the answers found did not solve my problem. I want to change all username items to 'kk', but printing the array afterwards show that nothing has been changed. What could be wrong?

<?php
$myArray = Array(
    0 => Array(
        'sender' => kk,
        'message' => hhiui,
        'timestamp' => '2017-02-04 10:04:57',
        'username' => '',
        'msgtype' => 0,
        'threadid' => 20737047302042017230457
    ) ,
    1 => Array(
        'sender' => kk,
        'message' => hhiui,
        'timestamp' => '2017-02-04 10:04:57',
        'username' => '',
        'msgtype' => 0,
        'threadid' => 20737047302042017230457
    ) ,
    2 => Array(
        'sender' => kk,
        'message' => hhiui,
        'timestamp' => '2017-02-04 10:04:57',
        'username' => '',
        'msgtype' => '16',
        'threadid' => 20737047302042017230457
    )
);

foreach($myArray as $value)
    {
    $value['username'] = "kk";
    }

print_r($myArray);

It just gives me:

Array
(
    [0] => Array
        (
            [sender] => kk
            [message] => hhiui
            [timestamp] => 2017-02-04 10:04:57
            [username] => 
            [msgtype] => 0
            [threadid] => 2.0737047302042E+22
        )

    [1] => Array
        (
            [sender] => kk
            [message] => hhiui
            [timestamp] => 2017-02-04 10:04:57
            [username] => 
            [msgtype] => 0
            [threadid] => 2.0737047302042E+22
         )

     [2] => Array
         (
             [sender] => kk
             [message] => hhiui
             [timestamp] => 2017-02-04 10:04:57
             [username] => 
             [msgtype] => 16
             [threadid] => 2.0737047302042E+22
        )

)
Array
(
     [2] => Array
         (
             [sender] => kk
             [message] => hhiui
             [timestamp] => 2017-02-04 10:04:57
             [username] => 
             [msgtype] => 16
             [threadid] => 2.0737047302042E+22
         )
Nils
  • 2,665
  • 15
  • 30
Corey Hart
  • 173
  • 1
  • 2
  • 15

3 Answers3

6

You should have done it that way:

foreach ($myArray as &$value)
{
   $value['username'] = "kk";
}   

Notice the & symbol. It gives you ability to change the array.

akond
  • 15,865
  • 4
  • 35
  • 55
2

use the reference operator & before the $value in your loop to edit array row

References in PHP are a means to access the same variable content by different names. http://php.net/manual/en/language.references.whatare.php

<?php
    $myArray = Array(
        0 => Array(
            'sender' =>"kk",
            'message' => "hhiui",
            'timestamp' => '2017-02-04 10:04:57',
            'username' => '',
            'msgtype' => 0,
            'threadid' => 20737047302042017230457
        ) ,
        1 => Array(
            'sender' => "kk",
            'message' => "hhiui",
            'timestamp' => '2017-02-04 10:04:57',
            'username' => '',
            'msgtype' => 0,
            'threadid' => 20737047302042017230457
        ) ,
        2 => Array(
            'sender' => "kk",
            'message' => "hhiui",
            'timestamp' => '2017-02-04 10:04:57',
            'username' => '',
            'msgtype' => '16',
            'threadid' => 20737047302042017230457
        )
    );

    foreach($myArray as &$value)
    {
          $value['username'] = "kk";
    }

    print_r($myArray);
Hamza Alayed
  • 635
  • 5
  • 17
adwairi
  • 181
  • 2
  • 8
1

Make this way:

foreach ($myArray as $key=>$value)
{
$myArray[$key]['username'] = "kk";
} 
L. Vadim
  • 539
  • 3
  • 12