Welcome to the Cookbook

loading...

8.5.5 combine

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

array Set::combine ($data, $path1 = null, $path2 = null, $groupPath = null)

Creates an associative array using a $path1 as the path to build its keys, and optionally $path2 as path to get the values. If $path2 is not specified, all values will be initialized to null (useful for Set::merge). You can optionally group the values by what is obtained when following the path specified in $groupPath.


$result = Set::combine(array(), '{n}.User.id', '{n}.User.Data');
// $result == array();

$result = Set::combine('', '{n}.User.id', '{n}.User.Data');
// $result == array();

$a = array(
	array(
		'User' => array(
			'id' => 2, 
			'group_id' => 1,
			'Data' => array(
				'user' => 'mariano.iglesias',
				'name' => 'Mariano Iglesias'
			)
		)
	),
	array(
		'User' => array(
			'id' => 14, 
			'group_id' => 2,
			'Data' => array(
				'user' => 'phpnut', 
				'name' => 'Larry E. Masters'
			)
		)
	),
	array(
		'User' => array(
			'id' => 25, 
			'group_id' => 1,
			'Data' => array(
				'user' => 'gwoo',
				'name' => 'The Gwoo'
			)
		)
	)
);
$result = Set::combine($a, '{n}.User.id');
/* $result now looks like: 
	Array
	(
		[2] => 
		[14] => 
		[25] => 
	)
*/

$result = Set::combine($a, '{n}.User.id', '{n}.User.non-existant');
/* $result now looks like: 
	Array
	(
		[2] => 
		[14] => 
		[25] => 
	)
*/

$result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
/* $result now looks like: 
	Array
	(
	    [2] => Array
	        (
	            [user] => mariano.iglesias
	            [name] => Mariano Iglesias
	        )
	    [14] => Array
	        (
	            [user] => phpnut
	            [name] => Larry E. Masters
	        )
	    [25] => Array
	        (
	            [user] => gwoo
	            [name] => The Gwoo
	        )
	)
*/

$result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
/* $result now looks like: 
	Array
	(
	    [2] => Mariano Iglesias
	    [14] => Larry E. Masters
	    [25] => The Gwoo
	)
*/

$result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
/* $result now looks like: 
	Array
	(
	    [1] => Array
	        (
	            [2] => Array
	                (
	                    [user] => mariano.iglesias
	                    [name] => Mariano Iglesias
	                )
	            [25] => Array
	                (
	                    [user] => gwoo
	                    [name] => The Gwoo
	                )
	        )
	    [2] => Array
	        (
	            [14] => Array
	                (
	                    [user] => phpnut
	                    [name] => Larry E. Masters
	                )
	        )
	)
*/

$result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
/* $result now looks like: 
	Array
	(
	    [1] => Array
	        (
	            [2] => Mariano Iglesias
	            [25] => The Gwoo
	        )
	    [2] => Array
	        (
	            [14] => Larry E. Masters
	        )
	)
*/

$result = Set::combine($a, '{n}.User.id', array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.group_id');
/* $result now looks like: 
	Array
	(
	    [1] => Array
	        (
	            [2] => mariano.iglesias: Mariano Iglesias
	            [25] => gwoo: The Gwoo
	        )
	    [2] => Array
	        (
	            [14] => phpnut: Larry E. Masters
	        )
	)		
*/

$result = Set::combine($a, array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
/* $result now looks like: 
	Array
	(
	    [mariano.iglesias: Mariano Iglesias] => 2
	    [phpnut: Larry E. Masters] => 14
	    [gwoo: The Gwoo] => 25
	)
*/

$result = Set::combine($a, array('{1}: {0}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
/* $result now looks like: 
	Array
	(
	    [Mariano Iglesias: mariano.iglesias] => 2
	    [Larry E. Masters: phpnut] => 14
	    [The Gwoo: gwoo] => 25
	)		
*/

$result = Set::combine($a, array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');

/* $result now looks like: 
	Array
	(
	    [mariano.iglesias: 2] => Mariano Iglesias
	    [phpnut: 14] => Larry E. Masters
	    [gwoo: 25] => The Gwoo
	)
*/

$result = Set::combine($a, array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
/* $result now looks like: 
	Array
	(
	    [2: mariano.iglesias] => Mariano Iglesias
	    [14: phpnut] => Larry E. Masters
	    [25: gwoo] => The Gwoo
	)
*/
  1. $result = Set::combine(array(), '{n}.User.id', '{n}.User.Data');
  2. // $result == array();
  3. $result = Set::combine('', '{n}.User.id', '{n}.User.Data');
  4. // $result == array();
  5. $a = array(
  6. array(
  7. 'User' => array(
  8. 'id' => 2,
  9. 'group_id' => 1,
  10. 'Data' => array(
  11. 'user' => 'mariano.iglesias',
  12. 'name' => 'Mariano Iglesias'
  13. )
  14. )
  15. ),
  16. array(
  17. 'User' => array(
  18. 'id' => 14,
  19. 'group_id' => 2,
  20. 'Data' => array(
  21. 'user' => 'phpnut',
  22. 'name' => 'Larry E. Masters'
  23. )
  24. )
  25. ),
  26. array(
  27. 'User' => array(
  28. 'id' => 25,
  29. 'group_id' => 1,
  30. 'Data' => array(
  31. 'user' => 'gwoo',
  32. 'name' => 'The Gwoo'
  33. )
  34. )
  35. )
  36. );
  37. $result = Set::combine($a, '{n}.User.id');
  38. /* $result now looks like:
  39. Array
  40. (
  41. [2] =>
  42. [14] =>
  43. [25] =>
  44. )
  45. */
  46. $result = Set::combine($a, '{n}.User.id', '{n}.User.non-existant');
  47. /* $result now looks like:
  48. Array
  49. (
  50. [2] =>
  51. [14] =>
  52. [25] =>
  53. )
  54. */
  55. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
  56. /* $result now looks like:
  57. Array
  58. (
  59. [2] => Array
  60. (
  61. [user] => mariano.iglesias
  62. [name] => Mariano Iglesias
  63. )
  64. [14] => Array
  65. (
  66. [user] => phpnut
  67. [name] => Larry E. Masters
  68. )
  69. [25] => Array
  70. (
  71. [user] => gwoo
  72. [name] => The Gwoo
  73. )
  74. )
  75. */
  76. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
  77. /* $result now looks like:
  78. Array
  79. (
  80. [2] => Mariano Iglesias
  81. [14] => Larry E. Masters
  82. [25] => The Gwoo
  83. )
  84. */
  85. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  86. /* $result now looks like:
  87. Array
  88. (
  89. [1] => Array
  90. (
  91. [2] => Array
  92. (
  93. [user] => mariano.iglesias
  94. [name] => Mariano Iglesias
  95. )
  96. [25] => Array
  97. (
  98. [user] => gwoo
  99. [name] => The Gwoo
  100. )
  101. )
  102. [2] => Array
  103. (
  104. [14] => Array
  105. (
  106. [user] => phpnut
  107. [name] => Larry E. Masters
  108. )
  109. )
  110. )
  111. */
  112. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  113. /* $result now looks like:
  114. Array
  115. (
  116. [1] => Array
  117. (
  118. [2] => Mariano Iglesias
  119. [25] => The Gwoo
  120. )
  121. [2] => Array
  122. (
  123. [14] => Larry E. Masters
  124. )
  125. )
  126. */
  127. $result = Set::combine($a, '{n}.User.id', array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.group_id');
  128. /* $result now looks like:
  129. Array
  130. (
  131. [1] => Array
  132. (
  133. [2] => mariano.iglesias: Mariano Iglesias
  134. [25] => gwoo: The Gwoo
  135. )
  136. [2] => Array
  137. (
  138. [14] => phpnut: Larry E. Masters
  139. )
  140. )
  141. */
  142. $result = Set::combine($a, array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
  143. /* $result now looks like:
  144. Array
  145. (
  146. [mariano.iglesias: Mariano Iglesias] => 2
  147. [phpnut: Larry E. Masters] => 14
  148. [gwoo: The Gwoo] => 25
  149. )
  150. */
  151. $result = Set::combine($a, array('{1}: {0}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
  152. /* $result now looks like:
  153. Array
  154. (
  155. [Mariano Iglesias: mariano.iglesias] => 2
  156. [Larry E. Masters: phpnut] => 14
  157. [The Gwoo: gwoo] => 25
  158. )
  159. */
  160. $result = Set::combine($a, array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
  161. /* $result now looks like:
  162. Array
  163. (
  164. [mariano.iglesias: 2] => Mariano Iglesias
  165. [phpnut: 14] => Larry E. Masters
  166. [gwoo: 25] => The Gwoo
  167. )
  168. */
  169. $result = Set::combine($a, array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
  170. /* $result now looks like:
  171. Array
  172. (
  173. [2: mariano.iglesias] => Mariano Iglesias
  174. [14: phpnut] => Larry E. Masters
  175. [25: gwoo] => The Gwoo
  176. )
  177. */