N
N
Nubbin2018-06-02 22:24:56
Laravel
Nubbin, 2018-06-02 22:24:56

Laravel Collection change keys?

Good night guys there is an array

Array
(
    [0] => Array
        (
            [name] => 412
        )

    [1] => Array
        (
            [name] => 3144
        )

    [2] => Array
        (
            [name] => 632
        )
)

How to make it not start from 0, but from 1.
1,2,3,4...
With the help of collect()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2018-06-02
@Nubbin

If we are talking about an array (the printout of which is given above, which is not a collection, despite the conditions of the problem), then:

$arr = ;
$new_arr = array_combine(range(1, count($arr)), array_values($arr));

var_dump($new_arr);
/*
array(3) {
  [1]=>
  array(1) {
    ["name"]=>
    int(412)
  }
  [2]=>
  array(1) {
    ["name"]=>
    int(3144)
  }
  [3]=>
  array(1) {
    ["name"]=>
    int(632)
  }
}
*/

Y
Yan-s, 2018-06-02
@Yan-s

Do you do perversions?
Well, this is how you can

$collection = collect();
$collection = $collection->prepend(0, '-1')->values()->except(0);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question