phplaravelarray-map

Remove non-digits from all values in a flat array


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?


Solution

  • 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