0

I want to remove one ore more array element using array_diff_key() with one specific key but the result is different

$a = [
        ['value' => 1, 'text' => '1 Hour'],
        ['value' => 12, 'text' => '12 Hour'],
        ['value' => 24, 'text' => '1 Day'],
    ];

$b = [
        ['value' => 12],    
    ];

$c = array_diff_key($a, $b);

//print_r($c);
//return Array ( [1] => Array ( [value] => 12 [text] => 12 Hour ) [2] => Array ( [value] => 24 [text] => 1 Day ))

Expected result

Array ( 
      [1] => Array ( 
        [value] => 1 
        [text] => 1 Hour ) 
      [2] => Array ( 
        [value] => 24 
        [text] => 1 Day ) 
)

I can get expected result if I set text in $b but in this case I don't want to set text.

Where i'm doing wrong? other solution is wellcome.

Thanks in advance

PS: $b can be one or more element

4
  • Based on what I understand, you simply want to remove the nested array in $a where the value matches that of the incoming array $b, right? Commented Jul 1, 2017 at 10:52
  • $b is an array or just you want to search 12 in text key ? Commented Jul 1, 2017 at 10:53
  • @terry, yes I want to remove $a element which match $b Commented Jul 1, 2017 at 10:58
  • @sahil, $b is array and I want to remove $a element which match $b Commented Jul 1, 2017 at 10:59

3 Answers 3

2

Try below code,

Check thorough loop.

<?php
    $a = [
            ['value' => 1, 'text' => '1 Hour'],
            ['value' => 12, 'text' => '12 Hour'],
            ['value' => 24, 'text' => '1 Day'],
        ];

    $b = [
            ['value' => 12]
        ];
    $c = array();


    function checkExists($array,$value){
        foreach($array as $k=>$values){
            if($values['value']==$value){
                return true;break;
            }
        }
        return false;
    }
    foreach($a as $k=>$v){
        if(checkExists($b,$v['value'])===false){
            $c[$k]=$v;
        }
    }
?>

Output

Array
(
    [0] => Array
        (
            [value] => 1
            [text] => 1 Hour
        )

    [2] => Array
        (
            [value] => 24
            [text] => 1 Day
        )

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

Comments

1

There's no need to author very complicated for loops. You can simply define your own comparator function when using array_udiff():

$a = [
        ['value' => 1, 'text' => '1 Hour'],
        ['value' => 12, 'text' => '12 Hour'],
        ['value' => 24, 'text' => '1 Day'],
    ];

$b = [
        ['value' => 12],    
    ];

function comparator($a, $b)
{
    return $a['value'] - $b['value'];
}

$c = array_udiff($a, $b, 'comparator');
print_r($c);

That should give you the array you expected:

Array (
    [0] => Array (
        [value] => 1
        [text] => 1 Hour
    )
    [2] => Array (
        [value] => 24
        [text] => 1 Day
    )
)

Comments

0

Are you sure you didn't use array_diff()? You are misunderstanding array_diff_keys(); it only works with a single dimensional associative array—i.e., elements with keys. The first dimension of your array does not have any keys.

We can write a simple solution with array_reduce():

$a = [
        ['value' => 1, 'text' => '1 Hour'],
        ['value' => 12, 'text' => '12 Hour'],
        ['value' => 24, 'text' => '1 Day'],
    ];

$b = [
        ['value' => 12]
    ];

function filter($v1,$v2)
{
  global $b;
  var_dump($v1,$v2);
  return ($b[0]['value'] == $v2['value'])
    ? $v1
    : array_merge($v1, [$v2]);
}

print_r(array_reduce($a,"filter",[]));

Short, sweet, and no explicit iterators nor temporary variables.

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.