Duplicate keys in an array

What is the best way to prevent duplicate key on type?

Example:

//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);

Output

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [1] => Duplicate key 1
    [2] => Duplicate key 2
    [3] => Duplicate key 3
)

Now I know that some smart people will say it, because one keyis string, and the other intusesvar_dump

var_dump($array);

Output

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 problem is that it’s not even possible to get the key

echo $array['1'] ,PHP_EOL;     //Expected Duplicate key 1
echo $array[1] ,PHP_EOL;

Output

2
2

Is there any workaround for this problem without having to quote? Obviously, I would never have made this mistake if @PeeHaa didn’t give a beer again, but I think any answer should help educated developers PHP.

Note . - This can be easily resolved using array_values, sortor any php function that changes the position of the key

Example

sort($array);
print_r($array);

Output

Array
(
    [0] => Duplicate key 1
    [1] => Duplicate key 2
    [2] => Duplicate key 3
    [3] => 1
    [4] => 2
    [5] => 3
)
+5
source share
3

array_values ​​ reset .

:

$obj = (object)array(1,2,3);
$obj->{1} = "Duplicate key 1";
$obj->{2} = "Duplicate key 2";
$obj->{3} = "Duplicate key 3";
$array = (array)$obj ;

$array = array_values($array);
print_r($array);

:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => Duplicate key 1
    [4] => Duplicate key 2
    [5] => Duplicate key 3
)

array_values ​​ , :

$array = array_values((array)$obj);

, !

+1

, , .

// function that convers an array to an object, and prefixes the numeric values with a string
function toObj(Array $arr, $prefix = '_', $convertAll = false ) {
    foreach ($arr as $key => $value) {
        if (is_numeric($key) || $convertAll) {
            unset($arr[$key]);
            $arr[$prefix . $key] = $value;
        }
    }
    return (object)$arr;
}

$obj = toObj(array(1, 2, 3));
$obj->{'_0'} = "Duplicate key 0"; // either this way
$obj->_1 = "Duplicate key 1"; // or this way
$obj->{'_2'} = "Duplicate key 2";
$array = (array)$obj;
print_r($array);

:

Array
(
    [_0] => Duplicate key 0
    [_1] => Duplicate key 1
    [_2] => Duplicate key 2
)

array_values, - , , .

+1

, (.. , ) . :

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;
    }
}

Due to the use __set()and definition of all as a dynamic public properties it will continue to go well with json_encode(), foreach, casting, and other things that you would expect from stdClass. However, this forces a string type for property names, which means that overlapping free types are no longer possible.

You can also define a helper function that allows you to make the "casting" syntax perfectly understand what is 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);
}

See what happens:

$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);

Output :

Array
(
    [0] => 1
    [1] => Duplicate key 1
    [2] => Duplicate key 2
    [3] => Duplicate key 3
)
+1
source

All Articles