-1

I have tables with data, I would like to sort it equally according to the type field in order to get such a result:

1 - http://site1.com
2 - http://sitex.com
3 - http://sites.com
4 - http://site2.com
5 - http://site1.com
1 - http://site2.com
2 - http://site3.com
3 - http://site6.com
4 - http://site1.com
5 - http://siteX.com
1 - http://site2.com
2 - http://site8.com
3 - http://site9.com
4 - http://site1.com
5 - http://site1.com

I want to group the mixed data from the array into equal groups according to the value of the type field, from 1-5. I will add that the array does not always have an equal amount of data for type=X. Sometimes there are more and sometimes less

php script:

<?php

$arr = array(
   array('type' => 1,'url' => 'http://site1.com'),
   array('type' => 4,'url' => 'http://site2.com'),
   array('type' => 1,'url' => 'http://site2.com'),
   array('type' => 2,'url' => 'http://sitex.com'),
   array('type' => 5,'url' => 'http://site1.com'),
   array('type' => 1,'url' => 'http://site2.com'),
   array('type' => 2,'url' => 'http://site3.com'),
   array('type' => 3,'url' => 'http://sites.com'),
   array('type' => 4,'url' => 'http://site1.com'),
   array('type' => 5,'url' => 'http://siteX.com'),
   array('type' => 3,'url' => 'http://site6.com'),
   array('type' => 4,'url' => 'http://site1.com'),
   array('type' => 5,'url' => 'http://site1.com'),
   array('type' => 2,'url' => 'http://site8.com'),
   array('type' => 3,'url' => 'http://site9.com'),
);

foreach ($arr as $a){
    echo $a['type'].' - '.$a['url'].'<br>';
}

/*-- result

1 - http://site1.com
4 - http://site2.com
1 - http://site2.com
2 - http://sitex.com
5 - http://site1.com
1 - http://site2.com
2 - http://site3.com
3 - http://sites.com
4 - http://site1.com
5 - http://siteX.com
3 - http://site6.com
4 - http://site1.com
5 - http://site1.com
2 - http://site8.com
3 - http://site9.com

--*/
?>

Do you have any idea how I can get such a result?

Wojtek
  • 67
  • 1
  • 6
  • So u want data based on type orde sorting ? like 1 , 2 , 3 , 4 ? – TrickStar Oct 23 '18 at 12:35
  • Yes. 1,2,3,4,5,1,2,3,4,5 – Wojtek Oct 23 '18 at 12:37
  • Refer this link : https://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php – TrickStar Oct 23 '18 at 12:39
  • I checked, unfortunately I did not find any solutions that worked in my case. I want to sort the array into equal portions of data (1,2,3,4,5...1,2,3,4,5), not all array (1,1,2,2,3,3...) – Wojtek Oct 23 '18 at 12:48

2 Answers2

1

Like this:

$mapped = [];

foreach ($arr as $a){
    $mapped[$a['type']][] = $a['url'];
}
mblaettermann
  • 1,916
  • 2
  • 17
  • 23
0

Use this code may work for you..

<?php function group_by($key, $data) {
    $result = array();

    foreach($data as $val) {
    if(array_key_exists($key, $val)){
    $result[$val[$key]][] = $val;
    }else{
    $result[""][] = $val;
    }
    }
    
    return $result;
}

$arr = array(
   array('type' => 1,'url' => 'http://site1.com'),
   array('type' => 4,'url' => 'http://site2.com'),
   array('type' => 1,'url' => 'http://site2.com'),
   array('type' => 2,'url' => 'http://sitex.com'),
   array('type' => 5,'url' => 'http://site1.com'),
   array('type' => 1,'url' => 'http://site2.com'),
   array('type' => 2,'url' => 'http://site3.com'),
   array('type' => 3,'url' => 'http://sites.com'),
   array('type' => 4,'url' => 'http://site1.com'),
   array('type' => 5,'url' => 'http://siteX.com'),
   array('type' => 3,'url' => 'http://site6.com'),
   array('type' => 4,'url' => 'http://site1.com'),
   array('type' => 5,'url' => 'http://site1.com'),
   array('type' => 2,'url' => 'http://site8.com'),
   array('type' => 3,'url' => 'http://site9.com'),
);

$byGroup = group_by("type", $arr);

echo "<pre>" . var_export($byGroup, true) . "</pre>";
?>
Pols
  • 13
  • 3