如何在 PHP 中使用逗号分隔的值对日期进行排序

huangapple go评论67阅读模式
英文:

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 -->

 &lt;?php
$s1=explode(&#39;,&#39;,$val[&#39;ddate&#39;]);
$s2=explode(&#39;,&#39;,$val[&#39;details&#39;]);
$s3=explode(&#39;,&#39;,$val[&#39;type&#39;]);
$s4=explode(&#39;,&#39;,$val[&#39;ref&#39;]);
$s5=explode(&#39;,&#39;,$val[&#39;status&#39;]);
 sort($s1);
$ii=1;
for($i=0;$i&lt;count($s1);$i++){
      ?&gt;
      &lt;tr &gt;
        &lt;th scope=&quot;row&quot;&gt;&lt;?php echo $s1[$i];?&gt;&lt;/th&gt;

        &lt;td&gt;&lt;?php echo $s3[$i];?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php echo $s2[$i];?&gt;&lt;/td&gt;

        &lt;td&gt; &lt;?php echo $s5[$i];?&gt;&lt;/td&gt;

      &lt;/tr&gt;
&lt;?php $ii++; } ?&gt;

<!-- 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:

&lt;?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 =&gt; $val) {
    echo &quot;ar[&quot; . $key . &quot;] = &quot; . $val . &quot;&lt;br&gt;&quot;;
}
echo &#39;&lt;br&gt;&#39;;
foreach ($ar2 as $key =&gt; $val) {
    echo &quot;ar[&quot; . $key . &quot;] = &quot; . $val . &quot;&lt;br&gt;&quot;;
}
echo &#39;&lt;br&gt;&#39;;
foreach ($ar3 as $key =&gt; $val) {
    echo &quot;ar[&quot; . $key . &quot;] = &quot; . $val . &quot;&lt;br&gt;&quot;;
}
?&gt;

huangapple
  • 本文由 发表于 2023年6月8日 13:23:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定