Welcome to the Cookbook

loading...

8.5.18 map

There is no translation yet for this section. Please help out and translate this.. More information about translations

object Set::map ($class = 'stdClass', $tmp = 'stdClass')

This method Maps the contents of the Set object to an object hierarchy while maintaining numeric keys as arrays of objects.

Basically, the map function turns array items into initialized class objects. By default it turns an array into a stdClass Object, however you can map values into any type of class. Example: Set::map($array_of_values, 'nameOfYourClass');

$data = array(
	array(
		"IndexedPage" => array(
			"id" => 1,
			"url" => 'http://blah.com/',
			'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
			'get_vars' => '',
			'redirect' => '',
			'created' => "1195055503",
			'updated' => "1195055503",
		)
	),
	array(
		"IndexedPage" => array(
			"id" => 2,
			"url" => 'http://blah.com/',
			'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
			'get_vars' => '',
			'redirect' => '',
			'created' => "1195055503",
			'updated' => "1195055503",
		),
	)
);
$mapped = Set::map($data);

/* $mapped now looks like:

	Array
	(
	    [0] => stdClass Object
	        (
	            [_name_] => IndexedPage
	            [id] => 1
	            [url] => http://blah.com/
	            [hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
	            [get_vars] => 
	            [redirect] => 
	            [created] => 1195055503
	            [updated] => 1195055503
	        )

	    [1] => stdClass Object
	        (
	            [_name_] => IndexedPage
	            [id] => 2
	            [url] => http://blah.com/
	            [hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
	            [get_vars] => 
	            [redirect] => 
	            [created] => 1195055503
	            [updated] => 1195055503
	        )

	)

*/
  1. $data = array(
  2. array(
  3. "IndexedPage" => array(
  4. "id" => 1,
  5. "url" => 'http://blah.com/',
  6. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  7. 'get_vars' => '',
  8. 'redirect' => '',
  9. 'created' => "1195055503",
  10. 'updated' => "1195055503",
  11. )
  12. ),
  13. array(
  14. "IndexedPage" => array(
  15. "id" => 2,
  16. "url" => 'http://blah.com/',
  17. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  18. 'get_vars' => '',
  19. 'redirect' => '',
  20. 'created' => "1195055503",
  21. 'updated' => "1195055503",
  22. ),
  23. )
  24. );
  25. $mapped = Set::map($data);
  26. /* $mapped now looks like:
  27. Array
  28. (
  29. [0] => stdClass Object
  30. (
  31. [_name_] => IndexedPage
  32. [id] => 1
  33. [url] => http://blah.com/
  34. [hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
  35. [get_vars] =>
  36. [redirect] =>
  37. [created] => 1195055503
  38. [updated] => 1195055503
  39. )
  40. [1] => stdClass Object
  41. (
  42. [_name_] => IndexedPage
  43. [id] => 2
  44. [url] => http://blah.com/
  45. [hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
  46. [get_vars] =>
  47. [redirect] =>
  48. [created] => 1195055503
  49. [updated] => 1195055503
  50. )
  51. )
  52. */

Using Set::map() with a custom class for second parameter:

class MyClass {
    function sayHi() {
        echo 'Hi!';
    }
}

$mapped = Set::map($data, 'MyClass');
//Now you can access all the properties as in the example above, 
//but also you can call MyClass's methods
$mapped->[0]->sayHi();
  1. class MyClass {
  2. function sayHi() {
  3. echo 'Hi!';
  4. }
  5. }
  6. $mapped = Set::map($data, 'MyClass');
  7. //Now you can access all the properties as in the example above,
  8. //but also you can call MyClass's methods
  9. $mapped->[0]->sayHi();