1

Hi i am new to php actully i want to convert this array

Array(
[0] => 
[1] => 2
[2] => 6
[3] => 7
[4] => 
[5] => 1
[6] => 5
[7] => 6
[8] => 
[9] => 1
[10] => 3
[11] => 5)

I have empty valus at index 0,4,8
to another array like

Array([0] => 2,6,7
[1] => 1,5,6
[2] => 1,3,5)
3
  • 1
    array_chunk() & foreach() & implode() Commented Jan 20, 2018 at 8:26
  • And what have you tried in order to do your homework? What are the problems? In any case, start off with a description of what you want done, then translate the whole thing into PHP. Commented Jan 20, 2018 at 8:31
  • Do you want the split to be on the blank items or in chunks of 3? Commented Jan 20, 2018 at 8:32

3 Answers 3

2

1st way if you want to group based on empty values but they are at randome indexes

$new_array = [];

$data = '';
foreach($array as $key=>$arr){
   if($arr == ''){
    if($key >0){
      $new_array[] = trim($data,',');
      $data = '';
    }
  }else{
     $data .= ','.$arr;
  }
}
$new_array[] = trim($data,',');
print_r($new_array);

Output:- https://eval.in/938714

2nd way if you want to group based on empty values and they are at sequential indexes like 0,4,8,12...

$array = array_chunk($array,4);

foreach($array as &$arr){
   $arr = trim(trim(implode(',',$arr),','));
}

print_r($array);

Output:- https://eval.in/938698

3rd way. if you want to group based on empty values and they are at sequential indexes like 0,4,8,12... (another approach)

$array = array_chunk(array_values(array_filter($array)),3);

foreach($array as &$arr){
   $arr = implode(',',$arr);
}

print_r($array);

Output:-https://eval.in/938692

Reference:-

array_chunk()

foreach()

implode()

array_filter()

array_values()

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

2 Comments

This obviously only works with regular groups of 3. I am not sure if that is what the OP is after. I read the question as such that the "empty" items should be seen as separators regardless of where they are positioned.
@rolfv1 added solution for that too.Please check
1

Short implode() + preg_split() solution:

Extended input array is used:

$arr = ['', '', 2, 6, 7, '', '', 1, 5, 6, 8, 9, '', 1, 3, 5, 7, '', 10, 11, ''];
$result = preg_split('/,,+/', trim(implode(',', $arr), ','));

print_r($result);

The output:

Array
(
    [0] => 2,6,7
    [1] => 1,5,6,8,9
    [2] => 1,3,5,7
    [3] => 10,11
)

2 Comments

With such pattern it's easy to use explode(',,',
@splash58, preg_split is more unified in case if there would be multiple gaps like [' ', 1, 2, ' ', ' ', ... You may check my update
0

When meet a '', just make a subarray and append the previous non-empty array to the result. Demo.

This only cost O(n)

$result = [];
$sub = [];
foreach($array as $v){
    if($v != ''){
        $sub[] = $v;
    }else{
        if(count($sub))  // avoid continus ''
            $result[] = $sub;
        $sub = [];
    }
}

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.