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.

0

6 Answers 6

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.

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

Comments

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);

Comments

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
)

Comments

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.

Comments

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

3 Comments

Indeed, but the rest of the answer is, so what's the problem?
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.
You can't add readable code to a comment. But of cource you're always welcome to edit and improve the answer, go ahead.
-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

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.