1

I want to put my string (separated by comma) in 2 different arrays.

$str = 1,First,2,Second,3,Third,4,Forth,5,Fifth

My goal is to group the numbers and the text.

$number = {1,2,3,4,5)
$text = {first, second, third, forth, fifth}

I tried to use explode(), but I only be ale to create a single array.

6
  • Are you sure that there will be always number in first array. Commented May 5, 2015 at 6:52
  • Show your code/attempt! Commented May 5, 2015 at 6:53
  • may be use for loop & create array for those at odd and those at even position Commented May 5, 2015 at 6:54
  • 1
    @N.M.N, actually that was my 1st idea, ut unfortunately I dont know how to.. Commented May 5, 2015 at 6:56
  • 1
    @khangkhungkhernitz You can accept one of these answers which really helped you as it seems all of these answers are awesome Commented May 5, 2015 at 7:23

7 Answers 7

4

This should work for you:

First explode() your string by a comma. Then you can array_filter() the numbers out. And at the end you can simply take the array_diff() from the $arr and the $numbers array.

<?php

    $str = "1,First,2,Second,3,Third,4,Forth,5,Fifth";
    $arr = explode(",", $str);

    $numbers = array_filter($arr, "intval"); 
    $text = array_diff($arr, $numbers);

?>
Sign up to request clarification or add additional context in comments.

3 Comments

It might look simple but it requires to iterate over the array twice. I expect other answers will show a better performance. At least this one. Hehe :)
@hek2mgl I think this would fall under micro-management. I think my code is pretty easy to read and understand.
@Rizier123 First, I have no issues with this. However, micro-optimization would be to ask if a while is slower than 3 ifs. But if you need to iterate once over an array instead of twice this makes a huge difference. Afar from hello world programs the input will not being that simple like in the example. Let's say it is a file with many lines of input.
3

You can use explode() and array_map() over the results:

$str = '1,First,2,Second,3,Third,4,Forth,5,Fifth';

array_map(function($item) use (&$numbers, &$strings) {
    if(is_numeric($item)) {
        $numbers []= $item;
    } else {
        $strings []= $item;
    }
}, explode(',', $str));

var_dump($numbers, $strings);

2 Comments

Yeah I really like the way to use array_map()
@Uchiha I suggest to have a look at their friends array_walk(), array_reduce() and array_walk_recursive() as well.
3
<?php

$str = array(1,'First',2,'Second',3,'Third',4,'Forth',5,'Fifth');

$letters =array();
$no = array();

for($i=0;$i<count($str);$i++){
  if($i%2 ==0){
    $letters[] = $str[$i];

  }else{
    $no[] = $str[$i];
  }
}
  print_r($no);
  print_r($letters);
?>

3 Comments

Not just me!!someone else also
My attempt,i had posted;one who asked the question if he wants it let he give upvote!!
Ok, I missed your comment below the answer. The situation was looking weird, sorry.
2

May be this can help you

$str = '1,First,2,Second,3,Third,4,Forth,5,Fifth';

$array = explode(',',$str);

$number = array();
$string = array();


foreach($array as $val)
{
    if(is_numeric($val))
    {
        $number[] = $val;
    }
    elseif(!is_numeric($val))
    {
        $string[] = $val;
    }
}

echo $commNum = implode(',',$number); // These are strings
echo '<br/>'.$commStr = implode(',',$string); // These are strings

echo '<pre>';
print_r($number);  // These are arrays

echo '<pre>';
print_r($string);  // These are arrays

Comments

2

This should work for you.

$str = '1,First,2,Second,3,Third,4,Forth,5,Fifth';
$result = explode(',',$str);
$number = array();
$text = array();
foreach(explode(',',$str) as $key => $value){
    if($key % 2 == 1){
        $text[] = $value;
    }elseif($key % 2 == 0){
        $number[] = $value;
    }
}
print_r($number);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
print_r($text);//Array ( [0] => First [1] => Second [2] => Third [3] => Forth [4] => Fifth )

and if your array is not consistent in this manner then some of these above answers are well suited for you like of Rizier123,hek2mgl,& Sunil's one

I'm updating my answer for your correspondent comments over Adrian

foreach(explode(',',$str) as $key => $value){
        if(is_numeric($value)){
            $number[] = $value;
        }else{
            $text[] = $value;
        }
    }

1 Comment

Great..! No one is considering my answer.. Good One
1

You can use explode, but you need apply some filter in this case is is_numeric:

<?php

$str = '1,First,2,Second,3,Third,4,Forth,5,Fifth, erewwrw , 6 ';

$array = explode(',', $str);

$array_numbers = array();
$array_letters = array();

for($i = 0; $i < count($array); $i++) {
    if(is_numeric(trim($array[$i]))) {
      $array_numbers[] = trim($array[$i]);  
    } else {
      $array_letters[] = trim($array[$i]);
    }       
}
print_r($array_numbers);
print_r($array_letters);

?>

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Array
(
    [0] => First
    [1] => Second
    [2] => Third
    [3] => Forth
    [4] => Fifth
)

3 Comments

If I have $str = '1,First,2,Second,3,Third,4,Forth,5,Fifth,xzcdx,10'; this string
@AjeetKumar But the string is $str = '1,First,2,Second,3,Third,4,Forth,5,Fifth';
But you have to give Appropriate result with your code '1,First,2,Second,3,Third,4,Forth,5,Fifth,xzcdx,10'
1

Assuming that your string contains number followed by string and so on,

following should be the solution.

Create two blank arrays: $numbers and $strings.

Just loop over the array and get even and odd elements.

Even elements should go to numbers array and odd elements should go to strings array.

$str = '1,First,2,Second,3,Third,4,Forth,5,Fifth';
$numbers = array();
$strings = array();
$temp = explode(',', $str);
$i=0;
foreach ($temp as $e) {
    if ($i%2) {
        $strings[] = $e;
    }
    else {
        $numbers[] = $e;
    }
    ++$i;
}

echo '<pre>';
print_r($numbers);
echo '</pre>';

echo '<pre>';
print_r($strings);
echo '</pre>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.