how to sort array according to index of key in php -
this array:
$arr = array("pic0", "pic1", "pic2", "pic3", "pic4");
how can following strings:
$str1 = "pic1,pic2,pic3,pic4,pic0"; $str2 = "pic0,pic2,pic3,pic4,pic1"; $str3 = "pic0,pic1,pic3,pic4,pic2"; $str4 = "pic0,pic1,pic2,pic4,pic3"; $str5 = "pic0,pic1,pic2,pic3,pic4";
i'd use array, not 5 strings:
$arr = array ("pic0", "pic1", "pic2", "pic3", "pic4"); $final = array (); for($i=1, $c = count($arr); $i <= $c; ++$i) // loop n times { $tmp = $arr; // tmp array $x = $arr[$i-1]; // element put in end unset($tmp[$i-1]); // unset tmp array $final[$i] = implode(",", $tmp) . "," . $x; // concatenate array $x in end } print_r($final); /* array ( [1] => pic1,pic2,pic3,pic4,pic0 [2] => pic0,pic2,pic3,pic4,pic1 [3] => pic0,pic1,pic3,pic4,pic2 [4] => pic0,pic1,pic2,pic4,pic3 [5] => pic0,pic1,pic2,pic3,pic4 ) */
Comments
Post a Comment