0

i have a multiple array.. the output looks like this:

Array
(
    [0] => Array
        (
            [0] => AdsBot
            [1] => 7

        )

    [1] => Array
        (
            [0] => SurveyBot
            [1] => 1

        )

    [2] => Array
        (
            [0] => bingbot
            [1] => 3

        )

    [3] => Array
        (
            [0] => bot
            [1] => 27

        )

what i need now is to sort arrays by there number.. so it should look exactly like this:

Array
(

    [0] => Array
        (
            [0] => bot
            [1] => 27

        )

    [1] => Array
        (
            [0] => AdsBot
            [1] => 7

        )

    [2] => Array
        (
            [0] => bingbot
            [1] => 3

        )    

    [3] => Array
        (
            [0] => SurveyBot
            [1] => 1

        )

i need to sort it by the numbers array key.. but i really dont know how- well, i'm new to php

the multi. array code :

$bot_array = [
        ['name' => 'bingbot', 'number' => $bingbot],
        ['name' => 'googlebot', 'number' => $googlebot],
        ['name' => 'robots.txt', 'number' => $robots_txt],
        ['name' => 'exabot', 'number' => $exabot],
        ['name' => 'bot', 'number' => $bot],
        ['name' => 'robot', 'number' => $robot],
        ['name' => 'BaiDuSpider', 'number' => $BaiDuSpider],
        ['name' => 'Yahoo Slurp', 'number' => $yahoo_slurp],
        ['name' => 'AdsBot', 'number' => $adsbot],
        ['name' => 'SurveyBot', 'number' => $surveybot],
        ['name' => 'scanner', 'number' => $scanner],
        ['name' => 'checker', 'number' => $checker],
        ];

or maybe there is a more smarter way to do this? i need this for a top ten :) on the left should be written all the names and at the right the quantity

thanks for any help :)

EDIT:

$tmp = Array();
foreach($bot_array as &$ba)
    $tmp[] = &$ba["number"];
array_multisort($tmp, $bot_array);
foreach($bot_array as &$ba)
    echo $ba["number"]."<br/>";

i did this but it still doesnt sort it like i want it..

0 
0
0 
1 
10 
12 
27 
3 
3 
5 
7 
9 

this is what it gives me now :o

PHPprogrammer42
  • 355
  • 1
  • 3
  • 7

5 Answers5

1

you can use this function

function sortAscending($accounts, $key)
{
    $ascending = function($accountA, $accountB) use ($key) {
        if ($accountA[$key] == $accountB[$key]) {
            return 0;
        }
        return ($accountA[$key] < $accountB[$key]) ? -1 : 1;
    };
    usort($accounts, $ascending);

    return $accounts;
}
synan54
  • 658
  • 6
  • 16
1

You can use usort

<?php
$data = array(
    array('AdsBot', 7),
    array('SurveyBot', 1),
    array('bingbot', 3),
    array('bot', 27)
);

usort($data, 'botSort');

function botSort($val1, $val2) {

    if (is_array($val1) && is_array($val2)) {
        if ($val1[1] <= $val2[1]) {
            return 1;
        }
    }

    return -1;
}

var_dump($data);

Output

array(4) {
  [0] =>
  array(2) {
    [0] =>
    string(3) "bot"
    [1] =>
    int(27)
  }
  [1] =>
  array(2) {
    [0] =>
    string(6) "AdsBot"
    [1] =>
    int(7)
  }
  [2] =>
  array(2) {
    [0] =>
    string(7) "bingbot"
    [1] =>
    int(3)
  }
  [3] =>
  array(2) {
    [0] =>
    string(9) "SurveyBot"
    [1] =>
    int(1)
  }
}

According to your updated question, it should be.

<?php
usort($bot_array, 'botSort');

function botSort($val1, $val2) {

    if (is_array($val1) && is_array($val2)) {
        if ($val1['number'] <= $val2['number']) {
            return 1;
        }
    }

    return -1;
}

print_r($data);
Pradeep Sanjaya
  • 1,816
  • 1
  • 15
  • 23
0

Try usort(). It allows you to define a callback to sort on a custom expression. In your case you want to reverse sort by the second indexed element in each sub-array:

<?php

$bots = [
    [
        'name'   => 'AdsBot',
        'number' => 7,

    ],
    [
        'name'   => 'SurveyBot',
        'number' => 1,

    ],
    [
        'name'   => 'bingbot',
        'number' => 3,

    ],
    [
        'name'   => 'bot',
        'number' => 27,

    ],
];

usort($bots, function($a, $b) {
    return $a['number'] <= $b['number'];
});

print_r($bots);

Yields:

Array
(
    [0] => Array
        (
            [name] => bot
            [number] => 27
        )

    [1] => Array
        (
            [name] => AdsBot
            [number] => 7
        )

    [2] => Array
        (
            [name] => bingbot
            [number] => 3
        )

    [3] => Array
        (
            [name] => SurveyBot
            [number] => 1
        )

)

Hope this helps :)

Darragh Enright
  • 13,676
  • 7
  • 41
  • 48
0

Please, see bellow code.

$arr = array(
    0 => array(
        '0' => 'AdsBot',
        '1' => 7
    ),
    1 => array(
        '0' => 'SurveyBot',
        '1' => 1
    ),
    2 => array(
        '0' => 'bingbot',
        '1' => 3
    ),
    3 => array(
        '0' => 'bot',
        '1' => 27
    )
);

echo '<pre>';
print_r(sortDescOrder($arr,'1'));
echo '</pre>';

function sortDescOrder($accounts, $key)
{
    $ascending = function($accountA, $accountB) use ($key) {
        if ($accountA[$key] == $accountB[$key]) {
            return 0;
        }
        return ($accountA[$key] > $accountB[$key]) ? -1 : 1;
    };
    usort($accounts, $ascending);

    return $accounts;
}

Or see another code

    $bot_array = [
        ['name' => 'bingbot', 'number' => 5],
        ['name' => 'googlebot', 'number' => 8],
        ['name' => 'robots.txt', 'number' => 3],
        ['name' => 'exabot', 'number' => 7],
        ['name' => 'bot', 'number' => 7],
        ['name' => 'robot', 'number' => 12],
        ['name' => 'BaiDuSpider', 'number' => 45],
        ['name' => 'Yahoo Slurp', 'number' => 18],
        ['name' => 'AdsBot', 'number' => 78],
        ['name' => 'SurveyBot', 'number' => 96],
        ['name' => 'scanner', 'number' => 41],
        ['name' => 'checker', 'number' => 10]
        ];

usort($bot_array, function($x, $y) {
    return $x['number'] <= $y['number'];
});

echo '<pre>';
print_r($bot_array);
echo '</pre>';
Jakir Hossain
  • 2,457
  • 18
  • 23
0

Use this way to sort your array

  $arry = array( array('AdsBot','7'),
            array('SurveyBot','1'), 
            array('bingbot','3'),
            array('bot','27') 
       ); 

   echo "<pre>"; print_r($arry); echo "</pre>"; 

  function sortByOrder($a, $b) {
     return $a['1'] - $b['1'];
  }

   usort($arry, 'sortByOrder');   

   echo "<pre>"; print_r($arry); echo "</pre>";    
Dharma
  • 1
  • 5