0

I'm in need of splitting of the single array into multiple array for some report generating purpose.

I have an array like this which I have given below.

Array
(
    [blah_1] => 1
    [blahblah_1] => 31
    [blahblahblah_1] => 25
    [blah_3] => 1
    [blahblah_3] => 3
    [blahblahblah_3] => 5
    [blah_10] => 1
    [blahblah_10] => 10
    [blahblahblah_10] => 2
)

I want to split the above to,

Array
(
    [blah_1] => 1
    [blahblah_1] => 31
    [blahblahblah_1] => 25
)

Array
(
    [blah_3] => 1
    [blahblah_3] => 3
    [blahblahblah_3] => 5
)

Array
(
    [blah_10] => 1
    [blahblah_10] => 10
    [blahblahblah_10] => 2
)

How can I do this in PHP ??

4
  • have you tried array_chunk? php.net/manual/en/function.array-chunk.php Commented May 29, 2012 at 12:09
  • Do all of the keys for each group always exist, or are the number of keys per group dynamic and determined only by the _n on the end? Commented May 29, 2012 at 12:13
  • Ya the groups are dynamic, sometimes it comes & sometimes it doesn't. I tried, Array_chunk & array_slice, both doesn't satisfy my need. This is purely determined by _n. Commented May 29, 2012 at 12:18
  • Added a downvote , because you didn't add an exact information in the question, that you would like to group by "_n" . If you don't tell me what you exactly need, we can't figure it out. Commented May 30, 2012 at 15:50

5 Answers 5

2
$oldarray=array('blah_1'=>1,'blahblah_1'=>31,'blahblahblah_1'=>25,
'blah_3'=>1,'blahblah_3'=>3,'blahblahblah_3'=>5,
'blah_10'=>1,'blahblah_10'=>10,'blahblahblah_10'=>2
)
$newarray=array();

foreach ($oldarray as $key=>$val) { //Loops through each element in your original array

    $parts=array_reverse(explode('_',$key)); //Splits the key around _ and reverses
    $newarray[$parts[0]][$key]=$val; //Gets the first part (the number) and adds the
    //value to a new array based on this number.

}

The output will be:

Array (
    [1]=>Array
    (
        [blah_1] => 1
        [blahblah_1] => 31
        [blahblahblah_1] => 25
    )

    [3]=>Array
    (
        [blah_3] => 1
        [blahblah_3] => 3
        [blahblahblah_3] => 5
    )

    [10]=>Array
    (
        [blah_10] => 1
        [blahblah_10] => 10
        [blahblahblah_10] => 2
    )
)
Sign up to request clarification or add additional context in comments.

4 Comments

I got the array like this, ``
I just fixed a few mismatched variables in the code I wrote - the reply box isn't great for formatting :L Should work fine now.
I got the array like what you have shown, but how do I get how I have shown above? or is there anyway that I can use it in while loop ? coz now the page loads, doesn't stops, I think infinite loop.
The only reason an infinite loop would be occuring is if the $oldarray value was being written to as the loop happened.
0

Use array_chunk. Example:

<?php
   $chunks = array_chunk($yourarray, 3);
   print_r($chunks);
?>

2 Comments

Will work if the keys are always grouped in the same quantities. Fails if, for example [blahblah_3] doesn't exist
He didn't mention that the keys must be grouped though.
0

Use array_chunk

i.e

$a = array(1,2,3,4,5,6,7);
var_dump(array_chunk($a, 3));

Comments

0

Not sure if you are looking for array_slice

but take a look at this example:

    <?php
        $input = array("a", "b", "c", "d", "e");


       print_r(array_slice($input, 2, -1));
       print_r(array_slice($input, 2, -1, true));
?>

will result in this:

Array
(
    [0] => c
    [1] => d
)
Array
(
    [2] => c
    [3] => d
)

Comments

-1
array_chunk ( array $input , int $size);

1 Comment

I can create a whole sentence for this response, I didn't feel it's necessary.

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.