Exemples

Exemple #1 Exemples Yaml

  1. <?php
  2. $addr = array(
  3. "given" => "Chris",
  4. "family"=> "Dumars",
  5. "address"=> array(
  6. "lines"=> "458 Walkman Dr.
  7. Suite #292",
  8. "city"=> "Royal Oak",
  9. "state"=> "MI",
  10. "postal"=> 48046,
  11. ),
  12. );
  13. $invoice = array (
  14. "invoice"=> 34843,
  15. "date"=> "2001-01-23",
  16. "bill-to"=> $addr,
  17. "ship-to"=> $addr,
  18. "product"=> array(
  19. array(
  20. "sku"=> "BL394D",
  21. "quantity"=> 4,
  22. "description"=> "Basketball",
  23. "price"=> 450,
  24. ),
  25. array(
  26. "sku"=> "BL4438H",
  27. "quantity"=> 1,
  28. "description"=> "Super Hoop",
  29. "price"=> 2392,
  30. ),
  31. ),
  32. "tax"=> 251.42,
  33. "total"=> 4443.52,
  34. "comments"=> "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.",
  35. );
  36.  
  37. // genère un représentation YAML de invoice
  38. $yaml = yaml_emit($invoice);
  39. var_dump($yaml);
  40.  
  41. // convertit la syntaxe YAML vers une variable PHP
  42. $parsed = yaml_parse($yaml);
  43.  
  44. // vérifie que la conversion a produit une structure équivalente
  45. var_dump($parsed == $invoice);
  46. ?>

L'exemple ci-dessus va afficher quelque chose de similaire à :

string(631) "---
invoice: 34843
date: "2001-01-23"
bill-to:
  given: Chris
  family: Dumars
  address:
    lines: |-
      458 Walkman Dr.
              Suite #292
    city: Royal Oak
    state: MI
    postal: 48046
ship-to:
  given: Chris
  family: Dumars
  address:
    lines: |-
      458 Walkman Dr.
              Suite #292
    city: Royal Oak
    state: MI
    postal: 48046
product:
- sku: BL394D
  quantity: 4
  description: Basketball
  price: 450
- sku: BL4438H
  quantity: 1
  description: Super Hoop
  price: 2392
tax: 251.420000
total: 4443.520000
comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.
...
"
bool(true)
LoadingChargement en cours