0

I have already asked a similar question, but they seemed to misunderstand me. I promise I will learn to ask more detailed questions. It is necessary to display this multidimensional array, but without repeating

array (size=3)
  0 => 
    array (size=3)
      'cards1' => 
        array (size=3)
          0 => string 'A' 
          1 => string 'b' 
          2 => string 'c'
      'cards2' => 
        array (size=3)
          0 => string 'A' 
          1 => string 'e' 
          2 => string 'd'
      'cards3' => 
        array (size=3)
         0 => string  'A' 
          1 => string 'o' 
          2 => string 'l'
  1 => 
    array (size=3)
      'cards1' => 
        array (size=3)
         0 => string 'A' 
         1 => string 'b' 
         2 => string 'c'
      'cards2' => 
        array (size=3)
          0 => string 'A' 
          1 => string 'r' 
          2 => string 'c'
      'cards3' => 
        array (size=3)
         0 => string 'A' 
          1 => string 'bbb' 
          2 => string 'yyy'
  2 => 
    array (size=3)
      'cards1' => 
        array (size=3)
         0 => string 'A' 
          1 => string 'bbb' 
          2 => string 'ggg'
      'cards2' => 
        array (size=1)
          0 => string 'A' 
      'cards3' => 
        array (size=1)
          0 => string 'A' 

result

array (size=3)
  0 => 
    array (size=3)
      'cards1' => 
        array (size=3)
          0 => string  'A' 
          1 => string 'b' 
          2 => string 'c'
      'cards2' => 
        array (size=2)
          1 => string 'e' 
          2 => string 'd'
      'cards3' => 
        array (size=2)
          1 => string 'o' 
          2 => string 'l'
 1 => 
    array (size=3)
    'cards1' => 
      array (size=1)
          0 => string  'A' 
      'cards2' => 
        array (size=1)
          1 => string 'r' 
      'cards3' => 
        array (size=3)
          1 => string 'bbb' 
          2 => string 'yyy'
  2 => 
    array (size=3)
     'cards1' => 
      array (size=1)
          0 => string  'WWW' 
        array (size=3)
          2 => string 'ggg'
      'cards2' => 

      'cards3' => 

There should be no repetition in the array. in the array can be 2 and 4 and 10 elements.

and array size can be 4 , 10 , 100

Josef
  • 39
  • 7
  • But it is written on what index. he deletes where the mail is, but I need to pass on all three – Josef Jul 29 '19 at 09:08
  • If you want us to help then make it easy for us to help. Posting print_r or var_dumps of an array is not making it easy for us. Use var_exprot or json_encode. – Andreas Jul 29 '19 at 09:16
  • Just put each value you output into a flat array, and then before you output the next value, check if it is contained in that flat array already using `in_array` …? – misorude Jul 29 '19 at 09:17
  • So I tried, but it does not work ` $has = array(); $output = array(); foreach ( $newUser as $data ){ if ( !in_array($data['cards1'][1], $has) ) { $has[] = $data['cards1'][1]; $output[] = $data; } } ` – Josef Jul 29 '19 at 09:20
  • there one by one, and I need so that in 1 2 and in cards1 cards2 – Josef Jul 29 '19 at 09:20

3 Answers3

0

Apply a basic foreach loop. Loop over all values and maintain an associative array where the values like 'A','b' ..etc are keys of this associative array. I call it $set in my code. Now, if the value exists in the $set with isset check, unset it, else add the current value in iteration to set. Note that I have used & to edit the same reference of the array rather than a cloned one.

<?php

$arr = [
   [
        'cards1' => [
            'A',
            'b',
            'c'
        ],
        'cards2' => [
            'A',
            'e',
            'd'
        ],
        'cards3' => [
            'A',
            'o',
            'l'
        ]
    ],
    [
         'cards1' => [
            'A',
            'b',
            'c'
        ],
        'cards2' => [
            'A',
            'r',
            'c'
        ],
        'cards3' => [
            'A',
            'bbb',
            'yyy'
        ]
    ],
    [
         'cards1' => [
            'A',
            'bbb',
            'ggg'
        ],
        'cards2' => [
            'A',
        ],
        'cards3' => [
            'A',
        ]    
    ]
];

$set = [];

foreach($arr as &$data){
    foreach($data as &$cards){
        foreach($cards as $index => $value){
            if(isset($set[$value])) unset($cards[$index]);
            else  $set[$value] = true;
        }
    }
}

print_r($arr);

Demo: https://3v4l.org/5vp0n

Update:

<?php

$arr = [
   [
        'cards1' => [
            'A',
            'b',
            'c'
        ],
        'cards2' => [
            'A',
            'e',
            'd'
        ],
        'cards3' => [
            'A',
            'o',
            'l'
        ]
    ],
    [
         'cards1' => [
            'A',
            'b',
            'c'
        ],
        'cards2' => [
            'A',
            'r',
            'c'
        ],
        'cards3' => [
            'A',
            'bbb',
            'yyy'
        ]
    ],
    [
         'cards1' => [
            'B',
            'bbb',
            'B'
        ],
        'cards2' => [
            'B',
        ],
        'cards3' => [
            'B',
        ]    
    ]
];

$set = [];
$except = [];


foreach($arr as $data){
    foreach($data as $key => $cards){
        if($key === 'cards1'){
            $except[$data[$key][0]] = true;
            break;
        }
    }
}

foreach($arr as &$data){
    foreach($data as $key => &$cards){
        foreach($cards as $index => $value){
            if(isset($set[$value]) && !isset($except[$value])) unset($cards[$index]);
            else  $set[$value] = true;
        }
    }
}


print_r($arr);

Demo: https://3v4l.org/14LHh

As discussed in the comments about the new requirement, we first collect all values of $cards1[0] in each sub array and store them in $except as keys. Now, we iterate over the array values again and unset only those which aren't found in $except and which are duplicates otherwise.

nice_dev
  • 17,053
  • 2
  • 21
  • 35
0

Use array_unique($your_array). It will filter duplicated values. Hope you find the best result.

donjuedo
  • 2,475
  • 18
  • 28
-1

Try with below code
Suppose your array is $result_arr

$final_arr = $val_arr = [];
foreach ($resul_arr as $key => $value) {
    foreach ($value as $k => $v) {
        $new_arr = [];
        foreach ($v as $s) {
            if (!in_array($s, $val_arr)) {
                $new_arr[] = $s;
            }
            $val_arr[] = $s;
        }
        $final_arr[$key][$k] = $new_arr;
        $val_arr = array_unique($val_arr);
    }
}
echo "Final Array is";
var_dump($final_arr);
Kirtee
  • 141
  • 9
  • Cannot use [] for reading $final_arr = $val_arr []; – Josef Jul 29 '19 at 09:25
  • Just updated the answer. Forgot to add "=" – Kirtee Jul 29 '19 at 09:26
  • no, it does not work, it still leaves repeated values – Josef Jul 29 '19 at 09:28
  • @josef I edited again there is small change. Could you please try again with edited code? – Kirtee Jul 29 '19 at 09:52
  • oooooo it's works, thanks very match . )))and how can I do that to zero index he did not take into account, please. so that it does not touch anywhere cards1[0] – Josef Jul 29 '19 at 09:57
  • can you mark my answer as correct if it works for you? Also I'm not getting zero index, you mean you want all elements are "cards1" ? – Kirtee Jul 29 '19 at 10:08
  • no no, you didn't understand. I here thought and understood that everything should be compared and deleted repetition EXCEPT cards1[0] – Josef Jul 29 '19 at 10:10