'Combine 2 variable with an array of html code in php
I have 2 variables in my php file and I want to combine it.
// Option 1
$x= array();
$x[] = '<h1>Hello</h1>';
$x[] = '<h1>World!</h1>';
// Option 2
$y= array();
$y[] = '<h1>This is</h1>';
$y[] = '<h1>Me!</h1>';
//Combined Options
$data = array();
$data[] = $x;
$data[] = $y;
If I return the $data
return $data; // this returns array to string error
and if I return the $x
return $x; // this is working
and if I return the $y
return $y; // this is working
However I'm getting array to string conversion error. My goal is,
to combine $x and $y
Solution 1:[1]
Array Merge PHP has a useful function to achieve this.
using it is as simple as this:
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | MysticSeagull |