Exception
Exception

Reputation: 787

How to merge multidimentional array into single array?

I have this array which contains 2 keys and values..

array (
  23 => 
  array (
    0 => 9,
    1 => 13,
    2 => 2,
    3 => 11,
    4 => 4,
    5 => 5,
    6 => 6,
    7 => 12,
    8 => 1,
    9 => 7,
    10 => 10,
    11 => 8,
    12 => 3,
  ),
  1 => 
  array (
    0 => 9,
    1 => 13,
    2 => 2,
    3 => 11,
    4 => 4,
    5 => 5,
    6 => 6,
    7 => 14,
    8 => 12,
    9 => 1,
    10 => 7,
    11 => 10,
    12 => 8,
    13 => 3,
  )
)

So How can I convert this into single query with distinct values like this

array (
  0 => 9,
  1 => 13,
  2 => 2,
  3 => 11,
  4 => 4,
  5 => 5,
  6 => 6,
  7 => 12,
  8 => 1,
  9 => 7,
  10 => 10,
  11 => 8,
  12 => 3,
  20 => 14,
)

That means it should be first merge and then create distinct values array without using more foreach/for loop.

This is the code I have tried http://codepad.org/x881cBt1

Upvotes: 0

Views: 149

Answers (5)

Thamilhan
Thamilhan

Reputation: 13293

You can use array_walk_recursive to flatten your multidimensional array:

$flatten = [];
array_walk_recursive($array, function ($value) use (&$flatten) { 
        $flatten[] = $value; 
});
$flatten = array_unique($flatten); //Taking Unique for the flattened array
print_r($flatten);

This should give:

Array
(
    [0] => 9
    [1] => 13
    [2] => 2
    [3] => 11
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 12
    [8] => 1
    [9] => 7
    [10] => 10
    [11] => 8
    [12] => 3
    [20] => 14
)

Check EVAL

Upvotes: 1

Ehsan Ilahi
Ehsan Ilahi

Reputation: 298

you can use this code as you said this code is without any loop

    $myArray = array (
      23 => 
      array (
        0 => 9,
        1 => 13,
        2 => 2,
        3 => 11,
        4 => 4,
        5 => 5,
        6 => 6,
        7 => 12,
        8 => 1,
        9 => 7,
        10 => 10,
        11 => 8,
        12 => 3,
      ),
      1 => 
      array (
        0 => 9,
        1 => 13,
        2 => 2,
        3 => 11,
        4 => 4,
        5 => 5,
        6 => 6,
        7 => 14,
        8 => 12,
        9 => 1,
        10 => 7,
        11 => 10,
        12 => 8,
        13 => 3,
      )
    );

    $objTmp = (object) array('array' => array());

    array_walk_recursive($myArray, create_function('&$v, $k, &$t', '$t->array[] = $v;'), $objTmp);

    print_r (array_unique($objTmp->array));

/* output
Array
(
    [0] => 9
    [1] => 13
    [2] => 2
    [3] => 11
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 12
    [8] => 1
    [9] => 7
    [10] => 10
    [11] => 8
    [12] => 3
    [20] => 14
)

*/

Thank you..

Upvotes: 0

Ashu
Ashu

Reputation: 1320

//Here 1st you have to Merge array & then remove duplicate entry

<?php
$main_array=array("0"=>array(1,2,3,4),"1"=>array(2,6,4),"2"=>array(2,8,5));

$temp_array=array();
for($i=0;$i<count($main_array);$i++)
{
$temp_array=array_merge($temp_array,$main_array[$i]);
}

//var_dump($temp_array);
$final_array=array_unique($temp_array);

echo "array:";
var_dump($final_array);
?>

Upvotes: 0

Deep 3015
Deep 3015

Reputation: 10075

$arr;//Your array
$final_arr=array();
foreach($arr as $r){
$r=array_unique($r);
$final_arr=array_merge($final_arr,$r);
}

$final_arr=array_unique($final_arr);
print_r($final_arr);

see it live ideone

Upvotes: 0

M A SIDDIQUI
M A SIDDIQUI

Reputation: 2205

<?php

$data; //Your array
$data_set = array_values($data);
$required_data = [];
for ($i=0; $i< count($data_set); $i++) {
    $required_data = array_merge($required_data, $data_set[$i]);
    unset($data_set[$i]);
}
var_dump($required_data);

This will solve the issue

Upvotes: 0

Related Questions