如果将数组定义为键值对,如何检查?

时间:2021-09-17 20:39:07

I have a function, that takes an array as an argument.

我有一个函数,它将一个数组作为参数。

function addSearch($arr) { }

How can I check in that function whether the array was defined just with values

如何检查该函数是否仅使用值定义数组

array('option1', 'option2', ...);

or as key-value pairs:

或作为键值对:

array('option1' => 'First Option', 'option2' => 'Second Option', ...)

What I like to achive, is that you could either pass the search fileds along with labels, or just the search fields, in which case the field name will also become the label.

我喜欢的是,您既可以将搜索文件与标签一起传递,也可以只传递搜索字段,在这种情况下,字段名称也将成为标签。

So I need to change an array, that has just values to $array['option1'] = 'option1';

所以我需要更改一个数组,其值只有$ array ['option1'] ='option1';

Any ideas how to achive this?

任何想法如何实现这一点?

4 个解决方案

#1


5  

You cannot know how an array was defined. Both kinds of arrays are the same thing, they both have keys and values. The difference is that the "keyless array" uses auto-generated numeric keys, while in the other example keys are explicitly given as strings. These two arrays are identical:

您无法知道数组是如何定义的。两种数组都是相同的,它们都有键和值。不同之处在于“无键数组”使用自动生成的数字键,而在另一个示例中,键显式为字符串。这两个数组是相同的:

array('option1', 'option2')
array(0 => 'option1', 1 => 'option2')

You can even mix both:

你甚至可以混合两种:

array('foo', 'bar' => 'baz', 42 => 'qux')

Therefore, what you're really interested in is whether the key is a string:

因此,您真正感兴趣的是密钥是否为字符串:

foreach ($options as $key => $value) {
    if (is_string($key)) {
        // string => value pair
    } else {
        // numerically indexed pair
        // note: could still have been explicitly defined, who knows?
    }
}

#2


2  

this will check if your array is associative

这将检查您的数组是否是关联的

function is_associative($array) {
    return array_values($array) !== $array;
}

#3


1  

Another solution

另一解决方案

<?php
function isAssociative($arr)
{
    return array_keys($arr) !== range(0, count($arr) - 1);
}
?>

#4


1  

I have to go with @phranx on this one.

我必须和@phranx一起去这个。

Let's say you have this array('1'=>'Under', '2'=>'Over')

假设你有这个数组('1'=>'Under','2'=>'Over')

The keys in this array will be evaluated as integers.

此数组中的键将被计算为整数。

The best way is to determine if the keys are key:values pairs is to check if the values of the array are equal to the array itself.

最好的方法是确定键是否是键:值对是检查数组的值是否等于数组本身。

#1


5  

You cannot know how an array was defined. Both kinds of arrays are the same thing, they both have keys and values. The difference is that the "keyless array" uses auto-generated numeric keys, while in the other example keys are explicitly given as strings. These two arrays are identical:

您无法知道数组是如何定义的。两种数组都是相同的,它们都有键和值。不同之处在于“无键数组”使用自动生成的数字键,而在另一个示例中,键显式为字符串。这两个数组是相同的:

array('option1', 'option2')
array(0 => 'option1', 1 => 'option2')

You can even mix both:

你甚至可以混合两种:

array('foo', 'bar' => 'baz', 42 => 'qux')

Therefore, what you're really interested in is whether the key is a string:

因此,您真正感兴趣的是密钥是否为字符串:

foreach ($options as $key => $value) {
    if (is_string($key)) {
        // string => value pair
    } else {
        // numerically indexed pair
        // note: could still have been explicitly defined, who knows?
    }
}

#2


2  

this will check if your array is associative

这将检查您的数组是否是关联的

function is_associative($array) {
    return array_values($array) !== $array;
}

#3


1  

Another solution

另一解决方案

<?php
function isAssociative($arr)
{
    return array_keys($arr) !== range(0, count($arr) - 1);
}
?>

#4


1  

I have to go with @phranx on this one.

我必须和@phranx一起去这个。

Let's say you have this array('1'=>'Under', '2'=>'Over')

假设你有这个数组('1'=>'Under','2'=>'Over')

The keys in this array will be evaluated as integers.

此数组中的键将被计算为整数。

The best way is to determine if the keys are key:values pairs is to check if the values of the array are equal to the array itself.

最好的方法是确定键是否是键:值对是检查数组的值是否等于数组本身。