数组转换问题
程序员文章站
2024-01-27 13:51:34
...
array (0 => array ( 59 => '1', 17 => '1', 23 => '2', 35 => '3', 36 => '1', 40 => '4',43 => '1,3,7', 44 => '1,2,6,9', 70 => '15345178810', ), )
上面的数组如何转换成下面的
array (0 => array ( 59 => '1', 17 => '1', 23 => '2', 35 => '3', 36 => '1', 40 => '4', 43_1 => '1',43_3 => '1',43_7 => '1', 44_1 => '1',44_2 => '1',44_6 => '1',44_9 => '1', 70 => '15345178810', ), )
如果$a数组$a = array (0 =>array()1=>array().....)有多个是不是改成
上面的数组如何转换成下面的
array (0 => array ( 59 => '1', 17 => '1', 23 => '2', 35 => '3', 36 => '1', 40 => '4', 43_1 => '1',43_3 => '1',43_7 => '1', 44_1 => '1',44_2 => '1',44_6 => '1',44_9 => '1', 70 => '15345178810', ), )
回复讨论(解决方案)
$a = array (0 => array ( 59 => '1', 17 => '1', 23 => '2', 35 => '3', 36 => '1', 40 => '4',43 => '1,3,7', 44 => '1,2,6,9', 70 => '15345178810', ), );$R = array();foreach($a[0] as $k=>$v) { if(strpos($v, ',') !== false) { foreach(explode(',', $v) as $i) $r["{$k}_$i"] = 1; }else $r[$k] = $v;}print_r(array($r));
Array( [0] => Array ( [59] => 1 [17] => 1 [23] => 2 [35] => 3 [36] => 1 [40] => 4 [43_1] => 1 [43_3] => 1 [43_7] => 1 [44_1] => 1 [44_2] => 1 [44_6] => 1 [44_9] => 1 [70] => 15345178810 ))
$a = array (0 => array ( 59 => '1', 17 => '1', 23 => '2', 35 => '3', 36 => '1', 40 => '4',43 => '1,3,7', 44 => '1,2,6,9', 70 => '15345178810', ), );$R = array();foreach($a[0] as $k=>$v) { if(strpos($v, ',') !== false) { foreach(explode(',', $v) as $i) $r["{$k}_$i"] = 1; }else $r[$k] = $v;}print_r(array($r));
Array( [0] => Array ( [59] => 1 [17] => 1 [23] => 2 [35] => 3 [36] => 1 [40] => 4 [43_1] => 1 [43_3] => 1 [43_7] => 1 [44_1] => 1 [44_2] => 1 [44_6] => 1 [44_9] => 1 [70] => 15345178810 ))
如果$a数组$a = array (0 =>array()1=>array().....)有多个是不是改成
$R = array(); $h=0; foreach($arr[$h] as $k=>$v) { if(strpos($v, ',') !== false) { foreach(explode(',', $v) as $i) $r["{$k}_$i"] = 1; }else $r[$k] = $v; $h++; } print_r(array($r));
那需要再套一层循环
上一篇: 博客爬取系统,博客爬取_PHP教程
推荐阅读
-
Atitit.jpg png格式差别以及解决jpg图片不显示的问题 - attilaxAti
-
PHP中二维数组的排序方法
-
PHP Simple HTML DOM Parser的编码问题
-
深入理解PHP 数组之count 函数_php实例
-
狸窝全能视频转换器怎么修改默认保存位置无须每次手动修改
-
浅谈Python 中整型对象的存储问题
-
如何把数组的值变成健名形成新的数组。
-
php从数组中随机选择若干不重复元素的方法,php数组重复元素_PHP教程
-
search.taobao.com php数组函数序列之array_search- 按元素值返回键名
-
安装PHP可能遇到的问题“无法载入mysql扩展” 的解决方法_php技巧