0

I have a controller in Laravel

This is my collection

$milestones = $this->getmilestones();
dump($milestones);

and the value is

array:3 [▼
  0 => "["109"
  1 => "110"
  2 => "111"]"
]

And I tried this code based on the answer here. So, I have code like this

array_unshift($milestones, $milestones[0]);
unset($milestones[0]);
dump($milestones);

and the value is (index was changed)

array:3 [▼
  1 => "["109"
  2 => "110"
  3 => "111"]"
]

So, after unshifting the collections, I tried to use array_map to convert array of strings to array of integers.

$milestones = array_map('intval', $milestones);
dump($milestones);

But, I still got the same value. The first index returns 0 like this

array:3 [▼
  1 => 0
  2 => 110
  3 => 111
]

What should I do?

spaceplane
  • 607
  • 1
  • 12
  • 27

3 Answers3

0

Try this one

array_splice($milestone, 0, 1);
dump($milestone);
Jesus Erwin Suarez
  • 1,571
  • 16
  • 17
0

Use array_values this should re-index your array the way you need it:

$milestones = array_values($milestones);

If $milestones is a collection:

$milestones = $milestones->values();

The method values() will call array_values on your items defined in your collection instance.

Source: http://php.net/manual/en/function.array-values.php

Adam Rodriguez
  • 1,850
  • 1
  • 12
  • 15
0

Ah, finally I got the results that I wanted. I try to remove square brackets and double quote. Because milestones is collection. So my code is

$milestones = str_replace(array('[', ']', '"'),'',$milestones);

Thank you all for your help