登录  /  注册

php怎么遍历数组替换数组中的元素的值

藏色散人
发布: 2021-07-05 10:03:32
原创
1930人浏览过

php遍历数组替换数组中元素值的方法:首先创建一个php示例文件;然后定义数组;最后通过“foreach($pid_arr_t as $key=>$value){...}”循环遍历并替换数组中元素的值即可。

php怎么遍历数组替换数组中的元素的值

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

php怎么遍历数组替换数组中的元素的值?

php 修改数据中某个元素值,其它元素值不变。

循环数组

array(8) {
  [0] => array(10) {
    ["opid"] => int(0)
    ["pid"] => int(11)
    ["price"] => string(4) "0.10"
    ["controller"] => string(4) "Live"
    ["type_mold"] => int(1)
    ["img"] => string(0) ""
    ["description"] => string(4) "1212"
    ["title"] => string(12) "测试回放"
    ["user_id"] => int(51)
    ["limit"] => string(19) "2020-01-25 15:03:14"
  }
  [1] => array(10) {
    ["opid"] => int(0)
    ["pid"] => int(10)
    ["price"] => string(4) "0.10"
    ["controller"] => string(4) "Live"
    ["type_mold"] => int(1)
    ["img"] => string(0) ""
    ["description"] => string(2) "12"
    ["title"] => string(12) "线上测试"
    ["user_id"] => int(51)
    ["limit"] => string(19) "2019-10-17 15:03:14"
  }
  [2] => array(10) {
    ["opid"] => int(0)
    ["pid"] => int(3)
    ["price"] => string(4) "1.00"
    ["controller"] => string(4) "Live"
    ["type_mold"] => int(1)
    ["img"] => string(0) ""
    ["description"] => string(4) "1212"
    ["title"] => string(21) "测试保长时直播"
    ["user_id"] => int(51)
    ["limit"] => string(19) "2019-10-17 15:03:14"
  }
  [3] => array(10) {
    ["opid"] => int(0)
    ["pid"] => int(2)
    ["price"] => string(4) "0.10"
    ["controller"] => string(5) "Video"
    ["type_mold"] => int(2)
    ["img"] => string(0) ""
    ["description"] => string(130) "最近长安刚刚公布了全新中型SUV CS85的预售价格,售价区间为14.99-17.19万元,成为长安首款轿跑SUV。"
    ["title"] => string(142) "测试点播最近长安刚刚公布了全新中型SUV CS85的预售价格,售价区间为14.99-17.19万元,成为长安首款轿跑SUV。"
    ["user_id"] => int(51)
    ["limit"] => string(19) "2019-10-17 15:03:14"
  }
  [4] => array(10) {
    ["opid"] => int(0)
    ["pid"] => int(8)
    ["price"] => string(4) "0.00"
    ["controller"] => string(4) "Shop"
    ["type_mold"] => int(4)
    ["img"] => string(0) ""
    ["description"] => string(0) ""
    ["title"] => string(3) "111"
    ["user_id"] => int(51)
    ["limit"] => string(19) "2019-10-17 15:03:14"
  }
  [5] => array(10) {
    ["opid"] => int(0)
    ["pid"] => int(61)
    ["price"] => string(4) "0.00"
    ["controller"] => string(9) "Testpaper"
    ["type_mold"] => int(3)
    ["img"] => string(45) "20190403/c378d2084ec9c3f05ceab36c00ac67a4.png"
    ["description"] => string(6) "健康"
    ["title"] => string(12) "健康管理"
    ["user_id"] => int(51)
    ["limit"] => string(19) "2022-01-14 15:03:14"
  }
  [6] => array(10) {
    ["opid"] => int(0)
    ["pid"] => int(55)
    ["price"] => string(4) "0.00"
    ["controller"] => string(9) "Testpaper"
    ["type_mold"] => int(3)
    ["img"] => string(45) "20190330/af4db555769d71589ae163bfd0a3c05b.jpg"
    ["description"] => string(3) "111"
    ["title"] => string(27) "模板导入试卷测试一"
    ["user_id"] => int(51)
    ["limit"] => string(19) "2019-05-01 15:03:14"
  }
  [7] => array(10) {
    ["opid"] => int(0)
    ["pid"] => int(62)
    ["price"] => string(7) "1000.00"
    ["controller"] => string(9) "Testpaper"
    ["type_mold"] => int(3)
    ["img"] => string(45) "20190330/f1cc9c7df38108c505338a529e3d06a6.jpg"
    ["description"] => string(18) "法学概论201704"
    ["title"] => string(18) "法学概论201704"
    ["user_id"] => int(51)
    ["limit"] => string(19) "2019-07-29 15:03:14"
  }
}
登录后复制

方法:

foreach($pid_arr_t as $key=>$value){
  $value['opid'] = 123;
  $pid_arr_t[$key] = $value;            
 }
登录后复制

只有修改opid的元素值

推荐学习:《PHP视频教程

以上就是php怎么遍历数组替换数组中的元素的值的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号