阵列中的重复键 [英] Duplicate keys in Array
问题描述
在类型转换期间防止重复键的最佳方法是什么?
What is the best way to prevent duplicate key during type casting ?
示例:
//Credits @bwoebi
$obj = (object)array(1,2,3);
$obj->{1} = "Duplicate key 1";
$obj->{2} = "Duplicate key 2";
$obj->{3} = "Duplicate key 3";
$array = (array)$obj ;
print_r($array);
输出
Array
(
[0] => 1
[1] => 2
[2] => 3
[1] => Duplicate key 1
[2] => Duplicate key 2
[3] => Duplicate key 3
)
现在我知道一些聪明的人会说它,因为一个key
是string
而另一个int
使用var_dump
Now i know some smart people would say its because one key
is string
and the other int
use var_dump
var_dump($array);
输出
array (size=6)
0 => int 1
1 => int 2
2 => int 3
'1' => string 'Duplicate key 1' (length=15)
'2' => string 'Duplicate key 2' (length=15)
'3' => string 'Duplicate key 3' (length=15)
但是主要问题是甚至没有办法获得密钥
But the main issue is there is no way to even get the key
echo $array['1'] ,PHP_EOL; //Expected Duplicate key 1
echo $array[1] ,PHP_EOL;
输出
2
2
有没有解决此问题的方法而不必循环?显然,除非@PeeHaa埽再给我啤酒,否则我永远不会犯这个错误,但我认为任何答案都应有助于受过教育的PHP
开发人员.
Is there any workaround to this issue without having to loop ? Obviously i would never make this mistake unless @PeeHaa埽 gives be beer again, but I think any answer should help educated PHP
developers.
注意.
-可以使用array_values
,sort
或任何更改键位置的php函数轻松解决此问题
Note.
- This can easly be resolved with array_values
, sort
or any php function that changes key position
示例
sort($array);
print_r($array);
输出
Array
(
[0] => Duplicate key 1
[1] => Duplicate key 2
[2] => Duplicate key 3
[3] => 1
[4] => 2
[5] => 3
)
推荐答案
我认为, only 解决方案(即唯一不是丑陋的东西)就是停止使用匿名对象并为此定义一个类.不必太复杂:
The way I see this, the only solution (i.e. the only thing that isn't an ugly hack) is to stop using anonymous objects and define a class for this purpose. It doesn't need to be complicated:
class SimpleArrayObject
{
public function __construct($array = null)
{
foreach ((array) $array as $key => $value) {
$this->{$key} = $value; // implicitly calls __set()
}
}
public function __set($key, $value)
{
$this->{(string) $key} = $value;
}
}
由于使用__set()
并将所有内容都定义为动态公共属性,因此对于json_encode()
,foreach
,转换以及您希望从stdClass
进行的所有其他操作,它仍然可以很好地发挥作用.但是,这会强制使用属性名称的字符串类型,这意味着不再有重叠的松散类型.
Because of the use of __set()
and defining everything as dynamic public properties, this will still play nice with json_encode()
, foreach
, casting and all the other things you would expect from a stdClass
. However this enforces a string type for property names, meaning that the overlapping loose types is no longer possible.
您还可以定义一个辅助函数,以使广播"语法非常清楚:
You can also define a helper function to allow you to make the "casting" syntax quite clear what's going on:
function object($value)
{
if (is_object($value)) {
return $value;
}
if (!is_array($value)) { // mimic the behaviour of a regular cast
$value = array('scalar' => $value);
}
return new SimpleArrayObject($value);
}
看看会发生什么:
$obj = object(array(1,2,3));
$obj->{1} = "Duplicate key 1";
$obj->{2} = "Duplicate key 2";
$obj->{3} = "Duplicate key 3";
$array = (array)$obj ;
print_r($array);
输出:
Array
(
[0] => 1
[1] => Duplicate key 1
[2] => Duplicate key 2
[3] => Duplicate key 3
)
这篇关于阵列中的重复键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!