如何在php API中简化格式化数组列表数据以提供React Native部分列表

9njqaruj  于 2022-12-10  发布在  PHP
关注(0)|答案(1)|浏览(103)

日安!我正在使用php编写RestAPI。我的团队成员正在使用React-native来使用API,他要求我将响应格式化为以下格式

{
        title: 'Class of 2007',
        data: [
            { id: 1, lastsName: 'Igbashio', firstName: 'Kalifort', middleName: 'Kashimana', email: 'igbashiokalifortkashimana@gmail.com', image: 'https://img.freepik.com/premium-photo/young-student-caucasian-woman-isolated-white-background-pointing-side-present-product_1368-289762.jpg?w=740' },
            { id: 2, lastsName: 'Usman', firstName: 'Bello', middleName: '', email: 'usmanbello@gmail.com', image: 'https://img.freepik.com/free-photo/waist-up-portrait-handsome-serious-unshaven-male-keeps-hands-together-dressed-dark-blue-shirt-has-talk-with-interlocutor-stands-against-white-wall-self-confident-man-freelancer_273609-16320.jpg?size=626&ext=jpg&ga=GA1.2.1337949710.1669025188' },
            { id: 3, lastsName: 'Manasseh', firstName: 'Isa', middleName: '', email: 'manassehisa@gmail.com', image: 'https://img.freepik.com/free-photo/young-attractive-handsome-guy-feels-delighted-gladden-amazed_295783-535.jpg?size=626&ext=jpg&ga=GA1.2.1337949710.1669025188' }
        ]
    },
    {
        title: 'Class of 2008',
        data: [
            { id: 4, lastsName: 'Igbashio', firstName: 'Sansa', middleName: 'Sewuese', email: 'igbashiosansaswewuese@gmail.com', image: 'https://img.freepik.com/free-photo/front-view-female-student-white-shirt-black-jacket-wearing-backpack-holding-files-with-copybooks-blue-wall-college-university-lessons_140725-43393.jpg?size=626&ext=jpg&ga=GA1.2.1337949710.1669025188' },
            { id: 5, lastsName: 'Yusuf', firstName: "Sa'atu", middleName: '', email: 'yusufsaatu@gmail.com', image: 'https://img.freepik.com/free-photo/cheerful-muslim-woman_53876-14375.jpg?w=360&t=st=1669732335~exp=1669732935~hmac=9942e2842661b423d7686f2ba66d87b2b9485e95f438b415993b38e910203937' },
            { id: 6, lastsName: 'Rimamtse', firstName: 'Bleesing', middleName: 'Fxiafatirimam', email: 'rimamblessing@gmail.com', image: 'https://img.freepik.com/premium-photo/smiling-black-woman-striped-shirt-with-arms-crossed_33839-10129.jpg?size=626&ext=jpg&ga=GA1.2.1337949710.1669025188' }
        ]
    },

为了在react-native sectionList中使用。下面是我到目前为止是如何实现这个任务的

$list = Members::where(function ($sql) use ($input) {
            if (isset($input["search"])) :
                return $sql->where('regNo', 'LIKE', $input["search"])->orWhere('lastsName', 'LIKE', $input["search"])
                    ->orWhere('firstName', 'LIKE', $input["search"])->orWhere('email', 'LIKE', $input["search"])
                    ->orWhere('phone', 'LIKE', $input["search"])->orWhere('gender', 'LIKE', $input["search"]);
            endif;
        })->orderBy('graduateYear', 'DESC')->orderBy('lastsName', 'ASC')->skip($input["start"])->take($input["length"])->get();
die(json_encode(formatMembers($list)));

private function formatMembers($list)
    {
        $json = array();
        $i = 0;
        $lastYear = null;
        foreach ($list as $x) {
            if (count($json) == 0) :
                $json[$i]["title"] = $x->graduateYear;
                $json[$i]['data'][] = array(
                    "id" => $x->id, "lastsName" => $x->lastsName, "firstName" => $x->firstName,
                    "middleName" => $x->middleName, "email" => $x->email, "image" => null
                );
                $lastYear = $x->graduateYear;
            else:
                if ($lastYear == $x->graduateYear) :
                    $json[$i]['data'][] = array(
                        "id" => $x->id, "lastsName" => $x->lastsName, "firstName" => $x->firstName,
                        "middleName" => $x->middleName, "email" => $x->email, "image" => null
                    );
                    $lastYear = $x->graduateYear;
                else :
                    $i++;
                    $json[$i]["title"] = $x->graduateYear;
                    $json[$i]['data'][] = array(
                        "id" => $x->id, "lastsName" => $x->lastsName, "firstName" => $x->firstName,
                        "middleName" => $x->middleName, "email" => $x->email, "image" => null
                    );
                    $lastYear = $x->graduateYear;
                endif;
            endif;
        }
        return $json;
    }

贝娄是我实施的结果

[
        {
            "title": "2016",
            "data": [
                {
                    "id": 6,
                    "lastsName": "Igbashio ",
                    "firstName": "Sansa",
                    "middleName": "Seember",
                    "email": "sansaigbashio@Gmail.com",
                    "image": null
                },
                {
                    "id": 7,
                    "lastsName": "Usman",
                    "firstName": "Smith",
                    "middleName": "",
                    "email": "smithusman@Gmail.com",
                    "image": null
                }
            ]
        }]

我的问题是有没有更好的方法来实现这一点??在处理大量记录时考虑甚至是一个较短的方法。任何理想都将受到欢迎。提前感谢!

eufgjt7s

eufgjt7s1#

看一看下面的代码块:

$json[$i]["title"] = $x->graduateYear;
                $json[$i]['data'][] = array(
                    "id" => $x->id, "lastsName" => $x->lastsName, "firstName" => $x->firstName,
                    "middleName" => $x->middleName, "email" => $x->email, "image" => null
                );
                $lastYear = $x->graduateYear;

这两种情况之间的唯一区别似乎是,在某些情况下添加了一个title,而在另一些情况下不添加标题。因此,与其复制整个代码,不如这样做更有意义:

if (!isset($json[$i])) {
    $json[$i] = [
        'title' => ($lastYear = $x->graduateYear),
    ];
}
/*Your template here*/
if ($lastYear !== $x->graduateYear) {
    $i++;
}

按照目前的情况,您需要重复代码三次,因此如果需要对模板进行任何更改,则需要重复3次,这将增加3倍的必要劳动力。
进一步的改进是创建一个function,它将接收一些参数,并创建您要指定为数据的对象。这将确保您的模板在其他地方可重用,如果您必须在另一个API函数中使用相同的格式。

相关问题