Home  >  Article  >  Backend Development  >  数组转换问题

数组转换问题

WBOY
WBOYOriginal
2016-06-23 13:50:46886browse

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 = 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));

那需要再套一层循环

Statement:
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