1

I have array with values from database

$values = $sql->fetchAll();

Which looks like this:

var_dump($values);

array (size=4)
  0 => 
    array (size=4)
      'id' => string '1' (length=1)
      0 => string '1' (length=1)
      'level' => string '0' (length=1)
      1 => string '0' (length=1)
  1 => 
    array (size=4)
      'id' => string '2' (length=1)
      0 => string '2' (length=1)
      'level' => string '3' (length=1)
      1 => string '3' (length=1)
  2 => 
    array (size=4)
      'id' => string '3' (length=1)
      0 => string '3' (length=1)
      'level' => string '0' (length=1)
      1 => string '0' (length=1)

What I am trying to do is to change level to for example 5, where player id = 3. I need to change both level related values. Number or array keys and their position may vary depending on SQL query. I need function which would take following arguments: $playerID, $keyName, $value and then it would change appropriate values.

For example:

function($playerID, $keyName, $value);
function(3, "level", 5);

Array after changes would look like this:

array (size=4)
  0 => 
    array (size=4)
      'id' => string '1' (length=1)
      0 => string '1' (length=1)
      'level' => string '0' (length=1)
      1 => string '0' (length=1)
  1 => 
    array (size=4)
      'id' => string '2' (length=1)
      0 => string '2' (length=1)
      'level' => string '3' (length=1)
      1 => string '3' (length=1)
  2 => 
    array (size=4)
      'id' => string '3' (length=1)
      0 => string '3' (length=1)
      'level' => string '5' (length=1)
      1 => string '5' (length=1)
Petr Hurtak
  • 2,239
  • 1
  • 19
  • 18
  • Can you rpivide us the `print_r($values)` or your array,`var_dump` is pretty confusing and clumsy here? – Aditya Apr 01 '14 at 17:05
  • Sure Array ( [0] => Array ( [id] => 1 [0] => 1 [level] => 0 [1] => 0 ) [1] => Array ( [id] => 2 [0] => 2 [level] => 3 [1] => 3 ) [2] => Array ( [id] => 3 [0] => 3 [level] => 0 [1] => 0 ) ) – Petr Hurtak Apr 01 '14 at 17:12

2 Answers2

1

Try something like this:

    function changeVal($values, $playerID, $keyName, $value)
    {
        foreach($values as $key => $val)
       {
           if ($val['id'] == $playerID)
           {
             $values[$key][$keyName] = $value;
             $index = array_search($keyName, array_keys($values))+1;
             $values[$key][$index] = $value;
           }
        }
    }

 changeVal($values , 3, "level", 5);
Aditya
  • 4,453
  • 3
  • 28
  • 39
  • this only changes values for "level" index but not for "1" index – Petr Hurtak Apr 01 '14 at 17:25
  • Yes, but this will not work if the level value represented by number index will be on different position, for example if I use different SQL query. I need more universal function. – Petr Hurtak Apr 01 '14 at 17:34
  • '**level value represented by number index will be on different position**' , can you please elaborate a bit? – Aditya Apr 01 '14 at 17:39
  • in this case index "level" and index "1" are both representing the same value so I need to change them both. – Petr Hurtak Apr 01 '14 at 17:49
  • I think what would be better is to find what position $keyName index has ("level" is 3rd index in this example) and change value of n+1 index. Is something like this possible? PS: indexes are always in this order 1: string representation (eg. "level") 2: number representation (eg. "1") – Petr Hurtak Apr 01 '14 at 18:01
0

PHP >= 5.5.0

function changeRow(&$rows, $playerID, $keyName, $value) {
    $key = array_search($playerID, array_column($rows, 'id'));
    $rows[$key][$keyName] = $value;
}

changeRow($values, 3, 'level', 5);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87