从以下数组:
Array
(
[0] => Array
(
[tag_name] => Quant-Arithmetic
[student_marks] => 1.05
[total_marks] => 2.00
[student_count] => 1
[level] => Easy
[overall_percentage] => 52
)
[1] => Array
(
[tag_name] => Quant-Arithmetic
[student_marks] => 1.10
[total_marks] => 4.00
[student_count] => 1
[level] => Medium
[overall_percentage] => 28
)
[2] => Array
(
[tag_name] => Quant-Algebra
[student_marks] => 0.20
[total_marks] => 1.00
[student_count] => 1
[level] => Easy
[overall_percentage] => 20
)
[3] => Array
(
[tag_name] => Quant-Algebra
[student_marks] => 1.00
[total_marks] => 6.00
[student_count] => 1
[level] => Medium
[overall_percentage] => 17
)
[4] => Array
(
[tag_name] => Quant-Algebra
[student_marks] => 1.50
[total_marks] => 6.00
[student_count] => 1
[level] => Hard
[overall_percentage] => 25
)
)
对于类似的东西:因为我需要一个基于标签名称的数组,其中包含该数组中的所有级别,以便我可以获得具有其级别和百分比的单个标记名称以进行进一步计算
[tag_name]
{
[level]:[overall_percentage]
[level]:[overall_percentage]
.
.
}
.
.
.
答案 0 :(得分:2)
$newArray = array();
foreach($yourarray as $item){
$newArray[$item['tag_name']][$item['level']] = $item['overall_percentage'];
}
这将添加如下创建$ newArray:
Array(
'tag_name' => Array(
'Medium' => '27'
)
)
答案 1 :(得分:0)
试试这段代码
$array =array
(
'0' => array
(
'tag_name' => 'Quant-Arithmetic',
'student_marks' => ' 1.05',
'total_marks' => ' 2.00',
'student_count' => ' 1',
'level' => ' Easy',
'overall_percentage' => ' 52',
),
'1' => array
(
'tag_name' => ' Quant-Arithmetic',
'student_marks' => ' 1.10',
'total_marks' => ' 4.00',
'student_count' => ' 1',
'level' => ' Medium',
'overall_percentage' => ' 28',
),
'2' => array
(
'tag_name' => ' Quant-Algebra',
'student_marks' => ' 0.20',
'total_marks' => ' 1.00',
'student_count' => ' 1',
'level' => ' Easy',
'overall_percentage' => ' 20',
)
);
// declare a new array
$new_array = array();
foreach($array as $values){
// trim array elements
$trimmed_array=array_map('trim',$values);
if(isset($new_array[$trimmed_array['tag_name']])){
//add elements to array
$new_array[$trimmed_array['tag_name']][$trimmed_array['level']] = $trimmed_array['overall_percentage'];
}else{
// create new array
$new_array[$trimmed_array['tag_name']] = array($trimmed_array['level'] => $trimmed_array['overall_percentage']);
}
}
// php array
print_r($new_array);
// json
echo json_encode($new_array);
Out Put:array
Array
(
[Quant-Arithmetic] => Array
(
[Easy] => 52
[Medium] => 28
)
[Quant-Algebra] => Array
(
[Easy] => 20
)
)
Out Put:Json
{"Quant-Arithmetic":{"Easy":"52","Medium":"28"},"Quant-Algebra":{"Easy":"20"}}