我试图在php中使用字符串数组,其图像数组和洗牌数组多次,我需要从巨大的数组中获得前13个值,并将其放置在另一个数组中,然后对第二个字符串数组进行排序。但我得到两个错误:
Notice: Array to string conversion in /Applications/XAMPP/xamppfiles/htdocs/index.php on line 46
Warning: ksort() expects parameter 1 to be array, string given in /Applications/XAMPP/xamppfiles/htdocs//index.php on line 50
Array30.png41.png24.png31.png25.png44.png2.png15.png14.png50.png36.png38.png32.png
下面是我的代码:
$images = array("1.png","2.png","3.png","4.png","5.png","6.png","7.png","8.png",
"9.png","10.png","11.png","12.png","13.png","14.png","15.png","16.png","17.png","18.png",
"19.png","20.png","21.png","22.png","23.png","24.png","25.png","26.png","27.png","28.png",
"29.png","30.png","31.png","32.png","33.png","34.png","35.png","36.png","37.png","38.png",
"39.png","40.png","41.png","42.png","43.png","44.png","45.png","46.png","47.png","48.png",
"49.png","50.png","51.png","52.png"
);
shuffle($images);
shuffle($images);
shuffle($images);
shuffle($images);
shuffle($images);
$playerArraySorted = array();
for ($i = 0; $i < 13; $i++) {
$playerArraySorted .= $images[$i];
}
ksort($playerArraySorted,2);
echo "$playerArraySorted";
为了在php中推送到数组,使用括号:
$playerArraySorted[] = $images[$i];
使用.=
可以连接字符串。
而不是echo
来检查数组,使用var_dump
:
var_dump($playerArraySorted);
似乎你的数组没有键,所以ksort
不会工作,使用sort
或usort
与自定义排序函数代替。