Redo array with maximum position limit

Asked

Viewed 41 times

1

I’m working with Laravel and was looking at a bootstrap template to customize the application and came across this format to display the followers:

inserir a descrição da imagem aqui

My question is how could treat the array that brings my users only with the photo and if the array is larger than 3 as in the example bring the count of other users.

Edit:

$network_users = User::select('photo')->where('parent', Auth::user()->id)->get()->toArray();

$network_images = array();
foreach ($network_users as $user) {
    if (count($network_users) >= 4) {
        $count = count($network_users) - 4;
        array_push($network_images, $user);
    } else {
        $count = count($network_users);
        array_push($network_images, $user);
    }
}
$data = array(
    'count' => $count,
    'network_images' => $network_images
);
dd($data);

I set up in this format followed by the idea of using the Count, this would be the most correct way to bring the data or would have some better way?

1 answer

1


Just use the math, see:

Suppose you have a list of 15 users and you have shown 3 of them. The rest of users can be obtained by subtracting 15 (the total length of the list) and 3 (the number of users shown by default).

You can do this quite simply by using the function count to determine the length of the list.

Something like that:

<?php

// [...]

$usuarios_restantes = count($lista_de_usuarios) - 3;

// [...]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.