La fusión de matrices

  • wpas
  • Graduate
  • Graduate
  • Avatar de Usuario
  • Registrado: Jul 12, 2010
  • Mensajes: 214
  • Loc: Canada
  • Status: Offline

Nota Noviembre 10th, 2010, 6:04 pm

Tengo dos matrices:

Código: [ Select ]

array1 = ("one", "two", "three", "four");

array2 = (1, 2, 3, 4);
  1. array1 = ("one", "two", "three", "four");
  2. array2 = (1, 2, 3, 4);


Si puedo combinar las matrices, me sale:

Código: [ Select ]

$array3 = array_merge($array1, $array2);
  1. $array3 = array_merge($array1, $array2);


lo que da:

Código: [ Select ]

Array
(
  [0] => one
  [1] => two
  [2] => three
  [3] => four
  [4] => 1
  [5] => 2
  [6] => 3
  [7] => 4
)
  1. Array
  2. (
  3.   [0] => one
  4.   [1] => two
  5.   [2] => three
  6.   [3] => four
  7.   [4] => 1
  8.   [5] => 2
  9.   [6] => 3
  10.   [7] => 4
  11. )


¿Es posible combinar los arreglos para que el resultado es el siguiente:

Código: [ Select ]

Array
(
  [0] => one 1
  [1] => two 2
  [2] => three 3
  [3] => four 4

)
  1. Array
  2. (
  3.   [0] => one 1
  4.   [1] => two 2
  5.   [2] => three 3
  6.   [3] => four 4
  7. )


[0] de array1 se fusionó con [0] de matriz2, etc

También me gustaría tener un espacio para separar, como se muestra

Gracias
http://www.schembrionics.com
The Ultimate Solutions Center
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Noviembre 10th, 2010, 6:04 pm

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • Avatar de Usuario
  • Registrado: May 22, 2004
  • Mensajes: 3415
  • Loc: Richland, WA
  • Status: Offline

Nota Noviembre 10th, 2010, 6:18 pm

Código: [ Select ]
$var1 = "one";
$var2 = 1;

$var3 = $var1 + " " + $var2;

echo $var3;
  1. $var1 = "one";
  2. $var2 = 1;
  3. $var3 = $var1 + " " + $var2;
  4. echo $var3;


Le dará "un 1"
#define NULL (::rand() % 2)
  • wpas
  • Graduate
  • Graduate
  • Avatar de Usuario
  • Registrado: Jul 12, 2010
  • Mensajes: 214
  • Loc: Canada
  • Status: Offline

Nota Noviembre 10th, 2010, 9:28 pm

Hola falsos

Yo entiendo su concepto, pero estoy teniendo problemas tratando de incorporarlo.

Como he indicado que tengo dos matrices, $ matriz1 y $ matriz2

Son creados de forma dinámica, los elementos que se agregan automáticamente a cada matriz haciéndolos más grandes con el tiempo.

Me uno tratando de utilizar los bucles foreach para conseguir lo que usted menciona, pero no estoy teniendo suerte.

¿Podría usted me empezó con el guión

Gracias
http://www.schembrionics.com
The Ultimate Solutions Center
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8211
  • Loc: USA
  • Status: Offline

Nota Noviembre 10th, 2010, 9:50 pm

PHP Código: [ Select ]
<?php
$array1 = array("one", "two", "three", "four");
$array2 = array(1, 2, 3, 4);
$array3 = array();
for ($i = 0; $i < count($array1); $i++) {
    $array3[$i] = $array1[$i] . ' ' . $array2[$i];
}
 
echo '<pre>';
print_r($array3);
echo '</pre>';
?>
  1. <?php
  2. $array1 = array("one", "two", "three", "four");
  3. $array2 = array(1, 2, 3, 4);
  4. $array3 = array();
  5. for ($i = 0; $i < count($array1); $i++) {
  6.     $array3[$i] = $array1[$i] . ' ' . $array2[$i];
  7. }
  8.  
  9. echo '<pre>';
  10. print_r($array3);
  11. echo '</pre>';
  12. ?>


Eso debería funcionar si ambas matrices son de igual longitud.

[EDIT] Usted puede utilizar el siguiente si usted no tiene la misma longitud y si no es importante
PHP Código: [ Select ]
<?php
$array1 = array("one", "two", "three", "four");
$array2 = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$array3 = array();
$c1 = count($array1);
$c2 = count($array2);
$count = ($c1 > $c2) ? $c1 : $c2;
for ($i = 0; $i < $count; $i++) {
    $array3[$i] = trim($array1[$i] . ' ' . $array2[$i]);
}
 
echo '<pre>';
print_r($array3);
echo '</pre>';
?>
  1. <?php
  2. $array1 = array("one", "two", "three", "four");
  3. $array2 = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
  4. $array3 = array();
  5. $c1 = count($array1);
  6. $c2 = count($array2);
  7. $count = ($c1 > $c2) ? $c1 : $c2;
  8. for ($i = 0; $i < $count; $i++) {
  9.     $array3[$i] = trim($array1[$i] . ' ' . $array2[$i]);
  10. }
  11.  
  12. echo '<pre>';
  13. print_r($array3);
  14. echo '</pre>';
  15. ?>
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • wpas
  • Graduate
  • Graduate
  • Avatar de Usuario
  • Registrado: Jul 12, 2010
  • Mensajes: 214
  • Loc: Canada
  • Status: Offline

Nota Noviembre 10th, 2010, 10:16 pm

Hola Bogey

Trabajó como un encanto, agradable y fácil

Muchas gracias
http://www.schembrionics.com
The Ultimate Solutions Center

Publicar Información

  • Total de mensajes en este tema: 5 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 159 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC