How to iterate over an array according to a given condition?

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
User avatar
Drummer
New php-forum User
New php-forum User
Posts: 11
Joined: Thu Dec 17, 2020 12:31 am

Wed Jul 14, 2021 10:03 pm

Good to everyone.

I need to iterate over the array according to a certain condition. I access the DB, and I get a response with an array of data:

Code: Select all

[
    {
        "id": 222,
        "equipment_id": 141490,
        "datetime": "2021-02-07 23:08:07",
        "distance": 5.48,
        "speed": 0.02
    },
    {
        "id": 223,
        "equipment_id": 141490,
        "datetime": "2021-02-07 23:10:08",
        "distance": 6.09,
        "speed": 0.02
    },
    {
        "id": 224,
        "equipment_id": 141490,
        "datetime": "2021-02-07 23:11:01",
        "distance": 6.36,
        "speed": 0.02
    },
    {
        "id": 418,
        "equipment_id": 141491,
        "datetime": "2021-02-07 22:05:38",
        "distance": 341.65,
        "speed": 0
    },
    {
        "id": 419,
        "equipment_id": 141491,
        "datetime": "2021-02-07 22:06:03",
        "distance": 341.65,
        "speed": 0
    },
    {
        "id": 1065,
        "equipment_id": 141491,
        "datetime": "2021-02-08 04:55:46",
        "distance": 172.12,
        "speed": 1.24
    },
    {
        "id": 1066,
        "equipment_id": 141491,
        "datetime": "2021-02-08 04:56:22",
        "distance": 184.51,
        "speed": 0.72
    },
]
How can I get such a result, how to group by the "equipment_id" field.

What should I read and what examples should I pay attention to?

Code: Select all

{
        "equipment_id": 141490,
        "speed": {
            "datetime": "2021-02-07 23:08:07",
            "value": 0.02,
            "distance": 5.48,
        },
    	{
            "datetime": "2021-02-07 23:10:08",
            "value": 0.02,
            "distance": 6.09,
        },
    {
            "datetime": "2021-02-07 23:11:01",
            "value": 0.02,
            "distance": 6.36,
        },

    },
    {
        "equipment_id": 141491,
        "speed": {
            "datetime": "2021-02-07 22:06:03",
            "value": 0,
            "distance": 341.65,
        },
    	{
            "datetime": "2021-02-08 04:55:46",
            "value": 0.02,
            "distance": 172.12,
        },
    {
            "datetime": "2021-02-08 04:56:22",
            "value": 0.72,
            "distance": 184.51,
        },

    },
    
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 137
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Wed Jul 14, 2021 11:27 pm

You can loop over your data and store each item in a keyed array, like so:

Code: Select all

$grouped = [];

foreach ($data as $item) {
    if (!isset($grouped[$item['equipment_id']])) {
        $grouped[$item['equipment_id']] = [];
    }

    $grouped[$item['equipment_id']][] = $item;
}
For the input you gave, this will give you:

Code: Select all

{
  "141490": [
    {
      "id": 222,
      "equipment_id": 141490,
      "datetime": "2021-02-07 23:08:07",
      "distance": 5.48,
      "speed": 0.02
    },
    {
      "id": 223,
      "equipment_id": 141490,
      "datetime": "2021-02-07 23:10:08",
      "distance": 6.09,
      "speed": 0.02
    },
    ...
  ],
  "141491": [
    {
      "id": 418,
      "equipment_id": 141491,
      "datetime": "2021-02-07 22:05:38",
      "distance": 341.65,
      "speed": 0
    },
    {
      "id": 419,
      "equipment_id": 141491,
      "datetime": "2021-02-07 22:06:03",
      "distance": 341.65,
      "speed": 0
    },
    ...
  ]
}
That doesn't look exactly like your output; I'm not sure what the "speed" key you've given is for. Does this do what you want?
User avatar
Drummer
New php-forum User
New php-forum User
Posts: 11
Joined: Thu Dec 17, 2020 12:31 am

Thu Jul 15, 2021 8:44 pm

simonbrahan Thank You!
Post Reply