Welcome to the Cookbook

loading...

8.5.4 reverse

array Set::reverse ($object)

Set::reverse is basically the opposite of Set::map. It converts an object into an array. If $object is not an object, reverse will simply return $object.

$result = Set::reverse(null);
// Null
$result = Set::reverse(false);
// false
$a = array(
	'Post' => array('id'=> 1, 'title' => 'First Post'),
	'Comment' => array(
		array('id'=> 1, 'title' => 'First Comment'),
		array('id'=> 2, 'title' => 'Second Comment')
	),
	'Tag' => array(
		array('id'=> 1, 'title' => 'First Tag'),
		array('id'=> 2, 'title' => 'Second Tag')
	),
);
$map = Set::map($a); // Turn $a into a class object
/* $map now looks like:
	stdClass Object
	(
	    [_name_] => Post
	    [id] => 1
	    [title] => First Post
	    [Comment] => Array
	        (
	            [0] => stdClass Object
	                (
	                    [id] => 1
	                    [title] => First Comment
	                )
	            [1] => stdClass Object
	                (
	                    [id] => 2
	                    [title] => Second Comment
					)
	        )
	    [Tag] => Array
	        (
	            [0] => stdClass Object
	                (
	                    [id] => 1
	                    [title] => First Tag
	                )
	            [1] => stdClass Object
	                (
	                    [id] => 2
	                    [title] => Second Tag
	                )
	        )
	)
*/

$result = Set::reverse($map);
/* $result now looks like:
	Array
	(
	    [Post] => Array
	        (
	            [id] => 1
	            [title] => First Post
	            [Comment] => Array
	                (
	                    [0] => Array
	                        (
	                            [id] => 1
	                            [title] => First Comment
	                        )
	                    [1] => Array
	                        (
	                            [id] => 2
	                            [title] => Second Comment
	                        )
	                )
	            [Tag] => Array
	                (
	                    [0] => Array
	                        (
	                            [id] => 1
	                            [title] => First Tag
	                        )
	                    [1] => Array
	                        (
	                            [id] => 2
	                            [title] => Second Tag
	                        )
	                )
	        )
	)
*/

$result = Set::reverse($a['Post']); // Just return the array
/* $result now looks like: 
	Array
	(
		[id] => 1
		[title] => First Post
	)
*/
	
  1. $result = Set::reverse(null);
  2. // Null
  3. $result = Set::reverse(false);
  4. // false
  5. $a = array(
  6. 'Post' => array('id'=> 1, 'title' => 'First Post'),
  7. 'Comment' => array(
  8. array('id'=> 1, 'title' => 'First Comment'),
  9. array('id'=> 2, 'title' => 'Second Comment')
  10. ),
  11. 'Tag' => array(
  12. array('id'=> 1, 'title' => 'First Tag'),
  13. array('id'=> 2, 'title' => 'Second Tag')
  14. ),
  15. );
  16. $map = Set::map($a); // Turn $a into a class object
  17. /* $map now looks like:
  18. stdClass Object
  19. (
  20. [_name_] => Post
  21. [id] => 1
  22. [title] => First Post
  23. [Comment] => Array
  24. (
  25. [0] => stdClass Object
  26. (
  27. [id] => 1
  28. [title] => First Comment
  29. )
  30. [1] => stdClass Object
  31. (
  32. [id] => 2
  33. [title] => Second Comment
  34. )
  35. )
  36. [Tag] => Array
  37. (
  38. [0] => stdClass Object
  39. (
  40. [id] => 1
  41. [title] => First Tag
  42. )
  43. [1] => stdClass Object
  44. (
  45. [id] => 2
  46. [title] => Second Tag
  47. )
  48. )
  49. )
  50. */
  51. $result = Set::reverse($map);
  52. /* $result now looks like:
  53. Array
  54. (
  55. [Post] => Array
  56. (
  57. [id] => 1
  58. [title] => First Post
  59. [Comment] => Array
  60. (
  61. [0] => Array
  62. (
  63. [id] => 1
  64. [title] => First Comment
  65. )
  66. [1] => Array
  67. (
  68. [id] => 2
  69. [title] => Second Comment
  70. )
  71. )
  72. [Tag] => Array
  73. (
  74. [0] => Array
  75. (
  76. [id] => 1
  77. [title] => First Tag
  78. )
  79. [1] => Array
  80. (
  81. [id] => 2
  82. [title] => Second Tag
  83. )
  84. )
  85. )
  86. )
  87. */
  88. $result = Set::reverse($a['Post']); // Just return the array
  89. /* $result now looks like:
  90. Array
  91. (
  92. [id] => 1
  93. [title] => First Post
  94. )
  95. */