Home > Back-end >  how to replace arrays by pointers from other array
how to replace arrays by pointers from other array

Time:08-01

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 im 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

im not sure its the right way :)

thanks for any help

CodePudding user response:

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);
  • Related