0

I have this array but I want to sort it by my criteria.

I want if "country" and "top" are the same then they heve to be on one place. Slightly below give an example of what I mean.

$array = array(
    array("city" => "New York", "country" => "USA", "top" => "1"),
    array("city" => "London", "country" => "UK", "top" => "1"),
    array("city" => "Sofia", "country" => "BG", "top" => "1"),
    array("city" => "Belgrad", "country" => "SRB", "top" => "2"),
    array("city" => "Varna", "country" => "BG", "top" => "2"),
    array("city" => "LA", "country" => "UK", "top" => "1"),
    array("city" => "Bat", "country" => "USA", "top" => "1")
);

Here is the array:

Array
(
    [0] => Array
        (
            [city] => New York
            [country] => USA
            [top] => 1
        )

    [1] => Array
        (
            [city] => London
            [country] => UK
            [top] => 1
        )

    [2] => Array
        (
            [city] => Sofia
            [country] => BG
            [top] => 1
        )

    [3] => Array
        (
            [city] => Belgrad
            [country] => SRB
            [top] => 2
        )

    [4] => Array
        (
            [city] => Varna
            [country] => BG
            [top] => 2
        )

    [5] => Array
        (
            [city] => LA
            [country] => USA
            [top] => 1
        )

    [6] => Array
        (
            [city] => Bat
            [country] => UK
            [top] => 1
        )
)

I want this result:

Array
(
    [0] => Array
        (
            [0] => Array 
                (
                    [city] => New York
                    [country] => USA
                    [top] => 1
                )

            [1] => Array
                (
                    [city] => LA
                    [country] => USA
                    [top] => 1
                )
            [2] => Array
                (
                    [top_cities] => 2
                )
        )

    [1] => Array
        (
            [0] => Array 
                (
                    [city] => London
                    [country] => UK
                    [top] => 1
                )
            [1] => Array
                (
                    [city] => Bat
                    [country] => UK
                    [top] => 1
                )
            [2] => Array
                (
                    [top_cities] => 2
                )
        )

    [2] => Array
        (
            [0] => Array 
                (
                    [city] => Sofia
                    [country] => BG
                    [top] => 1
                )
        )

    [3] => Array
        (
            [city] => Belgrad
            [country] => SRB
            [top] => 2
        )

    [4] => Array
        (
            [city] => Varna
            [country] => BG
            [top] => 2
        )
)

Criteria(must match): Country Top and count how top have in this array(sum)

I will be very grateful if someone give a idea how to handle with this problem. Thanks in advance.

6
  • may be your expected output is wrong, or very unclear for a logic. Commented Jul 12, 2016 at 6:28
  • Start with foreach Commented Jul 12, 2016 at 6:30
  • @u_mulder okay but then. Did you have idea how to make it? Commented Jul 12, 2016 at 6:32
  • @bansi I post the result that i want. Commented Jul 12, 2016 at 6:34
  • Check country and place values into subarrays. Commented Jul 12, 2016 at 6:34

1 Answer 1

1

Try the below code. There is amall modification in the result. But I believe it will work for you

$array = array(
    array("city" => "New York", "country" => "USA", "top" => "1"),
    array("city" => "London", "country" => "UK", "top" => "1"),
    array("city" => "Sofia", "country" => "BG", "top" => "1"),
    array("city" => "Belgrad", "country" => "SRB", "top" => "2"),
    array("city" => "Varna", "country" => "BG", "top" => "2"),
    array("city" => "LA", "country" => "UK", "top" => "1"),
    array("city" => "Bat", "country" => "USA", "top" => "1")
);

$newArray = array();
foreach($array as $key => $val){
    $newArray[$val['country']][] = $val;
}
print_r($newArray);
Sign up to request clarification or add additional context in comments.

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.