Php 如何分离数字数组';字母钥匙中的钥匙是什么?
这是我的密码:Php 如何分离数字数组';字母钥匙中的钥匙是什么?,php,arrays,html,Php,Arrays,Html,这是我的密码: $arr = array(); $arr[] = 1; $arr['txt'] = 'something'; $arr['txt2'] = 'something2'; $arr[] = 2; $arr[] = 3; echo '<pre>'; print_r($arr); /* output: 1 something something2 2 3 */ 如您所见,我需要对所有数组项重新编制索引,并将数字项放在数组的开头。可能吗 如何
$arr = array();
$arr[] = 1;
$arr['txt'] = 'something';
$arr['txt2'] = 'something2';
$arr[] = 2;
$arr[] = 3;
echo '<pre>';
print_r($arr);
/* output:
1
something
something2
2
3
*/
如您所见,我需要对所有数组项重新编制索引,并将数字项放在数组的开头。可能吗
如何将数字数组的键与字母键分开
最简单的方法是使用修改数组的ksort
按键对数组进行排序。使用SORT_STRING
标志获取搜索结果:
<?php
$arr = array();
$arr[] = 1;
$arr['txt'] = 'something';
$arr['txt2'] = 'something2';
$arr[] = 2;
$arr[] = 3;
echo '<pre>';
usort($arr, function($a, $b) {
if (is_float($a)) {
if ( is_float($b)) {
return $a - $b;
}
else
return -1;
}
elseif (is_float($b)) {
return 1;
}
else {
return strcmp($a, $b);
}
});
print_r($arr);
?>
如何将数字数组的键与字母键分开
最简单的方法是使用修改数组的ksort
按键对数组进行排序。使用SORT_STRING
标志获取搜索结果:
<?php
$arr = array();
$arr[] = 1;
$arr['txt'] = 'something';
$arr['txt2'] = 'something2';
$arr[] = 2;
$arr[] = 3;
echo '<pre>';
usort($arr, function($a, $b) {
if (is_float($a)) {
if ( is_float($b)) {
return $a - $b;
}
else
return -1;
}
elseif (is_float($b)) {
return 1;
}
else {
return strcmp($a, $b);
}
});
print_r($arr);
?>
正确的语法是: -使用数组值 -使用数组索引值
正确的语法是: -使用数组值 -使用数组索引值
<?php
$arr = array();
$arr[] = 1;
$arr['txt'] = 'something';
$arr['txt2'] = 'something2';
$arr[] = 2;
$arr[] = 3;
echo '<pre>';
usort($arr, function($a, $b) {
if (is_float($a)) {
if ( is_float($b)) {
return $a - $b;
}
else
return -1;
}
elseif (is_float($b)) {
return 1;
}
else {
return strcmp($a, $b);
}
});
print_r($arr);
?>
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => something
[4] => something2
)
<?php
$arr = array();
$arr[] = 1;
$arr['txt'] = 'something';
$arr['txt2'] = 'something2';
$arr[] = 2;
$arr[] = 3;
echo '<pre>';
usort($arr, SORT_STRING);
print_r($arr);
?>
Array
(
[0] => 1
[1] => 2
[2] => 3
[txt] => something
[txt2] => something2
)