替换它:
(
[0] => 'Header'
[1] => 'Body'
)
为此:
['Header'] => Array
(
[0] => Hello!
)
['Body'] => Array
(
[0] => This is the body
)
我错过了逻辑,我认为这种情况正在发生,因为我不知道语法。
按照原始数组(预览):
Array
(
[Title] => 'Hello!'
[Layout] => 'Shared/_Master'
[Section] => Array
(
[0] => 'Header'
[1] => 'Body'
)
)
代码:
<?php
$array = [
'Title' => 'Hello',
'Layout' => 'Shared/_Master',
'Section' => ['Header', 'Body']
];
$mineredSection = ['Header' => ['Hello!'], 'Body' => ['This is the body.']];
我已经尝试过了:
foreach ($array['Section'] as $index => $section) {
$t[$section] = [array_values(array_filter($mineredSection))[$index]];
}
$a = array_replace($array['Section'], $t);
print_r($a);
结果是:
Array
(
[0] => 'Header'
[1] => 'Body'
['Header'] => Array
(
[0] => Hello!
)
['Body'] => Array
(
[0] => This is the body
)
)
有人可以给我一个想法吗?
答案 0 :(得分:1)
您正在寻找array_combine
。你将它传递给两个数组,你想要的是键和你想要的值。
$array['Section'] = array_combine($array['Section'], $this->contents);
答案 1 :(得分:0)
或者这个,或者我不理解你在问什么
$origarr = array('header', 'body');
foreach($origarr as $value){
if ($value == 'header'){
$header['header'] = array('hello');
}
if ($value == 'body'){
$body['body'] = array('This is the body');
}
}
var_dump($header);
var_dump($body);