我无法更改多维数组的键并分解其值
I can't change keys of the multi-dimensional array and explode its values
我有一个数组:
Array ( [0] => Array ( [0] => Array ( [creator] => a, b ) [1] => Array ( [creator] => c, d ) ) [1] => Array ( [0] => Array ( [creator] => e, f ) ) [2] => Array ( [0] => Array ( [creator] => g, h ) [1] => Array ( [creator] => i, j ) [2] => Array ( [creator] => k, l ) [3] => Array ( [creator] => m, n ) [4] => Array ( [creator] => o, p ) ) )
我想将其所有键的名称从 [creator]
更改为 [author]
,并且必须将其项目分解为 [first]
、[last]
所以期望的输出应该是:
Array ( [0] => Array ( [0] => Array ( [author] => [first] => a [last] => b ) [1] => Array ( [author] => [first] => c [last] => d ) ) [1] => Array ( [0] => Array ( [author] => [first] => e [last] => f ) ) [2] => Array ( [0] => Array ( [author] => [first] => g [last] => h ) [1] => Array ( [author] => [first] => i [last] => j ) [2] => Array ( [author] => [first] => k [last] => l ) [3] => Array ( [author] => [first] => m [last] => n ) [4] => Array ( [author] => [first] => o [last] => p ) ) )
我试过:
for($loop=0; $loop<sizeof($output); $loop++) {
$authors[] = $output[$loop]['creators'];
}
$a123 = explode(', ', $authors);
foreach ($authors as $items) {
foreach ($items as $k => $v) {
$myarray[$k]['author'] = $v;
}
}
谢谢
一种方法是遍历数组的每个项目,分解 creator
值,使用该数据创建新的 author
值,然后使用 unset()
删除旧 creator
值。
foreach($array AS $a => $b)
{
foreach($b AS $c => $d)
{
$name = explode(', ', $d['creator']);
$array[$a][$c]['author'] = array(
'first' => $name[0],
'last' => $name[1]
);
unset($array[$a][$c]['creator']);
}
}
代码未经测试,但应该能表达想法。
我有一个数组:
Array ( [0] => Array ( [0] => Array ( [creator] => a, b ) [1] => Array ( [creator] => c, d ) ) [1] => Array ( [0] => Array ( [creator] => e, f ) ) [2] => Array ( [0] => Array ( [creator] => g, h ) [1] => Array ( [creator] => i, j ) [2] => Array ( [creator] => k, l ) [3] => Array ( [creator] => m, n ) [4] => Array ( [creator] => o, p ) ) )
我想将其所有键的名称从 [creator]
更改为 [author]
,并且必须将其项目分解为 [first]
、[last]
所以期望的输出应该是:
Array ( [0] => Array ( [0] => Array ( [author] => [first] => a [last] => b ) [1] => Array ( [author] => [first] => c [last] => d ) ) [1] => Array ( [0] => Array ( [author] => [first] => e [last] => f ) ) [2] => Array ( [0] => Array ( [author] => [first] => g [last] => h ) [1] => Array ( [author] => [first] => i [last] => j ) [2] => Array ( [author] => [first] => k [last] => l ) [3] => Array ( [author] => [first] => m [last] => n ) [4] => Array ( [author] => [first] => o [last] => p ) ) )
我试过:
for($loop=0; $loop<sizeof($output); $loop++) {
$authors[] = $output[$loop]['creators'];
}
$a123 = explode(', ', $authors);
foreach ($authors as $items) {
foreach ($items as $k => $v) {
$myarray[$k]['author'] = $v;
}
}
谢谢
一种方法是遍历数组的每个项目,分解 creator
值,使用该数据创建新的 author
值,然后使用 unset()
删除旧 creator
值。
foreach($array AS $a => $b)
{
foreach($b AS $c => $d)
{
$name = explode(', ', $d['creator']);
$array[$a][$c]['author'] = array(
'first' => $name[0],
'last' => $name[1]
);
unset($array[$a][$c]['creator']);
}
}
代码未经测试,但应该能表达想法。