How to traverse an array in php and replace the values ​​of elements in the array

藏色散人
Release: 2023-03-11 08:14:02
Original
2167 people have browsed it

php method to traverse an array and replace the element value in the array: first create a PHP sample file; then define the array; finally pass "foreach($pid_arr_t as $key=>$value){...}" Just loop through and replace the values ​​of the elements in the array.

How to traverse an array in php and replace the values ​​of elements in the array

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php traverse the array and replace the elements in the array? The value of the element?

php Modify the value of a certain element in the data, leaving the values ​​of other elements unchanged.

Loop array

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"
  }
}
Copy after login

Method:

foreach($pid_arr_t as $key=>$value){
  $value['opid'] = 123;
  $pid_arr_t[$key] = $value;            
 }
Copy after login

Only modify the element value of opid

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to traverse an array in php and replace the values ​​of elements in the array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template