Home > Blockchain >  PHP: Replace Associative Array Values with Values from indexed Array
PHP: Replace Associative Array Values with Values from indexed Array

Time:01-23

how can i replace values of an associative array with values from a indexed Array? i tried different things like array_replace, array_keys or array_combine but got to no clue yet

this is my given array in which i want to replace all values of key int

[0] => Array
    (
        [name] => test1
        [counter] => 17875858
    )

[1] => Array
    (
        [name] => test2
        [counter] => 10189639
    )

[2] => Array
    (
        [name] => test3
        [counter] => 8946741
    )

[3] => Array
    (
        [name] => test4
        [counter] => 7343543
    )

[4] => Array
    (
        [name] => test5
        [counter] => 7211592
    )

[5] => Array
    (
        [name] => test6
        [counter] => 6002602
    )

[6] => Array
    (
        [name] => test7
        [counter] => 5390961
    )

[7] => Array
    (
        [name] => test8
        [counter] => 4837229
    )

[8] => Array
    (
        [name] => test9
        [counter] => 4720184
    )

[9] => Array
    (
        [name] => test10
        [counter] => 4569563
    )

and this is my array with the values i want to insert in the corresponding index/key of the one above

[0] => -17768795
[1] => -10125149
[2] => -8886641
[3] => -7293128
[4] => -7163317
[5] => -5958945
[6] => -5351903
[7] => -4801531
[8] => -4690188
[9] => -4540440

can someone help me? i looked up several threads but found no solution yet, sorry if this is a duplicate.

CodePudding user response:

Just use loops. Let $arr1 to be the first array in your question, and $arr2 to be the 2nd array, with the values that you need to put in $arr1

for($i=0;$i<count($arr2);$i  ){
    $arr1[$i]['counter']=$arr2[$i];
}
  • Related