English 中文(简体)
A. 某些关键因素的阵容
原标题:Sorting an array by a certain key

我有以下阵列:

Array
(
    [Places] => Array
        (
            [public] => 0
            [entities] => Array
                (
                    ...
                )
        )
    [Issues] => Array
        (
            [public] => 1
            [entities] => Array
                (
                    ...
                )
        )

    [Source] => Array
        (
            [public] => 0
            [entities] => Array
                (
                    ...
                )
        )
)

我愿通过公共钥匙对阵列进行分类。 我知道,我可能不得不使用<代码>sort或usort,但我不知道如何执行。

任何想法都会受到极大感谢。

最佳回答
usort($array, function ($a, $b) { return $a["public"] - $b["public"]; });
问题回答

http://www.the-art-of-web.com/php/sortarray/“rel=“nofollow noreferer”>。 http://www.the-art-of-web.com/php/sortarray/。

我将尝试

usort(usort(array, function), function);

我可以应要求尝试一部样本代码,但已经掌握了这一资料。

采用阵列:

$test   =   array(
         Places  => array(
             public  => 0,
             entities  => array(

            )
        ),
         Issues  => array(
             public  => 1,
             entities  => array()
        ),
         Source  => array(
             public  => 0,
             entities  => array()
        )
    );

    echo  <pre> ;
    print_r($test);
    echo  </pre> ;

    array_multisort($test,SORT_ASC,$test);

    echo  <pre> ;
    print_r($test);
    echo  </pre> ;

采用<代码>array_multisort进行这项工作的一般办法是将你的分类价值放在自己的阵列中,然后将这两种阵列作为主要的分类阵列。

如下:

$test   =   array(
     Places  => array(
         public  => 0,
         entities  => array(

        )
    ),
     Issues  => array(
         public  => 1,
         entities  => array()
    ),
     Source  => array(
         public  => 0,
         entities  => array()
    )
);

echo  <pre> ;
print_r($test);
echo  </pre> ;

$sort = array();
foreach ($test as $k => $a) {
    $sort[$k] = $a[ public ];
}

// placing $sort first in array_multisort causes $test to be sorted in same order as the values in $sort
array_multisort($sort,SORT_ASC,$test);

echo  <pre> ;
print_r($test);
echo  </pre> ;

你可以利用我们的召回功能。

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

usort($array, "cmp");

为此:

$code = "return (-1*strnatcmp($a[ public ], $b[ public ]));";
uasort($array, create_function( $a,$b , $code));




相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签