我不确定问题标题是否合适!
我正在尝试在数组中动态输入数组。
应该是这样的:
示例数组:
$myArray = array (
'id' => 123,
'name' => 'Sufi',
'works' => array (
'show_work' => TRUE,
'number' => -1,
'order' => 'DESC',
/*i want this part to be dynamic */
array (
'title' => 'developer',
'experience' => '5 years',
'company' => 'ABC Inc.',
),
array (
'title' => 'CEO',
'experience' => '1 year',
'Company' => 'XYZ LLC.',
),
/*i want this part to be dynamic */
),
);
这就是我所拥有的:
//this array contains all works
$works = array(
array (
'title' => 'developer',
'experience' => '5 years',
'company' => 'ABC Inc.',
),
array (
'title' => 'CEO',
'experience' => '1 year',
'Company' => 'XYZ LLC.',
),
);
现在我该如何动态地将其传递给$myArray
?
Array
(
[id] => 123
[name] => sufi
[works] => Array
(
[show_work] => TRUE
[number] => -1
[order] => DESC
[0] => Array
(
[title] => developer
[experience] => 5 years
[company] => ABC Inc.
)
[1] => Array
(
[title] => CEO
[experience] => 1 year
[company] => XYZ LLC.
)
)
)
答案 0 :(得分:2)
希望我能正确理解你的问题: -
试试这个: -
$myArray = array (
'id' => 123,
'name' => 'Sufi',
'works' => array (
'show_work' => true,
'number' => -1,
'order' => 'DESC'
)
);
$works = array(
array (
'title' => 'developer',
'experience' => '5 years',
'company' => 'ABC Inc.',
),
array (
'title' => 'CEO',
'experience' => '1 year',
'Company' => 'XYZ LLC.',
),
);
foreach ( $works as $work ) {
$myArray ['works'][] = $work;
}
Final Array将是: -
$myArray = array (
'id' => 123,
'name' => 'Sufi',
'works' => array (
'show_work' => TRUE,
'number' => -1
'order' => 'DESC',
[0] => array (
'title' => 'developer',
'experience' => '5 years',
'company' => 'ABC Inc.',
),
[1] => array (
'title' => 'CEO',
'experience' => '1 year',
'Company' => 'XYZ LLC.',
),
),
);
答案 1 :(得分:-1)
感谢那些善于帮助我的人。我已根据@Ashutosh的想法解决了这个问题。
这是解决方案:
foreach ( $works as $work ) {
$myArray ['works'][] = $work;
}