3

I have an array and want to remove the related value form array.

Ex.

In array [0] have 1/2/3 and [1] have 1/2/3/4 then [0] is a related to [1] so remove [0] which have 1/2/3 from the array.

Another Example is

Ex.

[2] have 1/2/5, [3] have 1/2/5/6 and [4] have 1/2/5/6/7 then [2] and [3] depends on [4] so remove [2] and [3] both array form array.

For more details please check below example.

Array
(
    [0] => Array
        (
            [id] => 1/2/3
        )

    [1] => Array
        (
            [id] => 1/2/3/4
        )

    [2] => Array
        (
            [id] => 1/2/5
        )

    [3] => Array
        (
            [id] => 1/2/5/6
        )

    [4] => Array
        (
            [id] => 1/2/5/6/7
        )
)

Want Output :

Array
(

    [0] => Array
         (
            [id] => 1/2/3/4
        )


    [1] => Array
        (
            [id] => 1/2/5/6/7
        )
)

I don't have an idea how can I do that. Is it possible?

5
  • 1
    Are the numbers always in order? Like 1/2/6/5 and 1/2/5/6/7? Commented Jul 6, 2018 at 13:18
  • Yes, @Eddie then numbers always in order. Commented Jul 6, 2018 at 13:20
  • Like 1/2/3, 1/2/3/4 Commented Jul 6, 2018 at 13:21
  • @Eddie Is it possible? Commented Jul 6, 2018 at 13:23
  • HI @dan08, that are all ids value. [0] have 1/2/3 and [1] have 1/2/3/4 so 1/2/3 have already used in 1/2/3/4 so do not require in an array. I think you can getting now what I m saying. Commented Jul 6, 2018 at 13:30

2 Answers 2

1

Assuming that the array is in order, you can array_reverse the array. Use array_reduce to loop thru the array, Use strpos to check position of the first occurrence of a substring in a string.

$arr = //your array
$result = array_reduce( array_reverse( $arr ), function( $c, $v ){
    if ( count( $c ) === 0 ) $c[] = $v;
    else if ( count( $c ) && strpos( $c[0]['id'] , $v['id'] ) === false ) array_unshift( $c, $v );      
    return $c;
}, array());

echo "<pre>";
print_r( $result );
echo "</pre>";

This will result to:

Array
(
    [0] => Array
        (
            [id] => 1/2/3/4
        )

    [1] => Array
        (
            [id] => 1/2/5/6/7
        )

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

1 Comment

Yes @Eddie Thanks It working properly with proper code.
1

Of course it is possible. You need to sort the elements first and then it is very straightforward.

<?php

$input = [
  [ "id" => "1/2/3" ],
  [ "id" => "1/2/3/4" ],
  [ "id" => "1/2/5" ],
  [ "id" => "1/2/5/6" ],
  [ "id" => "1/2/5/6/7" ]
];

//firstly sort this array in reverse order
//like so 1/2/3/4 is before 1/2/3
usort(
  $input,
  function($e1, $e2) {
    return $e2["id"] <=> $e1["id"];
  }
);

$output = array_reduce(
  $input,
  function($out, $el) {
    if (empty($out)) {
      $out[] = $el;
    } else {
      $lastEl = $out[count($out) - 1];
      //we add element to the result array
      //only if actual last element doesn't begin with it 
      if (strpos($lastEl['id'], $el['id']) !== 0) {
        $out[] = $el;
      }
    }
    return $out;
  },
  []
);

var_dump($output);

1 Comment

Thanks, @Karol Samborski.

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.