使用嵌套标准类对象按特定值对多维数组进行分组

发布于 2024-10-20 05:44:34 字数 1342 浏览 2 评论 0原文

在上一篇文章中,有一个关于分组多维数组的非常有用的答案。我的问题与我的初始数组有一个嵌套标准类对象的问题类似。我的数组看起来像这样:

Array
(
[0] => stdClass Object
    (
        [InstallationAddress2] => LAKEWOOD, CA  90000
        [LineitemmasterDescription] => Apples
        [Workorder_id] => W008052094
    )

[1] => stdClass Object
    (
        [InstallationAddress2] => Santa Rosa, CA  90230
        [LineitemmasterDescription] => Berries
        [Workorder_id] => W008022342
    )

[2] => stdClass Object
    (
        [InstallationAddress2] => LAKEWOOD, CA  90000
        [LineitemmasterDescription] => Apples
        [Workorder_id] => W008052094
    )
)

就像上一篇文章一样,我希望得到类似的东西

Array
(
   Array (
    [0] => Array
        (
        [InstallationAddress2] => LAKEWOOD, CA  90000
        [LineitemmasterDescription] => Apples
        [Workorder_id] => W008052094
       )

    [1] => Array
      (
        [InstallationAddress2] => LAKEWOOD, CA  90000
        [LineitemmasterDescription] => Apples
        [Workorder_id] => W008052094
      )
   )
   Array (
     [2] => Array
    (
        [InstallationAddress2] => Santa Rosa, CA  90230
        [LineitemmasterDescription] => Berries
        [Workorder_id] => W008022342
    )
   )
)

我绊倒的是标准类对象,它拒绝转换为数组。

In a previous post there was a very helpful answer on grouping multidimensional arrays. My issue is similar with the twist that my initial array has a nested standard class object. My array looks like this:

Array
(
[0] => stdClass Object
    (
        [InstallationAddress2] => LAKEWOOD, CA  90000
        [LineitemmasterDescription] => Apples
        [Workorder_id] => W008052094
    )

[1] => stdClass Object
    (
        [InstallationAddress2] => Santa Rosa, CA  90230
        [LineitemmasterDescription] => Berries
        [Workorder_id] => W008022342
    )

[2] => stdClass Object
    (
        [InstallationAddress2] => LAKEWOOD, CA  90000
        [LineitemmasterDescription] => Apples
        [Workorder_id] => W008052094
    )
)

Like the previous post I'm hoping to get something like

Array
(
   Array (
    [0] => Array
        (
        [InstallationAddress2] => LAKEWOOD, CA  90000
        [LineitemmasterDescription] => Apples
        [Workorder_id] => W008052094
       )

    [1] => Array
      (
        [InstallationAddress2] => LAKEWOOD, CA  90000
        [LineitemmasterDescription] => Apples
        [Workorder_id] => W008052094
      )
   )
   Array (
     [2] => Array
    (
        [InstallationAddress2] => Santa Rosa, CA  90230
        [LineitemmasterDescription] => Berries
        [Workorder_id] => W008022342
    )
   )
)

The thing I'm stumbling over is the standard class object which refuses to be cast into an array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赤濁 2024-10-27 05:44:34

您可以轻松地将 stdClass 转换为数组...

$a = array (
    'index_0' => 'value_0',
    'index_1' => 'value_1'
);

$obj = (object) $a;
// Now $obj is an instance of stdClass
$arr = (array) $obj;
// Now it's an array again :)

更有趣的问题:您从哪里获得这个数组?

You can easily cast an stdClass to an array ...

$a = array (
    'index_0' => 'value_0',
    'index_1' => 'value_1'
);

$obj = (object) $a;
// Now $obj is an instance of stdClass
$arr = (array) $obj;
// Now it's an array again :)

The more-interesting question: Where did you get this array?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文