这是我正在使用的数组的一个简短示例。
Array ( [0] => Array ( [Name] => somename [Attributes] => Array ( [Body] => somebody
[Region] => someregion [State] => somestate [Url] => someurl [Date] => somedate [Tz] =>
sometz [Title] => sometitle [City] => somecity ) ) [1] => Array ( [Name] => somename1
[Attributes] => Array ( [Body] => somebody1 [Region] => someregion1 [State] => somestate1
[Url] => someurl1 [Date] => somedate1 [Tz] => sometz1 [Title] => sometitle1 [City] =>
somecity1 ) ) )
我只需要foreach语句的帮助就可以获得每个数组的值。
任何基于日期对这些数组进行排序的示例都将不胜感激。
感谢
查看php文档会有所帮助:
http://php.net/manual/en/control-structures.foreach.php
在这种情况下,你只想做:
foreach (your_array_name as $value)
//do something with $value
解决方案是使用两个嵌套的foreach():
foreach ($array as $value) {
foreach($value as $v){
echo $v;
}
}
最后,为了对数组进行排序,可以使用usort()或cmp()。不要忘记对日期使用相同的格式(最好是ISO 8601),这样您就可以轻松地对它们进行排序。
使用多重排序技术http://in.php.net/array_multisort
$array1 = array(0 => array('name' => somename1, 'Attributes' => array('Body' => somebody,
'Region' => someregion, 'State' => somestate, 'Url' => someurl, 'Date' => '26-02-2011', 'Tz' =>
sometz, 'Title' => sometitle, 'City' => somecity)),
1 => array('name' => somename2, 'Attributes' => array('Body' => somebody,
'Region' => someregion, 'State' => somestate, 'Url' => someurl, 'Date' => '25-02-2011', 'Tz' =>
sometz, 'Title' => sometitle, 'City' => somecity)));
foreach ($array1 as $index => $inner_arr) {
$attr = $inner_arr['Attributes'];
foreach ($attr as $key => $value) {
$date_arr[$index] = strtotime($attr['Date']);
}
}
//for multisort give date array as the array accoring to which the data to be sorted.
//We are dumping date into array and trying to sort data according tot he date array sorted.
array_multisort($date_arr,$array1);
//after sorting loop thru data
foreach ($array1 as $inner_arr) {
echo "name:". $inner_arr['name'];
foreach ($inner_arr['Attributes'] as $key => $value) {
echo "<br>$key: $value";
}
echo '<hr>';
}