這篇文章將為大家詳細(xì)講解有關(guān)php如何遍歷數(shù)組替換數(shù)組中的元素值,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)公司于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站設(shè)計、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元濟(jì)水街道做網(wǎng)站,已為上家服務(wù),為濟(jì)水街道各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220
php遍歷數(shù)組替換數(shù)組中元素值的方法:首先創(chuàng)建一個PHP示例文件;然后定義數(shù)組;最后通過“foreach($pid_arr_t as $key=>$value){...}”循環(huán)遍歷并替換數(shù)組中元素的值即可。
本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
php怎么遍歷數(shù)組替換數(shù)組中的元素的值?
php 修改數(shù)據(jù)中某個元素值,其它元素值不變。
循環(huán)數(shù)組
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的預(yù)售價格,售價區(qū)間為14.99-17.19萬元,成為長安首款轎跑SUV。" ["title"] => string(142) "測試點播最近長安剛剛公布了全新中型SUV CS85的預(yù)售價格,售價區(qū)間為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) "模板導(dǎo)入試卷測試一" ["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) "法學(xué)概論201704" ["title"] => string(18) "法學(xué)概論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的元素值
關(guān)于“php如何遍歷數(shù)組替換數(shù)組中的元素值”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。