php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #59135 Difference hash <-> list
Submitted: 2010-03-29 03:41 UTC Modified: 2010-09-23 22:31 UTC
From: Hoffmann dot P at gmx dot net Assigned: bd808 (profile)
Status: Wont fix Package: yaml (PECL)
PHP Version: 0.6.3 OS: Linux Ubuntu 9.10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: Hoffmann dot P at gmx dot net
New email:
PHP Version: OS:

 

 [2010-03-29 03:41 UTC] Hoffmann dot P at gmx dot net
Description:
------------
I see a problem which is due too the loosely implementation of php's array which is somehow a hybrid between a list/c-array and a hash/map/dictionary.

The following example will show the basic problem I'm heading when trying to map two different objects:

Reproduce code:
---------------
Two different constructs/objects map to the same php-object:

---
 - Zero
 - One
 - Two
 - Three
...

# and

---
  0: Zero
  1: One
  2: Two
  3: Three
...

# OR
---
[Zero, One, Two, Three]
...
# and
---
[0: Zero, 1:One, 2:Two, 3:Three]
...

<?php

$List = "---
 - Zero
 - One
 - Two
 - Three
...";
$Hash = "---
  0: Zero
  1: One
  2: Two
  3: Three
...";

echo("List: ");
print_r(yaml_parse($List));

echo("Hash: ");
print_r(yaml_parse($Hash));

echo("Are they equal? " . ((yaml_parse($List) === yaml_parse($Hash)) ? "yes" : "no") . "\n");

Expected result:
----------------
I expect a way to be able to separate both objects in some way. 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-29 03:46 UTC] Hoffmann dot P at gmx dot net
The other way round is more duck-taping (if it sound like a duck...) but may still lead to problems with yaml being the communication language to applications written in other languages like python,perl,ruby,...

And even as I know the php way of handling arrays with integer-as-string keys I personally find the following lines (or their results) disturbing:

---
echo("List: ");
print_r(yaml_emit(array(Zero, One, Two, Three)));

echo("Hash: ");
print_r(yaml_emit(array("0" => Zero, "1" => One, "2" => Two, "3" => Three)));
 [2010-03-29 18:34 UTC] bd808@php.net
I think I understand your issue, but I'm not exactly sure how to "fix" it in the php yaml bindings.

This behavior is inherent to php's internal typing engine. 

This phpt test passes:
--TEST--
PHP vector & dictionary equality
--SKIPIF--
<?php if(!extension_loaded('yaml')) die('skip yaml n/a'); ?>
--FILE--
<?php
$list = array("Zero", "One", "Two", "Three");
$hash = array("0" => "Zero", "1" => "One", "2" => "Two", "3" => "Three");
var_dump($list == $hash);
?>
--EXPECT--
bool(true)

There's really no way to differentiate between the two structures without changing the the yaml extension to produce and consume some sort of intermediate objects (ala SimpleXmlElement) that can carry more metadata. Personally I think that type of change would cause far more harm than good.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 04:01:31 2024 UTC