Home > Mobile >  how to create new array form a same array with unique value in php
how to create new array form a same array with unique value in php

Time:10-15

this is my output in the form of an array

[0] => stdClass Object
    (
        [status] => P
        [date] => 10/02/2022
        [firstname] => testing 10
        [subject_id] => 5
    )

[1] => stdClass Object
    (
        [status] => A
        [date] => 10/02/2022
        [firstname] => arsalan 12
        [subject_id] => 5
    )

[2] => stdClass Object
    (
        [status] => L
        [date] => 10/02/2022
        [firstname] => khan 4
        [subject_id] => 5
    )

[3] => stdClass Object
    (
        [status] => P
        [date] => 10/03/2022
        [firstname] => testing 10
        [subject_id] => 5
    )

[4] => stdClass Object
    (
        [status] => L
        [date] => 10/03/2022
        [firstname] => arsalan 12
        [subject_id] => 5
    )

[5] => stdClass Object
    (
        [status] => A
        [date] => 10/03/2022
        [firstname] => khan 4
        [subject_id] => 5
    )

[6] => stdClass Object
    (
        [status] => P
        [date] => 10/04/2022
        [firstname] => testing 10
        [subject_id] => 5
    )

[7] => stdClass Object
    (
        [status] => P
        [date] => 10/04/2022
        [firstname] => arsalan 12
        [subject_id] => 5
    )

[8] => stdClass Object
    (
        [status] => P
        [date] => 10/04/2022
        [firstname] => khan 4
        [subject_id] => 5
    )

[9] => stdClass Object
    (
        [status] => P
        [date] => 10/05/2022
        [firstname] => testing 10
        [subject_id] => 5
    )

[10] => stdClass Object
    (
        [status] => A
        [date] => 10/05/2022
        [firstname] => arsalan 12
        [subject_id] => 5
    )

this is the code on the controller

$attendance = DB::table('attendances')
        ->join('users', 'attendances.user_id', '=', 'users.id')
        ->havingBetween('attendances.date', array($dateFrom, $dateTo))
        ->having('attendances.subject_id','=',$ideas[0])
        ->orderBy('attendances.date','asc')
        ->get(['attendances.status','attendances.date','users.firstname','attendances.subject_id'])->toArray();

I want this type of array

[0] => stdClass Object
    (
        [status] =>
        [
            P,
             p,
            p,
             p
        ]
        [date] => [
            10/02/2022,
            10/03/2022,
            10/04/2022,
            10/05/2022,
            ]
        [firstname] => testing 10
        [subject_id] => 5
    )

[1] => stdClass Object
    (
        [status] =>
        [
            A,
             L,
            p,
             A
        ]
        [date] => [
            10/02/2022,
            10/03/2022,
            10/04/2022,
            10/05/2022,
            ]
        [firstname] => arsalan 12
        [subject_id] => 5
    )

[2] => stdClass Object
    (
        [status] =>
        [
            L,
             a,
            p,
             A
        ]
        [date] => [
            10/02/2022,
            10/03/2022,
            10/04/2022,
            10/05/2022,
            ]
        [firstname] => khan 4
        [subject_id] => 5
    )

to show students' attendance on the table form, just like attendance registered. attendance not on daily bases. please help me I have no idea what to do about this. this is my second question on the same problem.

CodePudding user response:

You want to group by firstname. We'll just loop over the items adding to the array of an item with the key of firstname as we go.

$arr = array(
    "0" => array(
        "status" => 'P',
        "date" => "10/02/2022",
        "firstname" => "testing 10",
        "subject_id" => 5,
    ),

    "1" => array(
        "status" => 'A',
        "date" => "10/02/2022",
        "firstname" => "arsalan 12",
        "subject_id" => 5,
    ),

    "2" => array(
        "status" => 'L',
        "date" => "10/02/2022",
        "firstname" => "khan 4",
        "subject_id" => 5,
    ),

    "3" => array(
        "status" => 'P',
        "date" => "10/03/2022",
        "firstname" => "testing 10",
        "subject_id" => 5,
    ),

    "4" => array(
        "status" => 'L',
        "date" => "10/03/2022",
        "firstname" => "arsalan 12",
        "subject_id" => 5,
    ),

    "5" => array(
        "status" => 'A',
        "date" => "10/03/2022",
        "firstname" => "khan 4",
        "subject_id" => 5,
    ),

    "6" => array(
        "status" => 'P',
        "date" => "10/04/2022",
        "firstname" => "testing 10",
        "subject_id" => 5,
    ),

    "7" => array(
        "status" => 'P',
        "date" => "10/04/2022",
        "firstname" => "arsalan 12",
        "subject_id" => 5,
    ),

    "8" => array(
        "status" => 'P',
        "date" => "10/04/2022",
        "firstname" => "khan 4",
        "subject_id" => 5,
    ),

    "9" => array(
        "status" => 'P',
        "date" => "10/05/2022",
        "firstname" => "testing 10",
        "subject_id" => 5,
    ),

    "10" => array(
        "status" => 'A',
        "date" => "10/05/2022",
        "firstname" => "arsalan 12",
        "subject_id" => 5,
    ),
);


$result = array_values(array_reduce($arr, function ($agg, $item) {
    if (!isset($agg[$item['firstname']])) {
        $agg[$item['firstname']] = [
            "status" => [],
            "date" => [],
            "firstname" => $item['firstname'],
            "subject_id" => $item['subject_id'],
        ];
    }
    $agg[$item['firstname']]['status'][] = $item['status'];
    $agg[$item['firstname']]['date'][] = $item['date'];
    return $agg;
}, []));

print_r($result);

Output:

Array
(
    [0] => Array
        (
            [status] => Array
                (
                    [0] => P
                    [1] => P
                    [2] => P
                    [3] => P
                )

            [date] => Array
                (
                    [0] => 10/02/2022
                    [1] => 10/03/2022
                    [2] => 10/04/2022
                    [3] => 10/05/2022
                )

            [firstname] => testing 10
            [subject_id] => 5
        )

    [1] => Array
        (
            [status] => Array
                (
                    [0] => A
                    [1] => L
                    [2] => P
                    [3] => A
                )

            [date] => Array
                (
                    [0] => 10/02/2022
                    [1] => 10/03/2022
                    [2] => 10/04/2022
                    [3] => 10/05/2022
                )

            [firstname] => arsalan 12
            [subject_id] => 5
        )

    [2] => Array
        (
            [status] => Array
                (
                    [0] => L
                    [1] => A
                    [2] => P
                )

            [date] => Array
                (
                    [0] => 10/02/2022
                    [1] => 10/03/2022
                    [2] => 10/04/2022
                )

            [firstname] => khan 4
            [subject_id] => 5
        )

)

CodePudding user response:

with small changes, it works for me such as

 $result = array_values(array_reduce($attendance, function ($agg, $item) {
        if (!isset($agg[$item->firstname])) {
            $agg[$item->firstname] = [
                "status" => [],
                "date" => [],
                "firstname" => $item->firstname,
                "subject_id" => $item->subject_id,
            ];
        }
        $agg[$item->firstname]['status'][] = $item->status;
        $agg[$item->firstname]['date'][] = $item->date;
        return $agg;
    }, []));

    print_r($result);

the output is

 [0] => Array
    (
        [status] => Array
            (
                [0] => P
                [1] => P
                [2] => P
                [3] => P
                [4] => P
                [5] => L
                [6] => L
                [7] => L
            )

        [date] => Array
            (
                [0] => 10/02/2022
                [1] => 10/03/2022
                [2] => 10/04/2022
                [3] => 10/05/2022
                [4] => 10/07/2022
                [5] => 10/10/2022
                [6] => 10/12/2022
                [7] => 10/13/2022
            )

        [firstname] => testing 10
        [subject_id] => 5
    )

[1] => Array
    (
        [status] => Array
            (
                [0] => A
                [1] => L
                [2] => P
                [3] => A
                [4] => P
                [5] => L
                [6] => L
                [7] => L
            )

        [date] => Array
            (
                [0] => 10/02/2022
                [1] => 10/03/2022
                [2] => 10/04/2022
                [3] => 10/05/2022
                [4] => 10/07/2022
                [5] => 10/10/2022
                [6] => 10/12/2022
                [7] => 10/13/2022
            )

        [firstname] => arsalan 12
        [subject_id] => 5
    )

[2] => Array
    (
        [status] => Array
            (
                [0] => L
                [1] => A
                [2] => P
                [3] => P
                [4] => P
                [5] => L
                [6] => L
                [7] => L
            )

        [date] => Array
            (
                [0] => 10/02/2022
                [1] => 10/03/2022
                [2] => 10/04/2022
                [3] => 10/05/2022
                [4] => 10/07/2022
                [5] => 10/10/2022
                [6] => 10/12/2022
                [7] => 10/13/2022
            )

        [firstname] => khan 4
        [subject_id] => 5
    )
  • Related