Home > Net >  Get Value from Array in Laravel
Get Value from Array in Laravel

Time:08-01

I have an array like below

  0 => "16339"
  1 => "16483"
  2 => "16484"
]

I want this to [16339,16483,16484]

How can I get it, Please help me out.

CodePudding user response:

You can use array_values() to do this. This returns all the values from the array and indexes the array numerically.

$new_array = array_values($old_array);

Just a warning that re-indexing an array by array_values() may cause you to reach the memory limit unexpectly. For example, if your PHP momory_limits is 8MB,and says there's a BIG array $bigArray which allocate 5MB of memory.

Doing this will cause PHP exceeds the momory limits: It's because array_values() does not re-index $bigArray directly, it just re-index it into another array, and assign to itself later.

PHP Manuals - array_values

CodePudding user response:

$newArray = array_values($yourArray);

CodePudding user response:

you can use array_map() function, like this way ...

  $input = [ 0 => "16339",1 => "16483",2 => "16484"];
  return array_map('intval', $input);

this is the easiest way to convert your array.

  • Related