All ways to combine arrays in php

php. array_merge() array_merge_recursive() . implode() . .

array_merge()

php . . , . , , .

, . .

First join example




, color, 2 4, a, b. shape => trapezoid 4. . green.

:

Array

(

[color] => green

[0] => 2

[1] => 4

[2] => a

[3] => b

[shape] => trapezoid

[4] => 4

)
      
      



, php.





The second example of unification




, , 3. . :

array(5) {

[0]=>

string(6) "zero_a"

[2]=>

string(5) "two_a"

[3]=>

string(7) "three_a"

[1]=>

string(5) "one_b"

[4]=>

string(6) "four_b"

}

array_merge_recursive()
      
      



array_merge_recursive()

php. . , array_merge(). , , .

The third example of unification




. , favorite, . 5, 10 . color => array(...) , 5 10. blue , red green. :





Array

(

[color] => Array

(

[favorite] => Array

(

[0] => red

[1] => green

)

[0] => blue

)

[0] => 5

[1] => 10

)
      
      



implode()

php , implode(). :

string implode ( string $glue , array $pieces )
      
      



$glue , . $pieces , . , $glue. :

$pieces[0] . $glue . $pieces[1] . $glue . $pieces[2]
      
      



implode() explode(), .

The fourth example of unification




'', '' '' $comma_separated. ",". “,,”.

php - :

  • array_merge();
  • array_merge_recursive();
  • implode().

, . , . , . , . , .

implode() . , .




All Articles