-1

I am having a string as follows:

$str = ,1,2;

I used explode method and make an array as follows:

 $idsArray = explode(",",$str); 

I print the array as follows:

print_r($idsArray);

The result I got is as follows:

Array(
[0]=>
[1]=>1
[2]=>2

)

I need a result as

Array(
    [0]=>1
    [1]=>2

    )

How can I correct it and make the expected result?

Santhucool
  • 1,656
  • 2
  • 36
  • 92

5 Answers5

1

You can simply use array_filter as

$idsArray = array_values(array_filter(explode(',',",1,2")));
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
0

use array_filter

print_r(array_filter($idsArray));

Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.

EDIT 01

$new_array = array_filter($idsArray);
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

Try this..

Use array_filter

print_r(array_values(array_filter($idsArray)));

Array( [0]=>1 [1]=>2 )

http://php.net/manual/en/function.array-filter.php

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

Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
0

Try this

$str = ",1,2";
$newarry = explode(',',trim($str,','));
Sathish
  • 2,440
  • 1
  • 11
  • 27
0

Try this

$tags = array_filter( explode(",", $str) ); 
var_dump($tags);
Gowtham
  • 3
  • 4