join array php code example

Example 1: php array_merge

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

Example 2: php array join

$arr = array('Hello','World!','Beautiful','Day!');
echo join(", ",$arr);

Example 3: php merge 2 arrays

<?php
  $array1 = [
      "color" => "green"
  ];
  $array2 = [
      "color" => "red", 
      "color" => "blue"
  ];
  $result = array_merge($array1, $array2);
?>

// $result
[
    "color" => "green"
    "color" => "red", 
    "color" => "blue"
]

Example 4: join array of strings php

$arr = array('Hello','World!','Beautiful','Day!');
echo join(",",$arr);

Example 5: php join array

Definition and Usage
The join() function returns a string from the elements of an array.

The join() function is an alias of the implode() function.

Note: The join() function accept its parameters in either order. However, for consistency with explode(), you should use the documented order of arguments.

Note: The separator parameter of join() is optional. However, it is recommended to always use two parameters for backwards compatibility.

Syntax
join(separator,array)
  
Example
Join array elements with a string:

<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo join(" ",$arr);
?>
  
Output:
Hello World! Beautiful Day!

Example 6: php inner join array

$array1 = [1, 5, 64, 2, 6];
$array2 = [2, 1, 8, 3];

//Method 1:
array_filter($array1, function($_){
    global $array2;
  return in_array($_, $array2);
}); // Output: [0 => 1, 3 => 2]

//Method 2:
array_intersect($array1, $array2); //Output: [0 => 1, 3 => 2]

Tags:

Php Example