English 中文(简体)
结构分布
原标题:Restructure array
  • 时间:2012-04-30 08:33:53
  •  标签:
  • php
  • arrays

我有多方面的阵容,具有许多价值,这是最小的:

$test = array(
    array(
        name    =>  test ,
        date    =>  2012-04-30 11:06:01 
    ),

    array(
        name    =>  test2 ,
        date    =>  2012-04-30 11:07:00 
    )
);

而且,它只是继续推进......。

现在的日期是随意的,因此,需要区分这种阵列,这样,从最小到最大,更清楚的是,在这里,我完全的阵列堆放场:。 页: 1 日期是任意的......

最佳回答

usort

From Manual:

这一功能将使用用户提供比较功能,根据其价值分类。 如果你希望分类的阵列需要某些非属地标准,那么你就应当利用这一功能。

function cmp($a, $b){
     return strtotime($a[ date ])-strtotime($b[ date ]);
}

usort($test,  cmp );
问题回答

你可以这样作,

usort() and DateTime class(if your using PHP5 or higher).

你们需要界定一项职能,对两个要素进行比较(在这种情况下,各阵列也是这样)。

function cmp($array1, $array2)
{
  $date1 = new DateTime($array1[ sent ]);
  $date2 = new DateTime($array2[ sent ]);

  if($date1 == $date2)
    return 0;

  return ($date1 < $date2) ? -1 : 1;
}

然后请:

usort($test,  cmp );

我将访问array_multisort(





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签