开发者

Grouping Multidimensional Arrays by Particular Value with nested Standard Class Objects

开发者 https://www.devze.com 2023-02-14 23:04 出处:网络
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

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] 开发者_StackOverflow=> 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
(
    [1] => Array
        [0] => Array
            (
            [InstallationAddress2] => LAKEWOOD, CA  90000
            [LineitemmasterDescription] => Apples
            [Workorder_id] => W008052094
           )

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

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


do (array) type cast of each of the std objects by looping through your array.

for($i=0;$i< count($myArray);  $i++)
    $myArray[$i] = (array)$myArray[$i];

Then you can apply your grouping logic. See typecasting example : http://codepad.org/uVuBzBm8

0

精彩评论

暂无评论...
验证码 换一张
取 消