Свежайшие Пирожки от CakePHP по-русски

Полнейшее руководство CakePHP 1.2 на русском языке, горячие новости и полезные статьи

combine

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.

Простой текст
  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('User' => array('id' => 2, 'group_id' => 1,
  7. 'Data' => array('user' => 'mariano.iglesias','name' => 'Mariano Iglesias'))),
  8. array('User' => array('id' => 14, 'group_id' => 2,
  9. 'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
  10. array('User' => array('id' => 25, 'group_id' => 1,
  11. 'Data' => array('user' => 'gwoo','name' => 'The Gwoo'))));
  12. $result = Set::combine($a, '{n}.User.id');
  13. /* $result now looks like:
  14. Array
  15. (
  16. [2] =>
  17. [14] =>
  18. [25] =>
  19. )
  20. */
  21. $result = Set::combine($a, '{n}.User.id', '{n}.User.non-existant');
  22. /* $result now looks like:
  23. Array
  24. (
  25. [2] =>
  26. [14] =>
  27. [25] =>
  28. )
  29. */
  30. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
  31. /* $result now looks like:
  32. Array
  33. (
  34. [2] => Array
  35. (
  36. [user] => mariano.iglesias
  37. [name] => Mariano Iglesias
  38. )
  39. [14] => Array
  40. (
  41. [user] => phpnut
  42. [name] => Larry E. Masters
  43. )
  44. [25] => Array
  45. (
  46. [user] => gwoo
  47. [name] => The Gwoo
  48. )
  49. )
  50. */
  51. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
  52. /* $result now looks like:
  53. Array
  54. (
  55. [2] => Mariano Iglesias
  56. [14] => Larry E. Masters
  57. [25] => The Gwoo
  58. )
  59. */
  60. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  61. /* $result now looks like:
  62. Array
  63. (
  64. [1] => Array
  65. (
  66. [2] => Array
  67. (
  68. [user] => mariano.iglesias
  69. [name] => Mariano Iglesias
  70. )
  71. [25] => Array
  72. (
  73. [user] => gwoo
  74. [name] => The Gwoo
  75. )
  76. )
  77. [2] => Array
  78. (
  79. [14] => Array
  80. (
  81. [user] => phpnut
  82. [name] => Larry E. Masters
  83. )
  84. )
  85. )
  86. */
  87. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  88. /* $result now looks like:
  89. Array
  90. (
  91. [1] => Array
  92. (
  93. [2] => Mariano Iglesias
  94. [25] => The Gwoo
  95. )
  96. [2] => Array
  97. (
  98. [14] => Larry E. Masters
  99. )
  100. )
  101. */
  102. $result = Set::combine($a, '{n}.User.id');
  103. /* $result now looks like:
  104. Array
  105. (
  106. [2] =>
  107. [14] =>
  108. [25] =>
  109. )
  110. */
  111. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
  112. /* $result now looks like:
  113. Array
  114. (
  115. [2] => Array
  116. (
  117. [user] => mariano.iglesias
  118. [name] => Mariano Iglesias
  119. )
  120. [14] => Array
  121. (
  122. [user] => phpnut
  123. [name] => Larry E. Masters
  124. )
  125. [25] => Array
  126. (
  127. [user] => gwoo
  128. [name] => The Gwoo
  129. )
  130. )
  131. */
  132. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
  133. /* $result now looks like:
  134. Array
  135. (
  136. [2] => Mariano Iglesias
  137. [14] => Larry E. Masters
  138. [25] => The Gwoo
  139. )
  140. */
  141. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  142. /* $result now looks like:
  143. Array
  144. (
  145. [1] => Array
  146. (
  147. [2] => Array
  148. (
  149. [user] => mariano.iglesias
  150. [name] => Mariano Iglesias
  151. )
  152. [25] => Array
  153. (
  154. [user] => gwoo
  155. [name] => The Gwoo
  156. )
  157. )
  158. [2] => Array
  159. (
  160. [14] => Array
  161. (
  162. [user] => phpnut
  163. [name] => Larry E. Masters
  164. )
  165. )
  166. )
  167. */
  168. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  169. /* $result now looks like:
  170. Array
  171. (
  172. [1] => Array
  173. (
  174. [2] => Mariano Iglesias
  175. [25] => The Gwoo
  176. )
  177. [2] => Array
  178. (
  179. [14] => Larry E. Masters
  180. )
  181. )
  182. */
  183. $result = Set::combine($a, '{n}.User.id', array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.group_id');
  184. /* $result now looks like:
  185. Array
  186. (
  187. [1] => Array
  188. (
  189. [2] => mariano.iglesias: Mariano Iglesias
  190. [25] => gwoo: The Gwoo
  191. )
  192. [2] => Array
  193. (
  194. [14] => phpnut: Larry E. Masters
  195. )
  196. )
  197. */
  198. $result = Set::combine($a, array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
  199. /* $result now looks like:
  200. Array
  201. (
  202. [mariano.iglesias: Mariano Iglesias] => 2
  203. [phpnut: Larry E. Masters] => 14
  204. [gwoo: The Gwoo] => 25
  205. )
  206. */
  207. $result = Set::combine($a, array('{1}: {0}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
  208. /* $result now looks like:
  209. Array
  210. (
  211. [Mariano Iglesias: mariano.iglesias] => 2
  212. [Larry E. Masters: phpnut] => 14
  213. [The Gwoo: gwoo] => 25
  214. )
  215. */
  216. $result = Set::combine($a, array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
  217. /* $result now looks like:
  218. Array
  219. (
  220. [mariano.iglesias: 2] => Mariano Iglesias
  221. [phpnut: 14] => Larry E. Masters
  222. [gwoo: 25] => The Gwoo
  223. )
  224. */
  225. $result = Set::combine($a, array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
  226. /* $result now looks like:
  227. Array
  228. (
  229. [2: mariano.iglesias] => Mariano Iglesias
  230. [14: phpnut] => Larry E. Masters
  231. [25: gwoo] => The Gwoo
  232. )
  233. */