英文:
how to sort using dates in comma separated value php
问题
I have a list of comma-separated values, I am trying to display them according to the dates in ascending order. I did the following code:
<?php
$s1=explode(',', $val['ddate']);
$s2=explode(',', $val['details']);
$s3=explode(',', $val['type']);
$s4=explode(',', $val['ref']);
$s5=explode(',', $val['status']);
sort($s1);
$ii=1;
for($i=0; $i<count($s1); $i++){
?>
<tr>
<th scope="row"><?php echo $s1[$i]; ?></th>
<td><?php echo $s3[$i]; ?></td>
<td><?php echo $s2[$i]; ?></td>
<td><?php echo $s5[$i]; ?></td>
</tr>
<?php
$ii++; } ?>
Here, when I use the sort function, only the dates are getting sorted correctly. Other fields are coming out wrong, meaning the respective values related to that date are not coming. Can anyone please tell me how to sort this correctly? Thanks in advance.
英文:
i have a list of comma separated values, i am trying to display them according to the dates in ascending order, i did the following code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<?php
$s1=explode(',',$val['ddate']);
$s2=explode(',',$val['details']);
$s3=explode(',',$val['type']);
$s4=explode(',',$val['ref']);
$s5=explode(',',$val['status']);
sort($s1);
$ii=1;
for($i=0;$i<count($s1);$i++){
?>
<tr >
<th scope="row"><?php echo $s1[$i];?></th>
<td><?php echo $s3[$i];?></td>
<td><?php echo $s2[$i];?></td>
<td> <?php echo $s5[$i];?></td>
</tr>
<?php $ii++; } ?>
<!-- end snippet -->
here, when i use that sort function, only the dates are getting sorted correctly, other fields are comming wrong, i mean the respective values related to that date is not coming, can anyone please tell me how to sort this correctly, thanks in advance
答案1
得分: 0
在Php中,您可以使用**array_multisort**函数。
**array_multisort()**函数返回一个排序后的数组。您可以分配一个或多个数组。该函数对第一个数组进行排序,其他数组随后跟随排序,然后,如果两个或更多值相同,则对下一个数组进行排序,依此类推。
以下是实现**array_multisort()**函数的示例:
<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
$ar3 = array(5, 7, 1, 8);
array_multisort($ar1, $ar2, $ar3);
foreach ($ar1 as $key => $val) {
echo "ar[" . $key . "] = " . $val . "<br>";
}
echo '<br>';
foreach ($ar2 as $key => $val) {
echo "ar[" . $key . "] = " . $val . "<br>";
}
echo '<br>';
foreach ($ar3 as $key => $val) {
echo "ar[" . $key . "] = " . $val . "<br>";
}
?>
英文:
In Php you can use array_multisort function.
The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.
Following is an example to implement array_multisort() function:
<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
$ar3 = array(5, 7, 1, 8);
array_multisort($ar1, $ar2, $ar3);
foreach ($ar1 as $key => $val) {
echo "ar[" . $key . "] = " . $val . "<br>";
}
echo '<br>';
foreach ($ar2 as $key => $val) {
echo "ar[" . $key . "] = " . $val . "<br>";
}
echo '<br>';
foreach ($ar3 as $key => $val) {
echo "ar[" . $key . "] = " . $val . "<br>";
}
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论