0

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

0

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

8 Comments

How [12] => 3 [13] => 3 value 3 comes 2 times
How about [20] => 14 from where does this 20 key comes from ?
Check the array that you get before applying array_unique. You will find the reason.
I need to know if you need unique values or unique keys, or what sort of algorithm are you using for your resultant array?
Well my question just simple from where does this 20 key came from as there is no such key available in given array?
|
0
<?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

Comments

0
$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

2 Comments

is there any way by with we can ignore the use of foearach() loop
@Exception go with the method explained by Thamilan above
0
//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);
?>

Comments

0

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..

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.