我怎样才能改变这个数组
<pre>
Array ( [0] => 68,Off
[1] => 07,Do
[2] => 88,Ph
[3] => )
</pre>
对这个
<pre>
Array ( [68] => Off
[07] => Do
[88] => Ph
[3] => )
</pre>
请帮忙....
我怎样才能改变这个数组
<pre>
Array ( [0] => 68,Off
[1] => 07,Do
[2] => 88,Ph
[3] => )
</pre>
对这个
<pre>
Array ( [68] => Off
[07] => Do
[88] => Ph
[3] => )
</pre>
请帮忙....
试试这个
$temp=array();
foreach($arr as $each)
{
$val=explode(",",$each);
$temp[$val[0]]=$val[1];
}
echo "<pre>";print_r($temp);
如果您遇到任何问题,请告诉我。
尝试这个,
<?php
$array_1=your_array_here;
$array_2=array();
foreach($array_1 as $value){
$parts=explode(',',$value);
$array_2[$parts[0]]=$parts[1];
}
print_r($array_2);
?>
$finalArray = array();
foreach($arr as $row)
{
$newArr = explode(',',$row);
$finalArray[$newArr[0]] = $newArr[1];
}
print_r($finalArray);
最清晰和最简单的方法是创建一个新数组并循环遍历旧数组:
$new_array = array();
foreach ($old_array as $val) {
$val = explode(',', $val);
$new_array [$val[0]] = $val[1];
}
我的解决方案:
$array = array('68,Off', '07,Do', '88,Ph'); // Old array
$tidy = array(); // New array
for( $i = 0; $i < count($array); $i++ ){
$tmp = explode(",", $array[$i]);
$tidy[$tmp[0]] = $tmp[1];
}
有几种方法可以实现您想要的。这是其中之一。如果值中没有逗号,此解决方案将保留原始键。
<?php
$new_array = array();
foreach ($array as $key => $value) {
list($new_key, $new_value) = explode(",", $value);
if (!strstr($value, ",")) {
list($new_key, $new_value) = array($key, $value);
}
$new_array[$new_key] = $new_value;
}
print_r($new_array);
?>