0

I got an array with values splits to 5 arrays with groups of 3 values, then I got more group of array of pointers, what I'm trying to do is replacing the pointed array with other value.

what I got is :

$a = array(["4", "3", "5"], ["4", "8", "11"], ["4", "11", "5"], ["4", "5", "5"], ["4", "4", "9"]);

$b = array(12); // the value to be placed

$pointers = array([1,2],[2,2],[2,1]);

foreach($pointers as $point)
{
    $a1 = array_replace($a[$point[0]], array($point[1] => $b ));
}

Expected results:

["4", "3", "5"], ["4", "8", "12"], ["4", "12", "12"], ["4", "5", "5"], ["4", "4", "9"]

What I need is to keep the array structure as is and just replace the pointed values to be 12.

I'm not sure its the right way.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Assaf Schwartz
  • 119
  • 1
  • 7

1 Answers1

1

You don't need to use array_replace(), just assign using the indexes in $pointers.

$a = array(["4", "3", "5"], ["4", "8", "11"], ["4", "11", "5"], ["4", "5", "5"], ["4", "4", "9"]);
$b = 12; // the value to be placed
$pointers = array([1,2],[2,2],[2,1]);

foreach($pointers as [$p1, $p2])
{
    $a[$p1][$p2] = $b;
}
print_r($a);
Barmar
  • 741,623
  • 53
  • 500
  • 612