Php 用作搜索的数组_列函数的替代方法
搜索所有导航项目:Php 用作搜索的数组_列函数的替代方法,php,arrays,Php,Arrays,搜索所有导航项目: $navitems_locations_array = array_unique(array_column($projects, 'location')); asort($navitems_locations_array); 这适用于我的本地主机,但不适用于live。我的服务器不支持它。我使用的是PHP的较低版本 这就是错误: 致命错误:调用未定义的函数数组_列() 有没有输出相同的替代方案 以下是我的示例arrray列表: Array([name]=>P1[descrip
$navitems_locations_array = array_unique(array_column($projects, 'location'));
asort($navitems_locations_array);
这适用于我的本地主机,但不适用于live。我的服务器不支持它。我使用的是PHP的较低版本
这就是错误:
致命错误:调用未定义的函数数组_列()
有没有输出相同的替代方案
以下是我的示例arrray列表:
Array([name]=>P1[description]=>Lorem Ipsum[location]=>air city[type]=>high-rise[status]=>new[tags]=>[page\u url]=>project-1[image]=>projects/images/P1/project-image.jpg[timeline]=>)
尝试一下,希望它能帮到你。在这里,我们使用array\u map
作为收集位置
$locations=array();
array_map(function($value) use (&$locations) {
$locations[]=$value["location"];//we are gathering index location
}, $yourArray);//add your array here in place of $yourArray.
print_r(array_unique($locations));
试试这个,希望它能帮助你。在这里,我们使用
array\u map
作为收集位置
$locations=array();
array_map(function($value) use (&$locations) {
$locations[]=$value["location"];//we are gathering index location
}, $yourArray);//add your array here in place of $yourArray.
print_r(array_unique($locations));
如果您的PHP版本不支持,请添加您自己的函数
array\u column
:
if (!function_exists('array_column')) {
function array_column($input = null, $columnKey = null, $indexKey = null)
{
$argc = func_num_args();
$params = func_get_args();
if ($argc < 2) {
trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
return null;
}
if (!is_array($params[0])) {
trigger_error(
'array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given',
E_USER_WARNING
);
return null;
}
if (!is_int($params[1])
&& !is_float($params[1])
&& !is_string($params[1])
&& $params[1] !== null
&& !(is_object($params[1]) && method_exists($params[1], '__toString'))
) {
trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
return false;
}
if (isset($params[2])
&& !is_int($params[2])
&& !is_float($params[2])
&& !is_string($params[2])
&& !(is_object($params[2]) && method_exists($params[2], '__toString'))
) {
trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
return false;
}
$paramsInput = $params[0];
$paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
$paramsIndexKey = null;
if (isset($params[2])) {
if (is_float($params[2]) || is_int($params[2])) {
$paramsIndexKey = (int) $params[2];
} else {
$paramsIndexKey = (string) $params[2];
}
}
$resultArray = array();
foreach ($paramsInput as $row) {
$key = $value = null;
$keySet = $valueSet = false;
if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
$keySet = true;
$key = (string) $row[$paramsIndexKey];
}
if ($paramsColumnKey === null) {
$valueSet = true;
$value = $row;
} elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
$valueSet = true;
$value = $row[$paramsColumnKey];
}
if ($valueSet) {
if ($keySet) {
$resultArray[$key] = $value;
} else {
$resultArray[] = $value;
}
}
}
return $resultArray;
}
}
如果(!function_存在('array_column')){
函数数组\列($input=null,$columnKey=null,$indexKey=null)
{
$argc=func_num_args();
$params=func_get_args();
如果($argc<2){
trigger_error(“array_column()需要至少2个参数,{$argc}给定”,E_USER_警告);
返回null;
}
如果(!is_数组($params[0])){
触发错误(
“array_column()要求参数1为数组,”.gettype($params[0])。“给定”,
用户警告
);
返回null;
}
如果(!is_int($params[1])
&&!is_float($params[1])
&&!是字符串($params[1])
&&$params[1]!==null
&&!(is_对象($params[1])和&method_存在($params[1],“\u toString”))
) {
trigger_error('array_column():列键应为字符串或整数,E_USER_警告);
返回false;
}
如果(isset($params[2])
&&!is_int($params[2])
&&!is_float($params[2])
&&!是字符串($params[2])
&&!(is_对象($params[2])和&method_存在($params[2],“\uu-toString”))
) {
trigger_error('array_column():索引键应为字符串或整数',E_USER_警告);
返回false;
}
$paramsInput=$params[0];
$paramsColumnKey=($params[1]!==null)?(字符串)$params[1]:null;
$paramsIndexKey=null;
if(isset($params[2])){
if(is_float($params[2])| is_int($params[2])){
$paramsIndexKey=(int)$params[2];
}否则{
$paramsIndexKey=(字符串)$params[2];
}
}
$resultArray=array();
foreach($paramsInput作为$row){
$key=$value=null;
$keySet=$valueSet=false;
如果($paramsIndexKey!==null&&array_key_存在($paramsIndexKey,$row)){
$keySet=true;
$key=(字符串)$row[$paramsIndexKey];
}
如果($paramsColumnKey==null){
$valueSet=true;
$value=$row;
}elseif(is_数组($row)和&array_key_存在($paramsColumnKey,$row)){
$valueSet=true;
$value=$row[$paramsColumnKey];
}
如果($valueSet){
如果($keySet){
$resultArray[$key]=$value;
}否则{
$resultArray[]=$value;
}
}
}
返回$resultArray;
}
}
如果您的PHP版本不支持,请添加您自己的函数
array\u column
:
if (!function_exists('array_column')) {
function array_column($input = null, $columnKey = null, $indexKey = null)
{
$argc = func_num_args();
$params = func_get_args();
if ($argc < 2) {
trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
return null;
}
if (!is_array($params[0])) {
trigger_error(
'array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given',
E_USER_WARNING
);
return null;
}
if (!is_int($params[1])
&& !is_float($params[1])
&& !is_string($params[1])
&& $params[1] !== null
&& !(is_object($params[1]) && method_exists($params[1], '__toString'))
) {
trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
return false;
}
if (isset($params[2])
&& !is_int($params[2])
&& !is_float($params[2])
&& !is_string($params[2])
&& !(is_object($params[2]) && method_exists($params[2], '__toString'))
) {
trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
return false;
}
$paramsInput = $params[0];
$paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
$paramsIndexKey = null;
if (isset($params[2])) {
if (is_float($params[2]) || is_int($params[2])) {
$paramsIndexKey = (int) $params[2];
} else {
$paramsIndexKey = (string) $params[2];
}
}
$resultArray = array();
foreach ($paramsInput as $row) {
$key = $value = null;
$keySet = $valueSet = false;
if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
$keySet = true;
$key = (string) $row[$paramsIndexKey];
}
if ($paramsColumnKey === null) {
$valueSet = true;
$value = $row;
} elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
$valueSet = true;
$value = $row[$paramsColumnKey];
}
if ($valueSet) {
if ($keySet) {
$resultArray[$key] = $value;
} else {
$resultArray[] = $value;
}
}
}
return $resultArray;
}
}
如果(!function_存在('array_column')){
函数数组\列($input=null,$columnKey=null,$indexKey=null)
{
$argc=func_num_args();
$params=func_get_args();
如果($argc<2){
trigger_error(“array_column()需要至少2个参数,{$argc}给定”,E_USER_警告);
返回null;
}
如果(!is_数组($params[0])){
触发错误(
“array_column()要求参数1为数组,”.gettype($params[0])。“给定”,
用户警告
);
返回null;
}
如果(!is_int($params[1])
&&!is_float($params[1])
&&!是字符串($params[1])
&&$params[1]!==null
&&!(is_对象($params[1])和&method_存在($params[1],“\u toString”))
) {
trigger_error('array_column():列键应为字符串或整数,E_USER_警告);
返回false;
}
如果(isset($params[2])
&&!is_int($params[2])
&&!is_float($params[2])
&&!是字符串($params[2])
&&!(is_对象($params[2])和&method_存在($params[2],“\uu-toString”))
) {
trigger_error('array_column():索引键应为字符串或整数',E_USER_警告);
返回false;
}
$paramsInput=$params[0];
$paramsColumnKey=($params[1]!==null)?(字符串)$params[1]:null;
$paramsIndexKey=null;
if(isset($params[2])){
if(is_float($params[2])| is_int($params[2])){
$paramsIndexKey=(int)$params[2];
}否则{
$paramsIndexKey=(字符串)$params[2];
}
}
$resultArray=数组();
foreach($paramsInput作为$row){
$key=$value=null;
$keySet=$valueSet=false;
如果($paramsIndexKey!==null&&array_key_存在($paramsIndexKey,$row)){
$keySet=true;
$key=(字符串)$row[$paramsIndexKey];
}
如果($paramsColumnKey==null){
$valueSet=true;
$value=$row;
}elseif(is_数组($row)和&array_key_存在($paramsColumnKey,$row)){
$valueSet=true;
$value=$row[$paramsColumnKey];
}
如果($valueSet){
如果($keySet){
$resultArray[$key]=$value;
}否则{
$resultArray[]=$value;
}
}
}
返回$resultArray;
}
}
在上阅读用户提供的注释通常很有用 例如,在本文下面,有几个ve实现的变体