“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

Throw out problem

Array subscript out of bounds is really a pain in the development process. Is there any other elegant way to solve this problem other than during the development process to determine whether to set, whether to empty?

Answer the questions

There has to be

Take a chestnut

For example, I have a tool method like this:

$batchUserCover[$userID][‘pickedFootprint’] and $batchFootprintList[$userID][‘list’]

Public static function batchImageFootprint($userIds, $batchUserCover, $batchUserCover) public static function batchImageFootprint($userIds, $batchUserCover, $batchUserCover) $batchFootprintList) { $ret = []; foreach ($userIds as $userid) { $ret[$userid] = array_slice(array_merge($batchUserCover[$userid]['pickedFootprint'], array_slice($batchFootprintList[$userid]['list'], 0, 3)), 0, 3); } return $ret; }Copy the code

Their thinking

  1. Determine whether to set a value outside the method

  2. $batchUserCover[$userID][‘pickedFootprint’] is already set.

I think idea 2 is better

The problem solving practice

Practice 1:

Before passing in the data, set the incoming value. Ensure that the key for the incoming value exists. The value can be null or an empty array. The core code is as follows:

    public static function batchFormatCoverAndPickedFootprint($userListInfo)
    {
        foreach ($userListInfo as &$element) {
            $retData[$element['userid']] = [
                'pickedFootprint' => [],
                'coverFootprint' => [],
            ];
        }
        
        .
        .
        .
        
        return $retData;
    }
Copy the code

The key of the array passed in must match [$userID][‘pickedFootprint’]. There is no out-of-bounds array subscript

    $batchUserCover = batchFormatCoverAndPickedFootprint(xxx);
    self::batchImageFootprint($userIds, $batchUserCover);
Copy the code

Practice 2:

This is the same underlying idea as Practice 1, except that practice 1 defines the initial values that conform to the specification first in the function

Practice 2 deals with the business logic first, defining conforming initial values before return

(The following code snippet is commented, focusing on the latter part; Joint query that part of the code quality is also good, did not omit, see if you can throw diversion jade.

public static function batchFootprintList($userIds, $pageCount = 21, $batchPickedFootprints = [], $select = 'userid,id,mid,image,text,ST_Astext(picgeom) as "picgeom",poi,poiid,city,province,country,pictime') { . . . $union = self::query()->selectRaw($select)-> WHERE ('userid', $userIds[0]) -> WHERE ('status', $userIds[0]) self::TYPE_STATUS_NORMAL) ->whereNotIn('mid', $batchPickedFootprints[$userIds[0]]) ->orderBy('id', 'desc') ->limit($pageCount); Unset ($userIds[0]); foreach ($userIds as $userId) { $unionItem = self::query()->selectRaw($select)->where('userid', $userId) ->where('status', self::TYPE_STATUS_NORMAL) ->whereNotIn('mid', $batchPickedFootprints[$userId]) ->orderBy('id', 'desc') ->limit($pageCount); $union->unionAll($unionItem); } $allUserFootprints = $union->get()->toArray(); $res = []; $chunkFootprintByUserid = self::_chunkFootprintByUserid($allUserFootprints); Foreach ($allUserIds as $userId) {$list = $chunkFootprintByUserid[$userId]? []; $count = count($list); $res[$userId]['list'] = $list; $res[$userId]['more'] = $count < $pageCount ? 0:1; $res[$userId]['track'] = $count > 0 ? (string)$list[$count - 1]['id'] : ''; } return $res; }Copy the code

Pay attention to

For the sake of compactness, code that is not relevant to the article is omitted from the code section, using three vertical ones. Omitted.

Participating in interactive

If you have any good ideas, please feel free to comment in the comments section.