June 29, 2013

Unsort in array

Alex’s Question:

Good day.

I would be like get 3 keys from $arr where value $arN[0] will be more than other…

Code:

$ar1=array(201,281);
$ar2=array(1252760,1359724);
$ar3=array(452760,34349724);
$ar4=array(1260,134344);
$ar5=array(232750,1359724);
$ar6=array(60,1439724);

$arr[]=array(6299927 => $ar1);
$arr[]=array(1252760 => $ar2);
$arr[]=array(3432444 => $ar3);
$arr[]=array(3435543 => $ar4);
$arr[]=array(7645466 => $ar5);
$arr[]=array(4574534 => $ar6);

Next function sorting array $a descending:

function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? 1 : -1;
}

$a = array(3, 2, 5, 6, 1);

usort($, "cmp");

foreach ($a as $key => $value) {
    echo "$key: $valuen";
}
outpoot: 0:6 1:5 2:4 3:2 4:1

But how change this function for my example(for my big array)?

Tell me please how make this?

How write right?

Very confusing but I think you are looking for something like this. It will give you the index of the array whose first item is greater than the first item in the next array.

foreach($arr as $key => $array) {
    if($array[0] > $arr[$key+1][0]) {
       echo $key;
    }
}
June 11, 2013

Php array duplicates separation

Joshua Fabillar’s Question:

In PHP I have a arrays of id

$ids = array(1, 1, 1, 2, 2, 3, 3);

I just wanted to separate the 1 which will become 1, 1, 1 and 2 become 2, 2 and 3 become 3, 3.
How can I do that in PHP?

You can do this

$ids = array(1, 1, 1, 2, 2, 3, 3);
foreach($ids as $key) {
    //Calculate the index to avoid duplication
    if(isset(${"arr_$key"})) $c = count(${"arr_$key"}) + 1;
    else $c = 0;
    ${"arr_$key"}[$c] = $key;
}

Demo

April 15, 2012

php arrays sorting contained values

Question by enrico

Ok, first of all, I’m not even sure the title is right, if so, I’m sorry.

I have this loop here which is the result of a MongoDB query:

foreach($cursor as $obj) {
   $monster = $obj["type"];
   $strenght = $obj["strenght"];
   $obj["value"] = rand(5, 15);
}

now, I have put rand there to signify that value changes for each iteration. Now i want that this array, when is printed, is ordered by that $obj["value"], and be able to chose if ascending or descending.


ok, I have tried this

foreach($cursor as $obj) {
   $type = $obj["monster"];
   $strenght = $obj["strenght"];
   $obj["value"] = rand(5, 15);

   $newarr[] = $obj;
}

    usort($newarr, "cmp");
    function cmp($a, $b)
    { return $b['value'] < $a['value']; }

    foreach ($newarr as $obj)
    {
        echo $obj['value'] . $obj['type'] . "<br/>";
    }

As I expected, the

 $obj["value"] = rand(5, 15);

does not get lost at every iteration in fact, the $newarr contains that value, the problem is that it does not sort them at all. The items are printed in the same order as they were put inside the array. Any help?

Thanks

Answer by thecodeparadox

function mysort ($arr,$d='asc') { 
        global $dir; 
        $dir = $d; 
        uasort($arr, 'cmp'); 
        return ($arr); 
    } 
    function cmp ($a, $b) {
        global $dir;
        if($a['value'] == $b['value']) return 0;
        else  {
            if(strtolower($dir) == 'asc')
                return ($a['value'] > $b['value']) ? 1 : -1;
            else if(strtolower($dir) == 'disc')
                return ($a['value'] > $b['value']) ? -1 : 1;

        }
    } 
    print_r(mysort($obj, 'disc'));

ACCORDING TO YOUR UPDATE

try this cmp()

function cmp($a, $b) {
    if($a['value'] == $b['value']) return 0;
    return $a['value'] > $b['value'] ? 1 : -1;
}

Answer by Starx

First of all, by doing $obj["value"] = rand(..) you are assigning same array variable with different values multiple times. By the end of the loop, the variable will only contain one value.

You probably were trying to do this

$obj["value"][] = rand(5, 15); //This adds a new random item to the array contained in $obj['value'] each time

When you have an array items, you can sort them by using sort()[ascending] function rsort()[Descending[

$sorted = sort($obj["value"]);
print_r($sorted);
...

Please fill the form - I will response as fast as I can!