如何使数组中的字段值与数组索引相同(how can i make a field value in an array the same as the array index)

编程入门 行业动态 更新时间:2024-10-28 00:21:03
如何使数组中的字段值与数组索引相同(how can i make a field value in an array the same as the array index)

我有一个显然有索引的数组..我想要将每个数组索引一个赋值作为我的数组中的值..这就是我的数组看起来像

Array ( [0] => Array ( [id] => 20 [CLASS] => 1234 [REGISTER] => 13 ) [1] => Array ( [id] => 11 [CLASS] => SEC [REGISTER] => 3 ) )

我想要的是使用索引并将它们放在REGISTER字段中

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

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

最满意答案

只需迭代这个数组,如下所示:

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

Just iterate over this array like this:

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

更多推荐

本文发布于:2023-07-25 10:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1259972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字段   组中   索引   index

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!