帮忙看看哪里错了,循环改数组中某值
程序员文章站
2022-06-10 10:30:25
...
dump($arr);
array(10) {
[0] => array(21) {
["id"] => string(2) "88"
........省略.........
["pu_pic"] => string(74) "1507/p_5594a950e92577.jpg|1507/p_5594a957e23c6.jpg|1507/p_5594a9518888.jpg"
........省略.........
}
[1] => array(21) {
[0] => array(21) {
["id"] => string(2) "88"
........省略.........
["pu_pic"] => string(74) "1507/p_5594a950e1789.jpg|1507/p_4444a951e23c6.jpg|1507/p_5594a9544fb44.jpg"
........省略.........
}
[2] => array(21) {
[0] => array(21) {
["id"] => string(2) "88"
........省略.........
["pu_pic"] => string(74) "1507/p_5594a950e1789.jpg|1507/p_4444a951e23c6.jpg|1507/p_5594a9544fb44.jpg"
........省略.........
}
源码
循环改变$arr["pu_pic"]中的值
for($i=0;$i if(strlen($arr[i]['pu_pic'])>30) //统计$arr["pu_pic"]中字符串个数
{
$thumb=explode('|',$arr[i]['pu_pic']);
$arr[i]['pu_pic']=$thumb[0];
}
dump($thumb);
}
dump($thumb)为空值;
dump($arr); 中的值也没有改变
我片段中的数组是贴错了。不好意思!
整个数组$arr是一个二维数组
array(10) {
[0] => array(21) {
["id"] => string(2) "74"
["pu_bt"] => string(14) "ssssssssssssss"
["pu_sfzj"] => string(1) "1"
["pu_lx"] => string(1) "1"
["pu_xjy"] => string(1) "0"
["pu_xjyb"] => string(0) ""
["pu_mj"] => string(2) "12"
["pu_zj"] => string(4) "3000"
["pu_qu"] => string(6) "天河"
["pu_qub"] => string(6) "车陂"
["pu_dz"] => string(0) ""
["pu_dh"] => string(7) "5555555"
["pu_lxr"] => string(0) ""
["pu_sm"] => string(0) ""
["pu_gk"] => string(0) ""
["pu_pic"] => string(74) "1507/p_55963e0e9685b.jpg|1507/p_55963e0eb388d.jpg|1507/p_55963e0ece755.jpg"
["puphoto1"] => string(0) ""
["puphoto2"] => string(0) ""
["puphoto3"] => string(0) ""
["thumburl"] => string(0) ""
["pu_time"] => string(10) "1435909646"
}
[1] => array(21) {
["id"] => string(2) "60"
["pu_bt"] => string(69) "非中介真实可靠!白云区广花路大型KTV酒吧转让出售"
["pu_sfzj"] => string(1) "1"
["pu_lx"] => string(1) "3"
["pu_xjy"] => string(3) "127"
["pu_xjyb"] => string(0) ""
["pu_mj"] => string(1) "4"
["pu_zj"] => string(5) "78000"
["pu_qu"] => string(1) "5"
["pu_qub"] => string(6) "广花"
["pu_dz"] => string(15) "广花大马路"
["pu_dh"] => string(11) "13660073733"
["pu_lxr"] => string(6) "阿娇"
["pu_sm"] => string(0) ""
["pu_gk"] => string(267) "白云区广花路现在有一处2600平方米营利中酒吧,证照齐全,内有大型KTV包房45间,加酒吧大厅,客源稳定,含路边带租约商铺7间,还有10年租期,现因老板全家移民,如有意者请电话联系,可前来考察。"
["pu_pic"] => string(74) "1507/p_55963e0e9685b.jpg|1507/p_55963e0eb388d.jpg|1507/p_55963e0ece755.jpg"
["puphoto1"] => string(0) ""
["puphoto2"] => string(0) ""
["puphoto3"] => string(0) ""
["thumburl"] => string(0) ""
["pu_time"] => string(10) "1434629313"
}
没有问题呀
没有问题呀
感谢!问题解决了。通过把您的代码和我写的代码,逐行对比,发现是我粗心大意造成的。把 【 $i 】写成了【 i 】
array(10) {
[0] => array(21) {
["id"] => string(2) "88"
........省略.........
["pu_pic"] => string(74) "1507/p_5594a950e92577.jpg|1507/p_5594a957e23c6.jpg|1507/p_5594a9518888.jpg"
........省略.........
}
[1] => array(21) {
[0] => array(21) {
["id"] => string(2) "88"
........省略.........
["pu_pic"] => string(74) "1507/p_5594a950e1789.jpg|1507/p_4444a951e23c6.jpg|1507/p_5594a9544fb44.jpg"
........省略.........
}
[2] => array(21) {
[0] => array(21) {
["id"] => string(2) "88"
........省略.........
["pu_pic"] => string(74) "1507/p_5594a950e1789.jpg|1507/p_4444a951e23c6.jpg|1507/p_5594a9544fb44.jpg"
........省略.........
}
源码
循环改变$arr["pu_pic"]中的值
for($i=0;$i
{
$thumb=explode('|',$arr[i]['pu_pic']);
$arr[i]['pu_pic']=$thumb[0];
}
dump($thumb);
}
dump($thumb)为空值;
dump($arr); 中的值也没有改变
回复讨论(解决方案)
认真检查一下你的 $arr 数组
在你贴出的片段中,除 $arr[0] 是一维数组外,其他的都是二维数组
你的代码是按 $arr[n] 是一维数组设计的,显然除了 $arr[0] 以外都不会进入修改代码的
认真检查一下你的 $arr 数组
在你贴出的片段中,除 $arr[0] 是一维数组外,其他的都是二维数组
你的代码是按 $arr[n] 是一维数组设计的,显然除了 $arr[0] 以外都不会进入修改代码的
我片段中的数组是贴错了。不好意思!
整个数组$arr是一个二维数组
array(10) {
[0] => array(21) {
["id"] => string(2) "74"
["pu_bt"] => string(14) "ssssssssssssss"
["pu_sfzj"] => string(1) "1"
["pu_lx"] => string(1) "1"
["pu_xjy"] => string(1) "0"
["pu_xjyb"] => string(0) ""
["pu_mj"] => string(2) "12"
["pu_zj"] => string(4) "3000"
["pu_qu"] => string(6) "天河"
["pu_qub"] => string(6) "车陂"
["pu_dz"] => string(0) ""
["pu_dh"] => string(7) "5555555"
["pu_lxr"] => string(0) ""
["pu_sm"] => string(0) ""
["pu_gk"] => string(0) ""
["pu_pic"] => string(74) "1507/p_55963e0e9685b.jpg|1507/p_55963e0eb388d.jpg|1507/p_55963e0ece755.jpg"
["puphoto1"] => string(0) ""
["puphoto2"] => string(0) ""
["puphoto3"] => string(0) ""
["thumburl"] => string(0) ""
["pu_time"] => string(10) "1435909646"
}
[1] => array(21) {
["id"] => string(2) "60"
["pu_bt"] => string(69) "非中介真实可靠!白云区广花路大型KTV酒吧转让出售"
["pu_sfzj"] => string(1) "1"
["pu_lx"] => string(1) "3"
["pu_xjy"] => string(3) "127"
["pu_xjyb"] => string(0) ""
["pu_mj"] => string(1) "4"
["pu_zj"] => string(5) "78000"
["pu_qu"] => string(1) "5"
["pu_qub"] => string(6) "广花"
["pu_dz"] => string(15) "广花大马路"
["pu_dh"] => string(11) "13660073733"
["pu_lxr"] => string(6) "阿娇"
["pu_sm"] => string(0) ""
["pu_gk"] => string(267) "白云区广花路现在有一处2600平方米营利中酒吧,证照齐全,内有大型KTV包房45间,加酒吧大厅,客源稳定,含路边带租约商铺7间,还有10年租期,现因老板全家移民,如有意者请电话联系,可前来考察。"
["pu_pic"] => string(74) "1507/p_55963e0e9685b.jpg|1507/p_55963e0eb388d.jpg|1507/p_55963e0ece755.jpg"
["puphoto1"] => string(0) ""
["puphoto2"] => string(0) ""
["puphoto3"] => string(0) ""
["thumburl"] => string(0) ""
["pu_time"] => string(10) "1434629313"
}
$arr = array( array( "pu_pic" => "1507/p_55963e0e9685b.jpg|1507/p_55963e0eb388d.jpg|1507/p_55963e0ece755.jpg"), array( "pu_pic" => "1507/p_55963e0e9685b.jpg|1507/p_55963e0eb388d.jpg|1507/p_55963e0ece755.jpg"),);for($i=0; $i30) { $thumb = explode('|', $arr[$i]['pu_pic']); $arr[$i]['pu_pic'] = $thumb[0]; }}print_r($arr);
Array( [0] => Array ( [pu_pic] => 1507/p_55963e0e9685b.jpg ) [1] => Array ( [pu_pic] => 1507/p_55963e0e9685b.jpg ))
没有问题呀
$arr = array( array( "pu_pic" => "1507/p_55963e0e9685b.jpg|1507/p_55963e0eb388d.jpg|1507/p_55963e0ece755.jpg"), array( "pu_pic" => "1507/p_55963e0e9685b.jpg|1507/p_55963e0eb388d.jpg|1507/p_55963e0ece755.jpg"),);for($i=0; $i30) { $thumb = explode('|', $arr[$i]['pu_pic']); $arr[$i]['pu_pic'] = $thumb[0]; }}print_r($arr);
Array( [0] => Array ( [pu_pic] => 1507/p_55963e0e9685b.jpg ) [1] => Array ( [pu_pic] => 1507/p_55963e0e9685b.jpg ))
没有问题呀
感谢!问题解决了。通过把您的代码和我写的代码,逐行对比,发现是我粗心大意造成的。把 【 $i 】写成了【 i 】
上一篇: Linux系统安装php扩展cURL
下一篇: php函数in_array奇怪现象
推荐阅读