list_keyed.phpt 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. --TEST--
  2. list() with keys
  3. --FILE--
  4. <?php
  5. $antonyms = [
  6. "good" => "bad",
  7. "happy" => "sad",
  8. ];
  9. list("good" => $good_antonym, "happy" => $happy_antonym) = $antonyms;
  10. var_dump($good_antonym, $happy_antonym);
  11. echo PHP_EOL;
  12. $powersOfTwo = [
  13. 1 => 2,
  14. 2 => 4,
  15. 3 => 8
  16. ];
  17. list(1 => $two_1, 2 => $two_2, 3 => $two_3) = $powersOfTwo;
  18. var_dump($two_1, $two_2, $two_3);
  19. echo PHP_EOL;
  20. $contrivedMixedKeyTypesExample = [
  21. 7 => "the best PHP version",
  22. "elePHPant" => "the cutest mascot"
  23. ];
  24. list(7 => $seven, "elePHPant" => $elePHPant) = $contrivedMixedKeyTypesExample;
  25. var_dump($seven, $elePHPant);
  26. echo PHP_EOL;
  27. $allTogetherNow = [
  28. "antonyms" => $antonyms,
  29. "powersOfTwo" => $powersOfTwo,
  30. "contrivedMixedKeyTypesExample" => $contrivedMixedKeyTypesExample
  31. ];
  32. list(
  33. "antonyms" => list("good" => $good_antonym, "happy" => $happy_antonym),
  34. "powersOfTwo" => list(1 => $two_1, 2 => $two_2, 3 => $two_3),
  35. "contrivedMixedKeyTypesExample" => list(7 => $seven, "elePHPant" => $elePHPant)
  36. ) = $allTogetherNow;
  37. var_dump($good_antonym, $happy_antonym);
  38. var_dump($two_1, $two_2, $two_3);
  39. var_dump($seven, $elePHPant);
  40. ?>
  41. --EXPECT--
  42. string(3) "bad"
  43. string(3) "sad"
  44. int(2)
  45. int(4)
  46. int(8)
  47. string(20) "the best PHP version"
  48. string(17) "the cutest mascot"
  49. string(3) "bad"
  50. string(3) "sad"
  51. int(2)
  52. int(4)
  53. int(8)
  54. string(20) "the best PHP version"
  55. string(17) "the cutest mascot"