Click to See Complete Forum and Search --> : Really need help in array sorting!!!


Last Minder
10-07-2008, 08:51 PM
First of all, thanks for everyone willing to helps me out~!



$test = array();

$test[0] = array
(
[id] => 0001
[content] => array
(
[clipA_exsiting] => "yes"
[clipB_exsiting] => "no"
[clipC_exsiting] => "yes"
)

}

$test[1] = array
(
[id] => 0002
[content] => array
(
[clipA_exsiting] => "no"
[clipB_exsiting] => "yes"
[clipC_exsiting] => "yes"
)

}


$test[2] = array
(
[id] => 0003
[content] => array
(
[clipA_exsiting] => "yes"
[clipB_exsiting] => "no"
[clipC_exsiting] => "no"
)

}


// Need Help for sorting the array and print out here



I am looking to get this out put:


if user choose sort content by ClipA:

id ......... ClipA ......... ClipB ......... ClipC
0001 ......... yes ......... no ......... yes
0003 ......... yes ......... no ......... no
0002 ......... no ......... yes ......... yes


if user choose sort content by ClipB:
id ......... ClipA ......... ClipB ......... ClipC
0002 ......... no ......... yes ......... yes
0001 ......... yes ......... no ......... yes
0003 ......... yes ......... no ......... no


if user choose sort content by ClipC:
id ......... ClipA ......... ClipB ......... ClipC
0001 ......... yes ......... no ......... yes
0002 ......... no ......... yes ......... yes
0003 ......... yes ......... no ......... no



Therefore, basicly I just want to sort this array $test by the sub-array $test[x][$content] base on user selection.

Am I make myself clear?

stephan.gerlach
10-08-2008, 03:49 AM
I don't think that php's array functions will do this for you.

You got to do your own code. maybe something like this.


$test = array();

$test[0] = array
(
[id] => 0001
[content] => array
(
[clipA_exsiting] => "yes",
[clipB_exsiting] => "no",
[clipC_exsiting] => "yes"
)

}

$test[1] = array
(
[id] => 0002
[content] => array
(
[clipA_exsiting] => "no",
[clipB_exsiting] => "yes",
[clipC_exsiting] => "yes"
)

}


$test[2] = array
(
[id] => 0003
[content] => array
(
[clipA_exsiting] => "yes",
[clipB_exsiting] => "no",
[clipC_exsiting] => "no"
)

}

// put in here by what you want to sort.
// clipA_exsiting or clipB_exsiting or clipC_exsiting
$orderby = 'clipA_existing';


$order[0] == 'yes';
$order[1] == 'no';


// loop twice through the code. Once for yes, once for no.
for ($i=0; $i<2; $i++) {

// loop through the arrays
foreach ($test as $te) {

// if it is matching then echo it.
if ($te['content'][$orderby] == $order[$i]) {
echo'<p>'.$te['id'].'.........'.$te['content']['clipA_existing'].'.........'.$te['content']['clipB_existing'].'.........'.$te['content']['clipC_existing'].'</p>';
}
}

}



Now I have not tested it but hope it will give you an idea how to do it.

Sodbuster
10-08-2008, 06:48 AM
Or you can use array_multisort():$sort_key = 'clipA_exsiting'; // [sic]

foreach ($test as $key => $each) {
$tmp[$key] = $each['content'][$sort_key];
}
array_multisort($tmp, SORT_DESC, $test);

echo 'id.........clipA.........clipB.........clipC<br />';
foreach ($test as $each) {
echo str_pad($each['id'], 4, '0', STR_PAD_LEFT);
foreach ($each['content'] as $content) {
echo '.........' . $content;
}
echo '<br />';
}

//display:
//id.........clipA.........clipB.........clipC
//0001.........yes.........no.........yes
//0003.........yes.........no.........no
//0002.........no.........yes.........yes

Last Minder
10-08-2008, 10:29 AM
Thanks both stephan.gerlach and Sodbuster, that's really helps a lot. Specially, Sodbuster that's really what I am looking for. Thank you very much!!!