使用PHP的数组中的动态数组。

时间:2022-05-10 13:25:52

I am not sure if question title is ok or not!

我不确定问题标题是否ok !

I am trying to dynamically input array inside an array.

我尝试在数组中动态输入数组。

This is how it should be:

它应该是这样的:

Example array:

示例数组:

$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 is what I have:

这就是我所拥有的:

//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.',
    ),
);

Now how can I pass this to $myArray dynamically?

现在,如何动态地将这个传递给myArray ?

My desired array format

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.
            )

        )

)

3 个解决方案

#1


2  

Hope I understood your question correctly:-

希望我能正确理解你的问题:-。

Try this:-

试试这个:

$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 will be:-

最终数组将:-

$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.',
    ),
  ),
);

#2


-1  

Use array_push() function. It will append your dynamic array into another array. For example

使用array_push()函数。它将把你的动态数组追加到另一个数组中。例如

$myArray=array("value1","value2");
array_push($myArray,"anotherValue1","anotherValue2");

Hope it will fine for you...

希望对你有好处…

#3


-1  

Thanks to those nice people, who tried to help me. I have solved it based on idea from @Ashutosh.

感谢那些好心的人,他们想帮助我。我是基于@Ashutosh的想法解决的。

This is the solution:

这是解决方案:

foreach ( $works as $work ) {
    $myArray ['works'][] = $work;
}

#1


2  

Hope I understood your question correctly:-

希望我能正确理解你的问题:-。

Try this:-

试试这个:

$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 will be:-

最终数组将:-

$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.',
    ),
  ),
);

#2


-1  

Use array_push() function. It will append your dynamic array into another array. For example

使用array_push()函数。它将把你的动态数组追加到另一个数组中。例如

$myArray=array("value1","value2");
array_push($myArray,"anotherValue1","anotherValue2");

Hope it will fine for you...

希望对你有好处…

#3


-1  

Thanks to those nice people, who tried to help me. I have solved it based on idea from @Ashutosh.

感谢那些好心的人,他们想帮助我。我是基于@Ashutosh的想法解决的。

This is the solution:

这是解决方案:

foreach ( $works as $work ) {
    $myArray ['works'][] = $work;
}