|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 05:00:01 2025 UTC |
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)));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.