0

I have array like this

array (size=6)
  0 => string '2      16     10     4      0      0      0      0      0'     
  1 => string '0      0      0      4' 
  2 => string '2      15     8      6      0      0      0      0      0' 
  3 => string '0      0      0      3' 
  4 => string '3      18     12     5      0      0      0      0      0' 
  5 => string '0      0      0      2'

I want to divide the array and create a new array like

array1 (size = 1)
0 => '2 16 10 4 0 0 0 0 0 0 0 0 0 4'

array2 (size = 1)
0 => '2 15 8 6 0 0 0 0 0 0 0 0 3'

array3 (size = 2)
0 => '3 18 12 5 0 0 0 0 0 0 0 0 2'

array_chunk() works fine. But it not supported my array

1
  • 4
    StackOverflow is not a coding service. Please provide code snippets of what you've attempted so far. Commented Dec 30, 2017 at 7:05

3 Answers 3

1

use array_chunk($array_name, 2)

the above will return a multi dimension array.

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

Comments

0

You can do it through array_chunk() and foreach()

$new_array = array_chunk($original_array,2);

$final_array = [];

foreach($new_array as $arr){
 $final_array[] = $arr[0].' '.$arr[1];
}

print_r($final_array);

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

Note:- If you want to remove extra white-spaces in-between the strings, then use preg_replace()

$new_array = array_chunk($original_array,2);

$final_array = [];

foreach($new_array as $arr){
 $final_array[] = preg_replace('/\s+/', ' ', $arr[0]).' '.preg_replace('/\s+/', ' ', $arr[1]);
}

print_r($final_array);

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

3 Comments

@Athi no that's not the correct behaviour. editing question again and again and asking for code is not correct. You have to ask new question regarding that. Thanks
Sorry sir, Please help me
@Athi as i said close this question by reverting question back to previous and mark answer as accepted,and then ask a new question input+code-effort_expected outcome. You will get answer for-sure. You can address me the link and if i wll free i will answer also.
0

Method #1: (Demo)

$prepped_copy=preg_replace('/\s+/',' ',$array);  // reduce spacing throughout entire array
while($prepped_copy){  // iterate while there are any elements in the array
    $result[]=implode(' ',array_splice($prepped_copy,0,2));  // concat 2 elements at a time
}

var_export($result);

Method #2 (Demo)

$pairs=array_chunk(preg_replace('/\s+/',' ',$array),2);  // reduce spacing and pair elements
foreach($pairs as $pair){
    $result[]="{$pair[0]} {$pair[1]}";  // concat 2 elements at a time
}

var_export($result);

Both Output:

array (
  0 => '2 16 10 4 0 0 0 0 0 0 0 0 4',
  1 => '2 15 8 6 0 0 0 0 0 0 0 0 3',
  2 => '3 18 12 5 0 0 0 0 0 0 0 0 2',
)

To my surprise, Method #1 was actually slightly faster using the small sample dataset (but not noticeably so).

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.