这是我正在使用的数组的简短示例。
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语句来获取每个数组的值。
任何根据日期对这些数组进行排序的例子也会受到赞赏。
由于
答案 0 :(得分:3)
有助于查看php文档:
http://php.net/manual/en/control-structures.foreach.php
在这种情况下你只想做:
foreach (your_array_name as $value)
//do something with $value
答案 1 :(得分:2)
解决方案是使用两个嵌套的foreach():
foreach ($array as $value) {
foreach($value as $v){
echo $v;
}
}
最后,为了对数组进行排序,您可以使用usort()或cmp()。 不要忘记使用相同的格式作为您的日期(理想情况下为ISO 8601),以便您轻松对其进行排序。
答案 2 :(得分:1)
使用多种排序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>';
}