Jump to content

Help on how to Sort Multi-Dimensional Array with Keys


nash_1126

Recommended Posts

Hi Im new here...  ;D

 

I just want to find out how do i actually re-format or re-sort an array of data (multi-dimensional array).

 

Example of the Array:

 

  Quote
Array

(

    [3] => Array

        (

            [0] => 2

            [1] => -2

            [2] => 2

            [3] => 4

        )

    [4] => Array

        (

            [0] => 0

        )

    [5] => Array

        (

            [0] => 1

            [1] => 0

        )

    [6] => Array

        (

            [0] => 0

            [1] => 0

            [2] => 1

        )

    [7] => Array

        (

            [0] => 1

            [1] => 9

        )

    [11] => Array

        (

            [0] => 0

            [1] => 1

        )

)

 

I need the data AS WELL as the Main Keys to be sorted out altogether (without seperating them).

 

I want the results to be like this: (sorted in desc order with keys still intact)

 

[7][1] => 9

[3][3] => 4

[3][0] => 2

[3][2] => 2

.... and so on

 

 

what you try to do

someting like this

<?php
$test = Array(
    3 => Array(
         0 => 2,
         1 => -2,
         2 => 2,
         3 => 4),
    4 => Array(
         0 => 0),
    5 => Array(
       0 => 1,
       1 => 0),
    6 => Array(
        0 => 0,
        1 => 0,
        2 => 1),
    7 => Array(
        0 => 1,
        1 => 9),
    11 => Array(
        0 => 0,
        1 => 1)
);
foreach ($test as $k1 => $v1){
foreach ($v1 as $k2 => $v2){
	$tmp[] = array( 'value' => $v2, 'key1' => $k1, 'key2' => $k2);
}
}
rsort($tmp);
print_r($tmp);
?>

If he has a really long string of data in those foreach loops wouldn't that kill the computer he is working on? Would adding the & help speed up a long array by making it a pointer instead of a duplicate array? I am asking because I am in a similar situation.

 

foreach ($test as $k1 => &$v1){
   foreach ($v1 as $k2 => &$v2){
      $tmp[] = array( 'value' => $v2, 'key1' => $k1, 'key2' => $k2);
   }
}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.