Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP: Replace Associative Array Values with Values from indexed Array

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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.

>Solution :

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];
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading