如何像这样转换数组
Array ([0] => title=Addition Worksheet [1] => operator=+ [2] => numOfQue=6 [3] => final_result=positive [4] => marks=3 [5] => numOfQueSet=3 [6] => orientation=vertical [7] => fnum=1 [8] => snum=1 )
到
Array('title'=>'Addition','operator'=>'+','numOfQue'=>'6','final_result'=>'positive','marks'=>'3','numOfQueSet'=>'3','orientation'=>'vertical','fnum'=>'1','snum'=>'1')
答案 0 :(得分:1)
我创建这样的样本
$array = array(
0 => 'Title=Addition worksheet',
1 => 'operator=+',
2 => 'numOfQue=6',
3 => 'final_result=positive',
4 => 'marks=3',
5 => 'numOfQueSet=3',
6 => 'orientation=vertical',
7 => 'fnum=1',
8 => 'snum=1'
);
$arr = array();
foreach($array as $a)
{
$e = explode('=', $a);
$arr[$e[0]] = $e[1];
}
var_dump($arr);
答案 1 :(得分:1)
我正在为您更新解决方案,首先要问问题是在google上找到解决方案。
$input_arr = array("title=Addition Worksheet", "operator=+", "numOfQue=6", "final_result=positive", "marks=3", "numOfQueSet=3", "orientation=vertical", "fnum=1", "snum=1" );
foreach($input_arr as $arr){
$val = explode('=',$arr);
$output_arr[$val[0]]= $val[1];
}
echo "<pre>"; print_r($output_arr); exit;
答案 2 :(得分:0)
使用foreach()
读取数组并使用=
分解值以获取数组的键和值并将其分配给新数组。
$arr = array (0 => 'title=Addition Worksheet',
1 => 'operator=+',
2 => 'numOfQue=6',
3 => 'final_result=positive',
4 => 'marks=3',
5 => 'numOfQueSet=3',
6 => 'orientation=vertical',
7 => 'fnum=1',
8 => 'snum=1' );
$arr2=[];
foreach($arr as $val){
list($key, $value) = explode('=', $val);
//echo "\n".$key.' = '.$value;
$arr2[trim($key)] = trim($value);
}
print_r($arr2);
答案 3 :(得分:0)
$oldArr=array('title=Addition Worksheet','operator=+','numOfQue=6','final_result=positive','marks=3','numOfQueSet=3','orientation=vertical','fnum=1','snum=1' );
$newArr=array();
foreach($oldArr as $row){
$rowArr=explode('=',$row);
$newArr[$rowArr[0]]=$rowArr[1];
}
print_r($newArr);