Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Php, get results from 3 or 4 different arrays (by order)

Hi E's, I need to compare 3 different arrays, and create a new array with by order of the values most frequently found. In practice:
//I have 3 arrays:
$array1(1000, 1001, 1002, 1003, 1005);
$array2(1005, 1010, 1011, 1012, 1003);
$array3(1002, 1005);

In the and I need a in this order:
1005 - 3times
1002 - 2times
1003
1000 - 1time
1001
1010
1011
1012

I need a solution that compare 3 or 4 arrays different arrays.
How I do that?

The best regards, JC
0
Pedro Chagas
Asked:
Pedro Chagas
  • 3
  • 2
1 Solution
 
Ray PaseurCommented:
You might want to take these array values and make them into keys.  Then you can create a merged array from the array values, and use an iterator like foreach() to go through each of the values.  As you find each value, add one to the corresponding key.  Then sort the array of keys by values and you have your answer.
0
 
Ray PaseurCommented:
http://www.laprbass.com/RAY_temp_joao.php
<?php // RAY_temp_joao.php
error_reporting(E_ALL);

// SAMPLE DATA FROM THE POST AT EE
$array1 = array(1000, 1001, 1002, 1003, 1005);
$array2 = array(1005, 1010, 1011, 1012, 1003);
$array3 = array(1002, 1005);

// GET ALL THE VALUES INTO ONE ARRAY
$combo  = array_merge($array1, $array2, $array3);

// FLIP VALUES INTO KEYS
$flips  = array_flip($combo);

// ZERO OUT THE COUNTERS
foreach ($flips as $key => $nothing)
{
    $flips[$key] = 0;
}

// COUNT THE ELEMENTS OF THE COMBINED ARRAY
foreach ($combo as $thing)
{
    $flips[$thing]++;
}

// SORT THE ELEMENTS FROM HIGH TO LOW
arsort($flips);

// SHOW THE WORK PRODUCT
echo "<pre>";
print_r($flips);

Open in new window

0
 
Pedro ChagasWebmasterAuthor Commented:
Hi Ray, thanks for your solution.
In your solution I have this in the end (output):
Array
(
    [1005] => 3
    [1002] => 2
    [1003] => 2
    [1012] => 1
    [1011] => 1
    [1010] => 1
    [1001] => 1
    [1000] => 1
)

But what I need is:
Array
(
    [0] => 1005
    [1] => 1002
    [2] => 1003
    [3] => 1000
    [4] => 1001
    [5] => 1010
    [6] => 1011
    [7] => 1012
)

Is possible?

Regards, JC
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi, I add the function array_key in the final of the code, and work:
$final = array_keys($flips);

The best regards, JC
0
 
Ray PaseurCommented:
I thought you wanted to know a count of how many times each array element appeared.  It's necessary to know that in order to sort them by frequency.  But if you do not need that information, then you are right about using array_keys() on the sorted array to remove the counts.  In any case, thanks for the points, ~Ray
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now