Astha
Astha

Reputation: 1734

Multi Dimensional Array resizing in php

I have a multi-dimensional array and what i want is in each second degree of array values limit should be set to 4 only. Like i have a array of type:

Array
(
 [test2] => Array
    (
        [0] => Array
            (
                [application_id] => 405275016
            )
        [1] => Array
            (
                [application_id] => 405275016
            )

        [2] => Array
            (
                [application_id] => 303198288
            )

        [3] => Array
            (
                [application_id] => 303841592
            )

    )

[test3] => Array
    (
        [0] => Array
            (
                [application_id] => 289267216
            )

        [1] => Array
            (
                [application_id] => 303198216
            )

        [2] => Array
            (
                [application_id] => 405275016
            )

        [3] => Array
            (
                [application_id] => 303198288
            )

        [4] => Array
            (
                [application_id] => 303841592
            )

        [5] => Array
            (
                [application_id] => 311430400
            )

        [6] => Array
            (
                [application_id] => 318096216
            )

        [7] => Array
            (
                [application_id] => 320256352
            )
    )

)

and what i want that if the inner arrays value exceed 5 count it should not add any further values to it. i have to format this above array to something like:

Array
(
 [test2] => Array
    (
        [0] => Array
            (
                [application_id] => 405275016
            )
        [1] => Array
            (
                [application_id] => 405275016
            )

        [2] => Array
            (
                [application_id] => 303198288
            )

        [3] => Array
            (
                [application_id] => 303841592
            )

    )

[test3] => Array
    (
        [0] => Array
            (
                [application_id] => 289267216
            )

        [1] => Array
            (
                [application_id] => 303198216
            )

        [2] => Array
            (
                [application_id] => 405275016
            )

        [3] => Array
            (
                [application_id] => 303198288
            )

        [4] => Array
            (
                [application_id] => 303841592
            )
    )

)

here in second array array last 3 arrays were truncated in order to take only 5 count as a value. I have tried many a methods but none achieved this.

Any idea on how can i achieve this will be highly appreciated ??

Upvotes: 0

Views: 1204

Answers (4)

deceze
deceze

Reputation: 522155

$array = array_map(function ($arr) { return array_slice($arr, 0, 5); }, $array);

Note that this uses PHP 5.3+ syntax.

If you actually want an array (or something like it) that cannot hold more than 5 elements, you'll need to implement a custom class (possibly by extending ArrayObject) and/or play around with SplFixedArray.

Upvotes: 2

Thorbear
Thorbear

Reputation: 2333

PHP has no ready functionality for this behavior, you'll have to make your own function for adding values to the array to achieve this. Ex:

function array_add_value($array, $value) {
    if(count($array) < 5) {
        $array[] = $value;
    }
}

If you wish to manipulate an existing array that already has too many elements, you will have to make a similar function for that Ex:

function fix_array($array) {
    if(count($array) > 5) {
        for($i=4;$i<count($array);$i++) {
            unset($array[$i]);
        }
    }
}

Upvotes: 1

mishu
mishu

Reputation: 5397

or you can use the array_slice function like this:

foreach ($array as $key=>$value)
{
    $array[$key] = array_slice($value, 0, 5);
}

Upvotes: 2

DaveRandom
DaveRandom

Reputation: 88677

foreach ($array as $k => $v) { // Loop outer array
  for ($i = 5; isset($array[$k][$i]); $i++) { // Loop inner array, starting at element #5
    unset($array[$k][$i]); // unset all elements >=5
  }
}

Upvotes: 1

Related Questions