P
P
Pavel Goreninov2015-02-01 12:20:01
PHP
Pavel Goreninov, 2015-02-01 12:20:01

How to find value repeated in PHP arrays?

Good afternoon! There are, say, 10 arrays. The task is to find values ​​that occur in at least three arrays and add them to a separate array. How it is better to implement it? Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
FanatPHP, 2015-02-01
@FanatPHP

The correct answer is: stop doing nonsense and finally learn how to work with the database.
The literal answer is: any operations on arrays are done using loops .

A
Alexey Shein, 2015-02-01
@conf

A little advice: always give the context in which you solve the problem, otherwise you can solve some of your invented problems, and the real problem is solved in a completely different way.
Regarding your question - read the documentation , everything is there:

php > $one = ['red']; $two = ['red', 'green', 'blue']; $three = ['red', 'blue'];
php > var_dump(array_intersect($one, $two, $three));
array(1) {
  [0]=>
  string(3) "red"
}

It remains for you to count the number of combinations from 10 to 3 in the loop and use the above function, or tell us what you are still trying to do and then, for sure, there will be a less crutch solution.

X
xmoonlight, 2015-02-01
@xmoonlight

//$arrays - исходный список массивов
$count=array();
$new=array();
foreach ($arrays as $a) {
  foreach ($a as $key=>$i) {
    $count[$key]++;
    if ($count[$key]===3) {$new[$key]=$a;}
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question