0

I have an array like this way

Array
(
    [0] => Array
        (
            [items] => 42
            [prize] => 122
        )

    [1] => Array
        (
            [items] => 14
            [prize] => 789
        )

    [2] => Array
        (
            [items] => 76
            [prize] => 1228
        )

    [3] => Array
        (
            [items] => 23
            [prize] => 122
        )

    [4] => Array
        (
            [items] => 15
            [prize] => 567
        )

)

From this i want to create two arrays one contains the maximum number of items and other contains minimum number of items

Expected output

array1(maximum)

[0] => Array
        (
            [items] => 76
            [prize] => 1228
        )

    [1] => Array
        (
            [items] => 42
            [prize] => 122
        )

    [2] => Array
        (
            [items] => 23
            [prize] => 122
        )

Array 2(minmal)

[0] => Array
        (
            [items] => 15
            [prize] => 567
        )

    [1] => Array
        (
            [items] => 23
            [prize] => 122
        )

n is length original array

if(n is odd)

length firstarr=ceil(n/2);

length secondarr =n- ceil(n/2)

else 

length firstarr=secondarr=n/2

So how can i spli the above with maximum and minimum range of values?

2 Answers 2

1

You should begin by sorting the array into order by number of items. We'll go with ascending order but it really doesn't matter that much.

There are a few different ways to achieve this. You could use array_multisort but I think usort with an anonymous function is a bit shorter and easier to follow.

If you have PHP 5.2 or earlier you'll have to move the anonymous function code into a named function and call it that way. If you're lucky enough to have PHP 7 you can just do return $a["items"] <=> $b["items"]; in the function.

$array = [["items" => 42, "prize" => 122], ["items" => 14, "prize" => 789],
          ["items" => 76, "prize" => 1228], ["items" => 23, "prize" => 122],
          ["items" => 15, "prize" => 567]];
usort($array, function($a, $b) {
    return $a["items"] < $b["items"] ? -1 : ($a["items"] > $b["items"] ? 1 : 0); });

Now split it into the highest and lowest values. After the array_splice the $array variable will be left containing only the values that haven't been removed and saved into $lowest.

In your example the highest values were in descending order but ours are in ascending order. array_reverse will fix this.

$lowest = array_splice($array, 0, floor(count($array) / 2));
$highest = array_reverse($array);

var_dump($highest);
var_dump($lowest);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use usort() and pass the values to a callback to compare, notice the use of closure in the callback,

<?php

function sort_order($key, $order=1){
    if($order==1){
        return function ($a, $b) use ($key) {
            return strcmp("$a[$key]","$b[$key]");
        };        
    }
    else{
        return function ($a, $b) use ($key) {
            return strcmp("$b[$key]","$a[$key]");
        };  
    }

}

$minimal = array
(
    0 => array
        (
            'items' => 42,
            'prize' => 122
        ),

    1 => array
        (
            'items' => 14,
            'prize' => 789
        ),

    2 => array
        (
            'items' => 76,
            'prize' => 1228
        ),

    3 => array
        (
            'items' => 23,
            'prize' => 122
        ),

    4 => array
        (
            'items' => 15,
            'prize' => 567
        )


);


$maximum = $minimal;

usort($minimal, sort_order('items'));
usort($maximum, sort_order('items',2));
print_r($minimal);
print_r($maximum);
?>

Here is the fiddle : https://eval.in/539270

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.