PHP настроить массив для конкретного формата - PullRequest
0 голосов
/ 25 февраля 2019

У меня есть массив, мне нужно организовать этот массив как мое требование.Могу ли я изменить их в соответствии с floor_id и timestamp.

мой формат массива:

Array (
    [0] => stdClass Object
        (
            [id] => 100638
            [gate_name] => Gate 148
            [floor_id] => 4
            [timestamp] => 2019-02-25 00:00:05
        )

    [1] => stdClass Object
        (
            [id] => 100639
            [gate_name] => Gate 148
            [floor_id] => 5
            [timestamp] => 2019-02-25 00:00:10
        )

    [2] => stdClass Object
        (
            [id] => 100640
            [gate_name] => Gate 148
            [floor_id] => 6
            [timestamp] => 2019-02-25 00:00:20
        )

    [3] => stdClass Object
        (
            [id] => 100644
            [gate_name] => Gate 148
            [floor_id] => 6
            [timestamp] => 2019-02-25 00:00:30
        )

    [4] => stdClass Object
        (
            [id] => 100643
            [gate_name] => Gate 148
            [floor_id] => 4
            [timestamp] => 2019-02-25 00:00:40
        )

OutPut Needed:

Array
(
    [0] => Array
        (
            [floors] => 4
            [data] => Array
                (
                    [1] => Array
                        (
                            [id] => 100638
                            [gate_name] => Gate 148
                            [floor_id] => 4
                            [timestamp] => 2019-02-25 00:00:05
                        )
                )
        )
    [1] => Array
        (

            [floors] => 5
            [data] => Array
                (
                    [2] => Array
                        (
                            [id] => 100639
                            [gate_name] => Gate 148
                            [floor_id] => 5
                            [timestamp] => 2019-02-25 00:00:10
                        )
                )
        )
    [2] => Array
        (
            [floors] => 6
            [data] => Array
                (
                    [3] => Array
                        (
                            [id] => 100640
                            [gate_name] => Gate 148
                            [floor_id] => 6
                            [timestamp] => 2019-02-25 00:00:20
                        )

                    [4] => Array
                        (
                            [id] => 100644
                            [gate_name] => Gate 148
                            [floor_id] => 6
                            [timestamp] => 2019-02-25 00:00:30
                        )
                )
        )
    [3] => Array
        (
            [floors] => 4
            [data] => Array
                (
                    [5] => Array
                        (
                            [id] => 100643
                            [gate_name] => Gate 148
                            [floor_id] => 4
                            [timestamp] => 2019-02-25 00:00:40
                        )


                )
        )
)

Логика этих массивов- если то же самое floor_id встречается в серии продолжения, тогда выполните массив, а если нет, то разбейте эти группировки с помощью отметка времени .

Ответы [ 3 ]

0 голосов
/ 25 февраля 2019

Может быть, это то, что вы ищете.

В следующий раз, пожалуйста, предоставьте сериализованный массив JSON, чтобы другим людям не приходилось переписывать ваши массивы.

<?php

$arrayData = array(
    (object)(array(
        "id" => 100638,
        "gate_name" => "Gate 148",
        "floor_id" => 4
    )),
    (object)(array(
        "id" => 100639,
        "gate_name" => " Gate 148",
        "floor_id" => 5
    )),
    (object)(array(
        "id" => 100640,
        "gate_name" => "Gate 148",
        "floor_id" => 6
    )),
    (object)(array(
        "id" => 100644,
        "gate_name" => "Gate 148",
        "floor_id" => 6
    )),
    (object)(array(
        "id" => 100643,
        "gate_name" => "Gate 148",
        "floor_id" => 4
    ))
);

$result = array();

foreach ($arrayData as $key => $data) {

    $result[$data->floor_id]["floors"] = $data->floor_id;
    $result[$data->floor_id]["data"][] = (array)$data;

    // Remove the old data to minimize the memory allocation.
    unset($arrayData[$key]);
}

// Clean up.
unset($arrayData, $key, $data);

// Clean up the array index.
$result = array_values($result);

print_r($result);

0 голосов
/ 25 февраля 2019

Основной код:

$arrObject = array(
            (object) (array(
        "id" => 100638,
        "gate_name" => "Gate 148",
        "floor_id" => 4
            )),
            (object) (array(
        "id" => 100639,
        "gate_name" => " Gate 148",
        "floor_id" => 5
            )),
            (object) (array(
        "id" => 100640,
        "gate_name" => "Gate 148",
        "floor_id" => 6
            )),
            (object) (array(
        "id" => 100644,
        "gate_name" => "Gate 148",
        "floor_id" => 6
            )),
            (object) (array(
        "id" => 100643,
        "gate_name" => "Gate 148",
        "floor_id" => 4
            ))
        );
        $onlyFloorId = array();
        foreach ($arrObject as $c) {
            $onlyFloorId[] = $c->floor_id;
        }
        $onlyFloorId = array_values(array_unique($onlyFloorId)); //convert unique and indexed
        $endResult = array();
        foreach ($arrObject as $c) {
            foreach ($onlyFloorId as $key => $value) {
                if ($c->floor_id == $value) {
                    $endResult[$key]['floors'] = $c->floor_id;
                    $endResult[$key]['data'][] = (array)$c;
                }
            }
        }
        echo'<pre>';print_r($endResult);die;

Выход:

Array
(
    [0] => Array
        (
            [floors] => 4
            [data] => Array
                (
                    [0] => Array
                        (
                            [id] => 100638
                            [gate_name] => Gate 148
                            [floor_id] => 4
                        )

                    [1] => Array
                        (
                            [id] => 100643
                            [gate_name] => Gate 148
                            [floor_id] => 4
                        )

                )

        )

    [1] => Array
        (
            [floors] => 5
            [data] => Array
                (
                    [0] => Array
                        (
                            [id] => 100639
                            [gate_name] =>  Gate 148
                            [floor_id] => 5
                        )

                )

        )

    [2] => Array
        (
            [floors] => 6
            [data] => Array
                (
                    [0] => Array
                        (
                            [id] => 100640
                            [gate_name] => Gate 148
                            [floor_id] => 6
                        )

                    [1] => Array
                        (
                            [id] => 100644
                            [gate_name] => Gate 148
                            [floor_id] => 6
                        )

                )

        )

)
0 голосов
/ 25 февраля 2019

Вы можете попробовать приведенный ниже код

$arayData = Array (
    array(
        'id' => 100638,
        'gate_name' => 'Gate 148',
        'floor_id' => 4
    ),
    array(
        'id' => 100639,
        'gate_name' => 'Gate 148',
        'floor_id' => 5
    ),
    array(
        'id' => 100640,
        'gate_name' => 'Gate 148',
        'floor_id' => 6
    ),
    array(
        'id' => 100644,
        'gate_name' => 'Gate 148',
        'floor_id' => 6
    ),
    array(
        'id' => 100643,
        'gate_name' => 'Gate 148',
        'floor_id' => 4
    ));
  $uniqueKeys = array_unique(array_column($arayData, 'floor_id'));
  $floorDetails = array_column($arayData, 'floor_id');
  $dataArrays = [];
  foreach($uniqueKeys as $uniqueKey) {
    $datas['floor_id'] = $uniqueKey;
    foreach (array_keys($floorDetails, $uniqueKey) as $key) {
    $datas['data'][] = $arayData[$key];
}
array_push($dataArrays, $datas);
$datas = [];

}

 echo json_encode($dataArrays);

http://sandbox.onlinephpfunctions.com/

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...