0

How is it possible to merge multiple separate arrays stored in a variable that are not included in an array themselves ? The variable $unique_answer_title contains these separate arrays, however , I am trying to combine all their elements into one big numerical array.. when I use array_merge($unique_answer_title), nothing gets returned. Below is the data contained in the variable ..

array(4) {
        [0]=> string(9) "Immediate" 
        [1]=> string(3) "Yes" 
        [2]=> string(29) "Have a representative call me" 
        [3]=> string(109) "Biomek<sup><em>&#135;&#135;</em></sup> FX<sup>P</sup> Workstation" 
        } 

    array(8) { 
        [0]=> string(9) "Immediate" 
        [1]=> string(3) "Yes" 
        [2]=> string(29) "Have a representative call me" 
        [3]=> string(109) "Biomek<sup><em>&#135;&#135;</em></sup> FX<sup>P</sup> Workstation" 
        [4]=> string(9) "Immediate" 
        [5]=> string(2) "No" 
        [6]=> string(29) "Have a representative call me" 
        [7]=> string(111) "Biomek<sup><em>&#135;&#135;</em></sup> NX<sup>P</sup> Workstation " 
        } 

    array(12) { 
        [0]=> string(9) "Immediate" 
        [1]=> string(3) "Yes" 
        [2]=> string(29) "Have a representative call me" 
        [3]=> string(109) "Biomek<sup><em>&#135;&#135;</em></sup> FX<sup>P</sup> Workstation" 
        [4]=> string(9) "Immediate" 
        [5]=> string(2) "No" 
        [6]=> string(29) "Have a representative call me" 
        [7]=> string(111) "Biomek<sup><em>&#135;&#135;</em></sup> NX<sup>P</sup> Workstation " 
        [8]=> string(20) "Greater than 3 years" 
        [9]=> string(3) "Yes" 
        [10]=> string(42) "Have a representative contact me via email" 
        [11]=> string(109) "Biomek<sup><em>&#135;&#135;</em></sup> FX<sup>P</sup> Workstation" 
        }
hakre
  • 193,403
  • 52
  • 435
  • 836
user1787184
  • 83
  • 1
  • 4
  • 11
  • `array_merge` is used to merge different arrays, but it seems that your var `$unique_answer_title` is an array containing all sub array. So you should instead do something like `array_merge($unique_answer_title[0], $unique_answer_title[1], ...)` to merge the content. Note that you can give all sub array to the function. If you don't know in advance the number of sub-array, then you will have to iterate to merge sequentially – MatRt May 23 '13 at 20:56
  • I am afraid $unique_answer_title is not an array, but a *string* In this case you are going to have first to separate the arrays, then to eval each code. Not really a good way to do it either, eval is a bunch of security holes if you don't control fully the code to evaluate – Ph.T May 23 '13 at 20:58
  • If `$unique_answer_title` is a string, instead, it will be more complicated, you will have to parse the string in order to re create your arrays, but its really weird. What is the purpose ? – MatRt May 23 '13 at 21:00
  • 1
    no , it's not a string .. when I var_dump it , it returns what I copied above .. without any type of its own .. only shows the multiple arrays in it – user1787184 May 23 '13 at 21:51

2 Answers2

1

The short answer to your question in code is:

$merged = call_user_func_array('array_merge', $unique_answer_title);

This is turning the one array you have into multiple parameters to array_merge() which accepts any number of parameters.

It had been previously (Sep 2010) outlined in:

If you then complain about double values, this has been handled already as well:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
0

You'll have to loop through each of them and the merge them:

$test = array(
 array(
  "Immediate",
  "Yes"
 ),
 array(
  'Test 2',
  'test 3'
 ),
 array(
  'test4',
  'test5'
 )
);

$new_array = array();
foreach($test as $array) $new_array = array_merge($new_array, $array);

var_dump($new_array);

Really, what you're asking is how to flatten a multi-dimensional array, the following may also be helpful: How to "flatten" a multi-dimensional array into a simple one in PHP?

Community
  • 1
  • 1
Justin
  • 471
  • 2
  • 8
  • Justin , those are separate ARRAYS .. they're not into an array otherwise it would have been easy to do .. that's the problem ... – user1787184 May 24 '13 at 03:04
  • You need to add some code (or a var export or something) with your question, because your question states they are in one variable. – Justin May 24 '13 at 12:14
  • Yes , they are in one variable , but they're all separate arrays .. the data you see in the question is what var_dump($unique_answer_title) returns ... I think I really made it clear. – user1787184 May 24 '13 at 15:40
  • I think I answered what you requested. However, maybe what you are wanting that final array to be ONLY the unique values? If so, on the final array just do [array_unique](http://php.net/manual/en/function.array-unique.php). – Justin May 24 '13 at 22:18