У вас много синтаксических ошибок в коде.
Чтобы отсортировать массив с пользовательским сортированием, вы можете использовать это:
$array = array(1,3,5,6,7,8,11,12,17,11);
$arrayCount = count($array); // no need to evaluate the count on every iteration of the for loop
for($i=0; $i < $arrayCount - 1; $i++)
{
for($j = $i+1; $j < $arrayCount; $j++)
{
// if you want the array sorted from bigger to smaller number use `>` here
if($array[$j] < $array[$i])
{
$temp = $array[$i];
$array[$i] = $array[$j];
$array[$j] = $temp;
}
}
}
print_r($array);
, который выводит:
Array
(
[0] => 1
[1] => 3
[2] => 5
[3] => 6
[4] => 7
[5] => 8
[6] => 11
[7] => 11
[8] => 12
[9] => 17
)
Теперь, чтобы получить желаемое значение из массива или false
, если оно не существует, вы должны написать что-то вроде этого, используя встроенный метод array_search()
:
// Check if array contains a value 12, if it does return the index location in the array
// returns false if the value is not found
$index = array_search(12, $array);
if($index === false)
{
echo 'Value does not exist in the array.';
}
else
{
echo 'Value '.$array[$index].' is at index '.$index.' in the array.';
}