我甚至不确定我们是否打电话给变换,但是,让我们说我们有一个像这样的数组:
A)
Array
(
[id] => 532b436477936
[name] => Record #1
[created] => 20-03-2014
[regions] => Array
(
[0] => Array
(
[id] => 532b3fcbf2353
[name] => Region #2
[created_at] => 1395343322
)
[1] => Array
(
[id] => 532b3fcbf2354
[name] => Region #3
[created_at] => 1395343323
)
)
)
我们希望改变它:
B)
Array
(
[0] => Array
(
[id] => 532b436477936
[name] => Record #1
[created] => 20-03-2014
[regions.id] => 532b3fcbf2353
[regions.name] => Region #2
[regions.created_at] => 1395343322
)
[1] => Array
(
[id] => 532b436477936
[name] => Record #1
[created] => 20-03-2014
[regions.id] => 532b3fcbf2354
[regions.name] => Region #3
[regions.created_at] => 1395343323
)
)
我可以帮忙获得一些代码吗?
$actual = array(
'id' => '532b436477936',
'name' => 'Record #1',
'created' => '20-03-2014',
'regions' => array(
array('id' => '532b3fcbf2353', 'name' => 'Region #2'),
array('id' => '532b3fcbf2354', 'name' => 'Region #3')
)
);
var_dump($actual);
$desired = array(
array(
'id' => '532b436477936',
'name' => 'Record #1',
'created' => '20-03-2014',
'regions.id' => '532b3fcbf2353',
'regions.name' => 'Region #2',
),
array(
'id' => '532b436477936',
'name' => 'Record #1',
'created' => '20-03-2014',
'regions.id' => '532b3fcbf2354',
'regions.name' => 'Region #3',
)
);
var_dump($desired);
为了将A变换为B,我可以看一些灯吗?
答案 0 :(得分:0)
$desired = array();
foreach($actual['regions'] as $region)
{
$desired[] = array(
'id' => '532b436477936',
'name' => 'Record #1',
'created' => '20-03-2014',
'region.id' => $region['id'],
'region.name' => $region['name']);
}
答案 1 :(得分:0)
$actual = array(
'id' => '532b436477936',
'name' => 'Record #1',
'created' => '20-03-2014',
'regions' => array(
array('id' => '532b3fcbf2353', 'name' => 'Region #2'),
array('id' => '532b3fcbf2354', 'name' => 'Region #3')
)
);
$desired = array();
foreach($actual as $actual_dummy => $a)
{
foreach($a['regions'] as $Region_dummy => $r)
{
$desired[]=array("id" => $a['id'],
"name" => $a['name'],
"created" => $a['created'],
"regions.id" => $r['id'],
"regions.name" => $r['name']);
}
}
答案 2 :(得分:0)
试试这个:
$desired = array();
$basic = $actual;
unset($basic['regions']);
foreach($actual['regions'] as $key=>$val){
foreach($val as $key2=>$val2) {
$basic["$key.$key2"] = $val2;
}
$desired[] = $basic;
}