如何将名称分配给PHP子数组

时间:2019-06-10 09:13:00

标签: php matrix multidimensional-array

我想用定义的子数组名称生成PHP表(不确定我是否使用正确的术语)。 通常我会得到: 数组 但想将其替换为: stdClass对象

使用附加的代码,我可以生成“标准”数组,但是当我想将数组替换为stdClass对象时 我得到结果:

>>> so with following code:

$testarray = array(
'stdClass Object'=>
      array('title' => 'Hotele - Miejsce',
            'display' => 'hot_miejsce',
            'align' => 'l',
            'width' => 450,
            'order' => 10,
            'format' =>'' ,),
      array('title' => 'Aktualizacja',
            'display' => 'hot_updated',
            'align' => 'c',
            'width' => 200,
            'order' => 20,
            'format' =>'' ,),
);

>>> I get following

[0] : Array
(
    [stdClass Object] => Array
        (
            [title] => Hotele - Miejsce
            [display] => hot_miejsce
            [align] => l
            [width] => 450
            [order] => 10
            [format] => 
        )

    [0] => Array
        (
            [title] => Aktualizacja
            [display] => hot_updated
            [align] => c
            [width] => 200
            [order] => 20
            [format] => 
        )
) 

>>>>> but wanted to get

[0] : Array
(
    [0] => stdClass Object
        (
            [title] => Hotele - Miejsce
            [display] => hot_miejsce
            [align] => l
            [width] => 450
            [order] => 10
            [format] => 
        )

    [1] => stdClass Object
        (
            [title] => Aktualizacja
            [display] => hot_updated
            [align] => c
            [width] => 200
            [order] => 20
            [format] => 
        )

)


>>> so how to modify my original code to get expected result

1 个答案:

答案 0 :(得分:0)

这一行代码将帮助您处理输入数组,

$temp = array_map(function($item){ return (object)$item;}, $testarray);

我使用类型转换来反对每个项目。

Demo