如何回显数据数组
How to echo data array
我是新手,谁能解决我的问题?
这是我的代码:
<?php
$data = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$datastart = 3;
$string = join(',', $data);
echo $string;
?>
此回显将是结果:
Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
我想要这样的结果:
Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday
谢谢你的回答:)
您可以使用 array_slice
to extract the portions of your array in the order you want to output them, and then array_merge
将它们重新组合在一起:
$data = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$datastart = 3;
$string = implode(',', array_merge(array_slice($data, $datastart), array_slice($data, 0, $datastart)));
echo $string;
或者您可以使用简单的 for
循环:
$len = count($data);
$string = '';
for ($i = 0; $i < $len; $i++) {
$string .= ($i > 0 ? ',' : '') . $data[($i + $datastart) % $len];
}
echo $string;
输出:
Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday
我是新手,谁能解决我的问题? 这是我的代码:
<?php
$data = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$datastart = 3;
$string = join(',', $data);
echo $string;
?>
此回显将是结果:
Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
我想要这样的结果:
Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday
谢谢你的回答:)
您可以使用 array_slice
to extract the portions of your array in the order you want to output them, and then array_merge
将它们重新组合在一起:
$data = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$datastart = 3;
$string = implode(',', array_merge(array_slice($data, $datastart), array_slice($data, 0, $datastart)));
echo $string;
或者您可以使用简单的 for
循环:
$len = count($data);
$string = '';
for ($i = 0; $i < $len; $i++) {
$string .= ($i > 0 ? ',' : '') . $data[($i + $datastart) % $len];
}
echo $string;
输出:
Wednesday,Thursday,Friday,Saturday,Sunday,Monday,Tuesday