PHP multidimensional array sorting and Operation function

Source: Internet
Author: User

PHP tutorials Multidimensional array sorting and Operation functions
function ARRAYCD ($arr) {
Static $res _arr=array ();
if (Is_array ($arr))
{
foreach ($arr as $key => $val)
{
if (Is_array ($val))
{
ARRAYCD ($val);
}else{
$res _arr[]= $val;
}
}
}elseif (is_string ($arr))
{
$res _arr[]= $arr;
}
Return implode (",", $res _arr);
}

$fruits = Array (
"Fruits" => Array ("A" => "Orange", "B" => "banana", "C" => "Apple"),
"Numbers" => Array (1, 2, 3, 4, 5, 6),
"Holes" => Array ("A", 5 => "second", "third")
);

$strayy =ARRAYCD ($fruits);

Var_dump ($STRAYY);

Look at a multidimensional array sort

<?php
function Sortdata ($data, $col, $order) {
if (count ($data)) $temp _array[key ($data)] = Array_shift ($data);

foreach ($data as $key => $val) {
$offset = 0;
$found = false;
foreach ($temp _array as $tmp _key => $tmp _val) {
if (! $found and Strtolower ($val [$col]) >

Strtolower ($tmp _val[$col])) {
$temp _array = Array_merge ((array)

Array_slice ($temp _array,0, $offset),
Array ($key => $val),
Array_slice ($temp _array, $offset)
);
$found = true;
}
$offset + +;
}
if (! $found) {
$temp _array = Array_merge ($temp _array, array

($key => $val));
}
}
if (Strtolower ($order) = = "ASC") {
$array = Array_reverse ($temp _array);
} else {
$array = $temp _array;
}
return $array;
}

Main function

$data =array (
Array
' Name ' => ' Alice ',
' Key ' => ' 2fc4ab3d639e5400efdfc73bc27e83f1 ',
' Age ' =>20
),
Array
' Name ' => ' Claudia ',
' Key ' => ' 831c2b79c1f19af39c7e3321e11e5f5e ',
' Age ' =>18
),
Array
' Name ' => ' Beatrice ',
' Key ' => ' 6f8512a2066b8f35a27a495ce1228c76 ',
' Age ' =>100
),
Array
' Name ' => ' Denise ',
' Age ' =>25,
' Key ' => ' Ef6de3b178bf9f69a9fef72e4ee7bbe9 '
)
);

$arr 1 = sortdata ($data, "name", "ASC");
echo "<pre>";
Var_dump ($arr 1);
echo "</pre>";

$arr 2 = sortdata ($data, "age", "desc");
echo "<pre>";
Var_dump ($arr 2);
echo "</pre>";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.