简体   繁体   中英

how can i make a field value in an array the same as the array index

i have an array that obviously have indexes.. i want want to take each array index an assign as a value in my array.. this is how my array looks like

Array
(
[0] => Array
    (
        [id] => 20
        [CLASS] => 1234
        [REGISTER] => 13
     )

[1] => Array
    (
        [id] => 11
        [CLASS] => SEC
        [REGISTER] => 3
   )
)

want i want is to use the indexes and put them in the REGISTER field like this

    Array
(
[0] => Array
    (
        [id] => 20
        [CLASS] => 1234
        [REGISTER] => 0
     )

[1] => Array
    (
        [id] => 11
        [CLASS] => SEC
        [REGISTER] => 1
   )
)

Just iterate over this array like this:

foreach($a as $k=>$v){
    $a[$k]['REGISTER'] = $k;
}

You can use below code to assign your key index to your selected value.

foreach($arr as $key=>$value):
    $arr[$key]['REGISTER'] = $key;
endforeach;
print_r($arr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM