我已经困惑了好几个小时。所以,我有这个数组
$data = array(
array(
'base_id' => 14,
'offset' => 1,
'name' => 'rental',
'value' => 200
),
array(
'base_id' => 14,
'offset' => 1,
'name' => 'tax',
'value' => 20
),
array(
'base_id' => 14,
'offset' => 1,
'name' => 'misc',
'value' => 10
),
array(
'base_id' => 13,
'offset' => 5,
'name' => 'rental',
'value' => 150
),
array(
'base_id' => 13,
'offset' => 5,
'name' => 'tax',
'value' => 15
),
array(
'base_id' => 13,
'offset' => 5,
'name' => 'misc',
'value' => 5
),
array(
'base_id' => 13,
'offset' => 7,
'name' => 'rental',
'value' => 170
)
);
我希望按base_id和偏移对它们进行分组,以便变得像这样
$result = array(
array(
array(
'base_id' => 14,
'offset' => 1,
'name' => 'rental',
'value' => 200
),
array(
'base_id' => 14,
'offset' => 1,
'name' => 'tax',
'value' => 20
),
array(
'base_id' => 14,
'offset' => 1,
'name' => 'misc',
'value' => 10
),
),
array(
array(
'base_id' => 13,
'offset' => 5,
'name' => 'rental',
'value' => 150
),
array(
'base_id' => 13,
'offset' => 5,
'name' => 'tax',
'value' => 15
),
array(
'base_id' => 13,
'offset' => 5,
'name' => 'misc',
'value' => 5
),
),
array(
array(
'base_id' => 13,
'offset' => 7,
'name' => 'rental',
'value' => 170
)
)
);
如何实现? php中的任何函数都可以做到这一点,或者只是使用循环。需要你的帮助,谢谢。
答案 0 :(得分:0)
迭代您的数据并检查base_id是13还是14。
$_14_result = array();
$_13_result = array();
foreach($data as $key=> $d){
if($d["base_id"] == 14)
$_14_result[] = $d;
else
$_13_result[] = $d;
}
将base_id 13,14结果合并到数组
$result[] = $_14_result;
$result[] = $_13_result;
print_r($result);
答案 1 :(得分:0)
没有执行此工作的php函数,但我为此porpuse提供了一个脚本:
$refrenceArray = array();
$outputArray = array();
$refrenceIndex = 0;
foreach($data as $key=>$value)
{
if(!isset($refrenceArray[$value['base_id']]))
{
$refrenceArray[$value['base_id']] = $refrenceIndex;
$refrenceIndex++;
}
$outputArray[$refrenceArray[$value['base_id']]][] = $value;
}
答案 2 :(得分:0)
解决方案是,
foreach
循环根据base_id
和offset
键对所有数组进行分组。array_merge()
应用于结果数组。所以你的代码应该是这样的:
// Suppose $data is your original array
$result_array = array();
foreach($data as $arr){
$result_array[$arr['base_id']][$arr['offset']][] = $arr;
}
$result_array = call_user_func_array('array_merge', $result_array);
// display $result_array
echo '<pre>';
print_r($result_array);
echo '</pre>';
以下是Live Demo
答案 3 :(得分:0)
试试这个 -
<?php
$data = array(
array(
'base_id' => 14,
'offset' => 1,
'name' => 'rental',
'value' => 200
),
array(
'base_id' => 14,
'offset' => 1,
'name' => 'tax',
'value' => 20
),
array(
'base_id' => 14,
'offset' => 1,
'name' => 'misc',
'value' => 10
),
array(
'base_id' => 13,
'offset' => 5,
'name' => 'rental',
'value' => 150
),
array(
'base_id' => 13,
'offset' => 5,
'name' => 'tax',
'value' => 15
),
array(
'base_id' => 13,
'offset' => 5,
'name' => 'misc',
'value' => 5
),
array(
'base_id' => 13,
'offset' => 7,
'name' => 'rental',
'value' => 170
)
);
foreach($data as $key=>$val){
$myArr[$val['base_id']][$val['offset']][] = $val;
}
echo '<pre>'; print_r($myArr);
?>
答案 4 :(得分:0)
试试这个
<?php
$data = array(
array(
'base_id' => 14,
'offset' => 1,
'name' => 'rental',
'value' => 200
),
array(
'base_id' => 14,
'offset' => 1,
'name' => 'tax',
'value' => 20
),
array(
'base_id' => 14,
'offset' => 1,
'name' => 'misc',
'value' => 10
),
array(
'base_id' => 13,
'offset' => 5,
'name' => 'rental',
'value' => 150
),
array(
'base_id' => 13,
'offset' => 5,
'name' => 'tax',
'value' => 15
),
array(
'base_id' => 13,
'offset' => 5,
'name' => 'misc',
'value' => 5
),
array(
'base_id' => 13,
'offset' => 7,
'name' => 'rental',
'value' => 170
)
);
foreach($data as $key=>$val){
$myArr[$val['base_id'].'_'.$val['offset']][] = $val;
}
echo '<pre>'; print_r($myArr);
?>