How to Array Push in PHP & Laravel Using Key Value Pairs

Often times it can be confusing to Array push using key value pairs from an array. Here is a clear example of how to push into an Empty Array Using Key Value Pairs. In this example I will be using the Laravel Eloquent Model where am going to select users from the Database which is not a current user.


       // Create an Empty Array
        $otherUsersArr = [];
        // Will be used to iterate otherUsers Returned
        $i = 0;

        // We now use the Foreach method
        foreach ($otherUsers as $key => $user) {
            // Finally push the values to the new Array
            $otherUsersArr[$i] = json_encode($user);

        // Display the results
        return $otherUsersArr;