0

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

1 Answer 1

2

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);
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.