【问题标题】:How to assign the name to the PHP subarrays如何将名称分配给 PHP 子数组
【发布时间】:2019-10-24 16:23:27
【问题描述】:

我想生成带有已定义子数组名称的 PHP 表(不确定我是否使用了正确的术语)。 通常我得到: 大批 但想通过以下方式替换它: 标准类对象

使用附加代码,我可以生成“标准”数组,但是当我想将 Array 替换为 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

【问题讨论】:

    标签: php matrix multidimensional-array


    【解决方案1】:

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

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

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

    Demo.

    【讨论】:

      猜你喜欢
      • 2019-01-10
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多