Skip to content

Funny business with zend_array #453

@kakserpom

Description

@kakserpom
#[php_function]
pub fn make_a_peculiar_array() -> Result<Zval> {
    let mut array = zend_array::with_capacity(5);
    array.insert_at_index(1, false)?;
    array.insert("1", true)?;
    array.into_zval(false)
}
<?php
$array = make_a_peculiar_array();
var_dump([
    '$array' => $array,
    "'1' exists" => array_key_exists("1", $array),
    "'1' value" => $array["1"],
    'count($array)' => count($array)
]);
unset($array["1"]);
var_dump([
    "array_key_exists('1', \$array)" => array_key_exists('1', $array),
    "isset(\$array['1'])" => isset($array['1']),
    'count($array)' => count($array),
    '$array' => $array,
]);

Output:

array(4) {
  ["$array"]=>
  array(2) {
    [1]=>
    bool(false)
    ["1"]=>
    bool(true)
  }
  ["'1' exists"]=>
  bool(true)
  ["'1' value"]=>
  bool(false)
  ["count($array)"]=>
  int(2)
}
array(4) {
  ["array_key_exists('1', $array)"]=>
  bool(false)
  ["isset($array['1'])"]=>
  bool(false)
  ["count($array)"]=>
  int(1)
  ["$array"]=>
  array(1) {
    ["1"]=>
    bool(true)
  }
}

ZendHashTable::insert() probably should insert numeric keys as indexes.

Also note that insert_at_index() accepts u64 and thus doesn't support negative indexes.

> var_dump([-1 => 1]);
array(1) {
  [-1]=>
  int(1)
}

— negative array indexes are a thing in PHP.

Sub-issues

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions