Nikhil Malik
Nikhil Malik

Reputation: 478

After converting Object to Array key showing undefined offset

I have result array and each array index represents combination of various objects (result is from doctrine query).
I want to combine these value to same indexes like you can see in example => Array [0] has 2 object values '0' and name so i want to combine these results to new array in 0 index and so on. So I can perform further processing on new result.

array (size=4)
      0 => 
        object(stdClass)[568]
          public '0' => 
            object(stdClass)[552]
              public 'id' => int 16
              public 'userId' => int 250
              public 'content' => string '<script>alert('Alert');</script>  
    adad adad adad
    ### Heading' (length=61)
          public 'name' => string 'biling' (length=13)
      1 => 
        object(stdClass)[556]
          public '0' => 
            object(stdClass)[554]
              public 'id' => int 15
              public 'userId' => int 250
              public 'content' => string '<script>alert('Alert');</script>  
    adad
    ### Heading' (length=51)
          public 'name' => string 'biling' (length=13)

I tried some code but real problem, I am facing is that it's not removing or unset-ing 0 index and showing offset '0' is undefined

code

    $results = 'Object Result which i shown';        
    $data = array();
    foreach ($results as $key => $item) {

        $resultsCopyArray = array_diff_key((array)$item, [0]);
        //this function must escape 0 index from $item and just add name to $resultsCopyArray but it's now working like it should be

        var_dump(array_keys($resultsCopyArray)); 
        // -> showing 0, name

        // var_dump($resultsCopyArray[0]); -> showing error

        // I also tried to unset this value but noting 

        $data[] = (array)$item->{'0'} + $resultsCopyArray;
    }
    var_dump($data);

Upvotes: 1

Views: 299

Answers (1)

ksimka
ksimka

Reputation: 1455

It's one of those "PHP array traps".

array_diff_key uses strict comparison on keys ($k1 === $k2). PHP arrays that was directly constructed (simply saying) use optimisation: numeric strings are converted to numbers. So strict comparison fails: "0" !== 0.

But weak comparison will also fail. You can see it here.

<?php

function key_compare_func($key1, $key2)
{
    var_dump($key1, $key2, $key1 == $key2, $key1 > $key2);
    if ($key1 == $key2)
        return 0;
    else if ($key1 > $key2)
        return 1;
    else
        return -1;
}

$a=new stdClass;
$a->{"0"}=1;
$a->n=2;
echo "key\n";
var_dump(array_diff_key((array)$a, [0]));
echo "ukey\n";
var_dump(array_diff_ukey((array)$a, [0], 'key_compare_func'));

output

key
array(2) {
  ["0"]=>
  int(1)
  ["n"]=>
  int(2)
}
ukey
string(1) "0"
int(0)
bool(true)
bool(false)
string(1) "n"
int(0)
bool(true)
bool(false)
array(0) {
}

If you are sure you have only string keys, use that kind of code

<?php

function key_compare_func($key1, $key2)
{
    if ((string)$key1 == (string)$key2)
        return 0;
    else if ((string)$key1 > (string)$key2)
        return 1;
    else
        return -1;
}

$a=new stdClass;
$a->{"0"}=1;
$a->n=2;
var_dump(array_diff_ukey((array)$a, [0], 'key_compare_func'));

output

array(1) {
  ["n"]=>
  int(2)
}

Upvotes: 1

Related Questions