Jump to content

PHP merging common array values?


Jriker1

Recommended Posts

I have an array from JSON that contains the below values.  If you see the below (first code block), the second item in each line has commonality in a lot of cases with the wdc values further down.  I am trying to create a single array that has only one of each second value in it (don't care about ftc or wdc), and a sum of the common entries.  Could be two, three, four entries with the same name.  So for the below, looking to get:

 

jsob.5970, 1116

jsob.5970cpu, 7

jsob.5970two, 925

 

and so on.  Basically adding like values together and creating a list of how much that item is doing total at any time as the list is dynamically refreshed whenever I refresh the json call (browser refresh).

ftc, jsob.5970, 370 
ftc, jsob.5970cpu, 7 
ftc, jsob.5970two, 427
ftc, jsob.5970twocpu, 7
ftc, jsob.69597950, 320 
ftc, jsob.An1cpu, 14
wdc, jsob.5970, 746 
wdc, jsob.5970two, 498 
wdc, jsob.69597950, 142 
wdc, jsob.An1cpu, 14
wdc, jsob.An3cpu, 7 

Thoughts?  Was going to loop thru putting each item in a new array and if I hit an item that was already in the array add the new number to it, but figured my code would have been convoluted and there is probably an elegant way I don't know of yet to accomplish this.

 

For reference the code to output the above (not with commas that's just for reference), is below:

echo "Workers:<br><table>";
foreach ($obj['workers'] as $key => $vals)
  {
    foreach ($vals as $name => $wrk)
     {
       if ($wrk['hashrate'] != "0" )
         {
           echo "<tr><td>" . $key . "</td>";
           echo "<td>" . $name . "</td>";
           echo "<td>" . $wrk['hashrate'] . "</td></tr>";
         }
     }
  }
echo "</table>";

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/282052-php-merging-common-array-values/
Share on other sites

This is what I ended up doing, not sure if it's the cleanest way or not.

echo "Workers:<br><table>";
foreach ($obj['workers'] as $key => $vals)
  {
    foreach ($vals as $name => $wrk)
     {
       if ($wrk['hashrate'] != "0" )
         {
           echo "<tr><td>" . $key . "</td>";
           echo "<td>" . $name . "</td>";
           echo "<td>" . $wrk['hashrate'] . "</td></tr>";
           if (isset($workersArray[$name]))
             {
               $workersArray[$name] = $workersArray[$name] + $wrk['hashrate'];
             }
           else 
             {
               $workersArray[$name] = $wrk['hashrate'];
             }
         }
     }
  }
echo "</table>";

So placed the isset check and settings in it.

function merge_common_keys(){
$arr = func_get_args();
$num = func_num_args();

$keys = array();
$i = 0;
for($i=0;$i<$num;++$i){
$keys = array_merge($keys, array_keys($arr[$i]));
}
$keys = array_unique($keys);

$merged = array();

foreach($keys as $key){
$merged[$key] = array();
for($i=0;$i<$num;++$i){
$merged[$key][] = isset($arr[$i][$key])?$arr[$i][$key]:null;
}
}
return $merged;
}

Usage:

$merged = merge_common_keys($array1,$array2);

PS. It can work with more than two arrays, just pass as many as you want as next arguments.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.