1

I've an array in php something like below

Array
(
    [0] => Array
        (
            [0] => 40173
            [1] => 514081
            [2] => 363885
            [3] => 891382
        ),
    [1] => Array
        (
            [0] => 40173
            [1] => 5181
            [2] => 385
            [3] => 891382
        )

)

Now I want to remove the parents indexes 0,1... and finally want to get all the values (only unique values).

Thanks.

Manish Jangir
  • 5,329
  • 4
  • 42
  • 75

6 Answers6

6

One possible approach is using call_user_func_array('array_merge', $arr) idiom to flatten an array, then extracting unique values with array_unique():

$new_arr = array_unique(
  call_user_func_array('array_merge', $old_arr));

Demo. Obviously, it'll work with array of any length.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
3
$startArray = Array
(
[0] => Array
    (
        [0] => 40173
        [1] => 514081
        [2] => 363885
        [3] => 891382
    ),
[1] => Array
    (
        [0] => 40173
        [1] => 5181
        [2] => 385
        [3] => 891382
    )

);
//Edited to handle more the 2 subarrays
$finalArray = array();

foreach($startArray as $tmpArray){
    $finalArray = array_merge($finalArray, $tmpArray);
}

$finalArray = array_unique($finalArray);
Andre
  • 170
  • 6
1

Using RecursiveArrayIterator Class

$objarr = new RecursiveIteratorIterator(new RecursiveArrayIterator($yourarray));
foreach($objarr as $v) {
    $new_arr[]=$v;
}
print_r(array_unique($new_arr));

Demo

OUTPUT:

Array
(
    [0] => 40173
    [1] => 514081
    [2] => 363885
    [3] => 891382
    [5] => 5181
    [6] => 385
)
Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0
$new_array = array_merge($array1, $array2);
$show_unique = array_unique($new_array);
print_r($show_unique);

array_merge is merging the array's, array_unique is removinge any duplicate values.

OfirH
  • 651
  • 1
  • 8
  • 19
-1

Try something like this:

$new_array = array();

foreach($big_array as $sub_array) {
    array_merge($new_array, $sub_array);
}

$new_array = array_unique($new_array);

(code not tested, this just a concept)

magnetronnie
  • 505
  • 3
  • 16
  • Indeed, but the rest of the answer is, so what's the problem? – magnetronnie May 24 '14 at 08:04
  • Good SO answer explains what is the problem and solution + shows code. Your one looks more like comment and there is no reason for person reading the "answer" to believe it will solve the problem. If you don't feel typing one sentence explaining your approach - just add your suggestion as comment. – Alexei Levenkov May 25 '14 at 01:33
  • You can't add readable code to a comment. But of cource you're always welcome to edit and improve the answer, go ahead. – magnetronnie May 26 '14 at 05:42
-1

Try this:

$Arr = array(array(40173, 514081, 363885, 891382),
             array(40173,5181, 385,891382));

$newArr = array();

foreach($Arr as $val1)
{

foreach($val1 as $val2)
{  
     array_push($newArr, $val2);
}
}

echo '<pre>';
print_r(array_unique($newArr));

Output:

Array
(
    [0] => 40173
    [1] => 514081
    [2] => 363885
    [3] => 891382
    [5] => 5181
    [6] => 385
)

Refer: https://eval.in/124240

-- Thanks

Anand Solanki
  • 3,419
  • 4
  • 16
  • 27