Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. <?php
  2. $origin = [2, 4, 2, 6];
  3. foreach ($origin as $value) {
  4. $value = 1;
  5. }
  6. print_r($origin);
  7.  
  8. $origin = ['x', 4, 'x', 6];
  9. foreach ($origin as $value) {
  10. $value = 1;
  11. }
  12. print_r($origin);
  13.  
  14. Array
  15. (
  16. [0] => 2
  17. [1] => 4
  18. [2] => 2
  19. [3] => 6
  20. )
  21. Array
  22. (
  23. [0] => x
  24. [1] => 4
  25. [2] => x
  26. [3] => 6
  27. )
  28.  
  29. <?php
  30. $origin = [2, 4, 2, 6];
  31.  
  32. $result = [];
  33. foreach ($origin as $value) {
  34.  
  35. // Cuando el valor es 2 se cambia por una 'x'
  36. if ($value == 2) {
  37. $result[] = 'x';
  38. } else {
  39. $result[] = $value;
  40. }
  41. }
  42. print_r($result);
  43.  
  44. <?php
  45. $origin = [2, 4, 2, 6];
  46.  
  47. $result = [];
  48. foreach ($origin as $value) {
  49.  
  50. // Cuando el valor es 2 se cambia por una 'x'
  51. if ($value == 2) {
  52. $result[] = 'x';
  53. continue;
  54. }
  55.  
  56. $result[] = $value;
  57. }
  58. print_r($result);
  59.  
  60. Array
  61. (
  62. [0] => x
  63. [1] => 4
  64. [2] => x
  65. [3] => 6
  66. )
  67.  
  68. <?php
  69. $origin = [2, 4, 2, 6];
  70.  
  71. $result = [];
  72. foreach ($origin as $value) {
  73.  
  74. // Cuando el valor es 2 se cambia por una 'x'
  75. if ($value == 2) {
  76. $result[] = 'x';
  77. continue;
  78. }
  79.  
  80. $result[] = $value;
  81. }
  82. print_r($result);
  83.  
  84. $origin = [];
  85. foreach ($result as $value) {
  86.  
  87. // Cuando el valor es 2 se cambia por una 'x'
  88. if ($value == 'x') {
  89. $origin[] = 2;
  90. continue;
  91. }
  92.  
  93. $origin[] = $value;
  94. }
  95. print_r($origin);
  96.  
  97. Array
  98. (
  99. [0] => x
  100. [1] => 4
  101. [2] => x
  102. [3] => 6
  103. )
  104. Array
  105. (
  106. [0] => 2
  107. [1] => 4
  108. [2] => 2
  109. [3] => 6
  110. )
  111.  
  112. <?php
  113. $origin = [2, 4, 2, 6];
  114.  
  115. // Reasignar algunos valores pasandolos por referencia
  116. foreach ($origin as &$value) {
  117. if ($value == 2)
  118. $value = 'x';
  119. }
  120. print_r($origin);
  121.  
  122. // Reasignar algunos valores pasandolos por referencia
  123. foreach ($origin as &$value) {
  124.  
  125. if ($value == 'x')
  126. $value = 1;
  127. }
  128. print_r($origin);
  129.  
  130. // Trato de reasignar todos los valores sin pasarlos por referencia
  131. foreach ($origin as $value) {
  132.  
  133. $value = 5;
  134. }
  135. print_r($origin);
  136.  
  137. Array
  138. (
  139. [0] => x
  140. [1] => 4
  141. [2] => x
  142. [3] => 6
  143. )
  144. Array
  145. (
  146. [0] => 1
  147. [1] => 4
  148. [2] => 1
  149. [3] => 6
  150. )
  151. Array
  152. (
  153. [0] => 1
  154. [1] => 4
  155. [2] => 1
  156. [3] => 5
  157. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement