[英]Sorting an array in a foreach loop in php
I am looking at how to sort an array within a foreach
loop to place my featured jobs to the top of the list. 我正在研究如何在
foreach
循环中对数组进行排序,以将我的特色作业放在列表的顶部。
The key and values are 'Featured Job' => 'Yes'
or 'Featured Job' => 'No'
键和值是
'Featured Job' => 'Yes'
或'Featured Job' => 'No'
I am just checking to see if the key isset
and then outputting a string saying 'Featured Job' - that part was easy. 我只是检查,看看是否的关键
isset
然后输出字符串说“精选工作” -这部分是容易的。 I am just unable to sort them to place the featured jobs at the top. 我只是无法对它们进行排序以将特色工作放在顶部。
Here is my code, any hints or help would be appreciated: 这是我的代码,任何提示或帮助将不胜感激:
if( isset($v->customLovs) && !empty($v->customLovs->customLov) ) {
$lovs = $v->customLovs->customLov;
if( isset($lovs->label) )
$lovs = array( $lovs );
foreach( $lovs as $lov ) {
switch( $lov->label ) {
case "Position":
$position = $lov->criteria->criterion->label;
break;
case "Town or City":
$townOrCity = $lov->criteria->criterion->label;
break;
case "Featured Job":
$fjob = $lov->criteria->criterion->label;
break;
}
}
}
And here is the output: 这是输出:
<h2><?php echo $position;?></h2>
<?php if ( isset($fjob) ) : ?>Featured Job<?php endif; ?>
<?php echo $townOrCity;?>
My feeble attempt to cure this was like so: 我微弱的尝试来解决这个问题,就像这样:
$lovs = usort($lovs);
I can only assume that I need to target the $fjob
somehow? 我只能假设我需要以某种方式定位
$fjob
?
You can look up php documentation on sorting arrays. 您可以查找有关排序数组的php文档。 you can create a date key and sort by the date.
您可以创建日期键并按日期排序。 http://php.net/manual/en/array.sorting.php
http://php.net/manual/en/array.sorting.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.