0

I have an associative array like this

  $arrz1=Array([sss] => Array
    (
        [0] => 315
        [1] => 330
        [2] => 345
        [3] => 315
        [4] => 330
        [5] => 345
        [6] => 360
        [7] => 315
        [8] => 330
        [9] => 345
        [10] => 360
        [11] => 375
    )

[fff] => Array
    (
        [0] => 315
        [1] => 330
        [2] => 345
        [3] => 315
        [4] => 330
        [5] => 345
        [6] => 360
        [7] => 315
        [8] => 330
        [9] => 345
        [10] => 360
        [11] => 375
        [12] => 750
        [13] => 765
        [14] => 780
    )

)

I want to achieve this.Provided array size can variate and every next should not have the previous all element :

Array([0] => Array
    (
        [0] => Array
            (
                [0] => 315
                [1] => 330
                [2] => 345
                [3] => 315
                [4] => 330
                [5] => 345
                [6] => 360
                [7] => 315
                [8] => 330
                [9] => 345
                [10] => 360
                [11] => 375
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [0] => 750
                [1] => 765
                [2] => 780
            )

    )

Need to figure out the best least time complexity feasible solution.So ,the result fetched is fast enough to achieve result in respective deadline.So far i have tried following

    $array_key = array(); 
    $array_val = array();
    $mult = array();


    foreach ($arrz1 as $key => $val) {

            $diff = array();
            foreach($val as $val1)
            {
                if(!in_array($val1, $array_val))
                {

                    $diff[] = $val1;
                }
            }
        if(!in_array($key, $array_key))
        {
            $array_key[] = $key;

            //print_r($diff);
            if(!empty($diff))
            {
                $mult[] = array($diff);
                foreach($val as $value)
                {
                      $array_val[] = $value;

                }
            }
            else
            {
                $mult[] = array($val);
                foreach($val as $value)
                {

                        $array_val[] = $value; 
                }
            }
        }
     } 

The result is correct but time is too much

3
  • 2
    And what have you tried so far Commented Jul 1, 2016 at 11:02
  • 1
    @RiggsFolly I have added the code Commented Jul 1, 2016 at 11:08
  • Use array_diff() or array_diff_assoc() depending on your requirements. Commented Jul 1, 2016 at 11:09

2 Answers 2

3

Consider the following approach using array_diff and array_values functions:

// $arr is your initial array
$items = array_values($arr);

foreach ($items as $k => &$item) {
    if ($k != 0) $item = array_values(array_diff($item, $items[$k-1]));
}

print_r($items);

The output:

Array
(
    [0] => Array
        (
            [0] => 315
            [1] => 330
            [2] => 345
            [3] => 315
            [4] => 330
            [5] => 345
            [6] => 360
            [7] => 315
            [8] => 330
            [9] => 345
            [10] => 360
            [11] => 375
        )

    [1] => Array
        (
            [0] => 750
            [1] => 765
            [2] => 780
        )
)
Sign up to request clarification or add additional context in comments.

5 Comments

This solution seems to work but i need an indexed array rather $items[1] is indexed from 12 to 14 intead of 1 to 4.
Check out second code snippet of question .Section 2 defining what i want to achieve.To reach an element i have to :$arr[0][0][0] but rather in yours i have to $items[0][0].Get the difference
@Lucky, I'm glad that you were able to fit my solution to your final needs. Thanks. But, I see no benefit of adding an additional nested array level as shown in your expected example
I am still in search of a solution.Kindly help.
Anyway, array_diff will return a single array. There's no essential need for a nested level
0

So a faster way would be to make use of array_diff. You can hold the current state which contains the already indexed elements and continue comparing the new array to the state.

This will work, is fast and is easy to understand.

<?php

$arr = 
[
    'sss' => [315, 330, 345, 125],
    'fff' => [315, 330, 345, 125, 750, 756, 780],
    'eee' => [330, 345, 220, 750]
];

$state = [];

foreach($arr as $key => $item)
{
    $arr[$key] = array_diff($item, $state);
    $state += $arr[$key];
}

var_dump($arr);

Which will result in:

array(3) {
  'sss' =>
  array(4) {
    [0] =>
    int(315)
    [1] =>
    int(330)
    [2] =>
    int(345)
    [3] =>
    int(125)
  }
  'fff' =>
  array(3) {
    [4] =>
    int(750)
    [5] =>
    int(756)
    [6] =>
    int(780)
  }
  'eee' =>
  array(1) {
    [2] =>
    int(220)
  }
}

Node: If have added the item eee to show that the order of the items inside an element wont matter.

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.