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:

 

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

 

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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);
   }
}

 

 

Link to comment
Share on other sites

I don't think it would hurt though I don't think it would be necessary unless you're talkin' about a really massive text file.  I don't think I'd go to using references there unless I found that the code was consuming too much memory.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.