Dynamic Table from PHP Array - php

I am trying to display a multidimension php array as a table... can someone help?
This is my array:
Array
(
[1] => Array
(
[0] => Array
(
[c1] => UA07
[s1] => 6
[c2] => Ultimate Force
[s2] => 8
)
[1] => Array
(
[c1] => UF HEROES
[s1] => 6
[c2] => OLD School
[s2] => 4
)
[2] => Array
(
[c1] => Winners 05
[s1] => not_played
[c2] => World XI
[s2] => not_played
)
[3] => Array
(
[c1] => Outlaw
[s1] => 4
[c2] => UWK
[s2] => 3
)
)
[2] => Array
(
[0] => Array
(
[c1] => Ultimate Force
[s1] => 2
[c2] => UF HEROES
[s2] => 4
)
[1] => Array
(
[c1] => BY
[s1] => 0
[c2] => Outlaw
[s2] => 0
)
)
[3] => Array
(
[0] => Array
(
[c1] => UF HEROES
[s1] => 5
[c2] => Outlaw
[s2] => 1
)
)
)
This array currently contains 3 rounds, but could in theory contain more... and within each round it contains each game, and within each game it contains the results of those games...
C1/C2 are competitors and S1 / S2 are the scores...
How do I display each array in a column and move on to the next array and show them in columns as well.. lining them up like a tournament bracket
Any thoughts would be very helpful.
I am trying to achieve a format like this:
http://new.playdat.com/tournament-results.php "Bracket"

As others have said in the comments, you need a nested loop. Since you didn't specify a table format, I just created an example. You can run this at http://writecodeonline.com/php/
/** Simulating your data **/
$rounds = array();
$rounds[1][] = array('c1' => 'UA07', 's1' => 6, 'c2' => 'Ultimate Force', 's2' => 8);
$rounds[1][] = array('c1' => 'UF HEROES', 's1' => 6, 'c2' => 'Old School', 's2' => 4);
$rounds[2][] = array('c1' => 'Ultimate Force', 's1' => 2, 'c2' => 'UF HEROES', 's2' => 4);
$rounds[2][] = array('c1' => 'BY', 's1' => 0, 'c2' => 'Outlaw', 's2' => 0);
$table = <<<EOD
<table>
<thead>
<tr>
<th>Round</th>
<th>C1</th>
<th>S1</th>
<th>C2</th>
<th>S2</th>
</tr>
</thead>
<tbody>
EOD;
foreach ($rounds as $roundNum=> $round){
foreach ($round as $game) {
$table .= "<tr>";
$table .= "<td>{$roundNum}</td>";
$table .= "<td>{$game['c1']}</td>";
$table .= "<td>{$game['s1']}</td>";
$table .= "<td>{$game['c2']}</td>";
$table .= "<td>{$game['s1']}</td>";
$table .= "</tr>";
}
}
$table .= <<<EOD
</tbody>
</table>
EOD;
echo $table;

Dynamic tournament bracket:
Your array:
$rounds = array(
//Round 1
array(
array(
'c1' => 'UA07 ',
's1' => 6 ,
'c2' => 'Ultimate Force ',
's2' => 8 ,
),
array(
'c1' => 'UF HEROES',
's1' => 6,
'c2' => 'OLD School',
's2' => 4,
),
array(
'c1' => 'Winners 05 ',
's1' => 'not_played',
'c2' => 'World XI',
's2' => 'not_played',
),
array(
'c1' => 'Outlaw',
's1' => 4,
'c2' => 'UWK',
's2' => 3,
),
),
//Round 2
array(
array(
'c1' => 'Ultimate Force',
's1' => 2,
'c2' => 'UF HEROES',
's2' => 4,
),
array(
'c1' => 'BY',
's1' => 0,
'c2' => 'Outlaw',
's2' => 0,
),
),
//Round 3
array(
array(
'c1' => 'UF HEROES',
's1' => 5,
'c2' => 'Outlaw',
's2' => 1,
),
)
);
The generator:
$roundCount = 1;
$totalRounds = count($rounds);
echo '<table border="1"><tr>';
for($i = 1;$i <= $totalRounds;$i++)
{
echo '<td>Round'.$i.'</td>';
}
echo '</tr><tr>';
foreach($rounds as $round)
{
$matches = count($round);
echo '<td>';
foreach($round as $match)
{
echo $match['c1'].' - '.$match['s1'].'<br>';
echo $match['c2'].' - '.$match['s2'].'<br><hr>';
}
$roundCount++;
echo '</td>';
}
echo '</tr></table>';
Results:
Generator 2:
$roundCount = 1;
$totalRounds = count($rounds);
echo '<table border="1"><tr>';
for($i = 1;$i <= $totalRounds;$i++)
{
echo '<td>Round'.$i.'</td>';
}
echo '</tr><tr>';
foreach($rounds as $round)
{
$matches = count($round);
echo '<td>';
foreach($round as $match)
{
echo '<table border="1">';
echo '<tr><td>'.$match['c1'].'</td><td>'.$match['s1'].'</td></tr>';
echo '<tr><td>'.$match['c2'].'</td><td>'.$match['s2'].'</td></tr>';
echo '</table>';
}
$roundCount++;
echo '</td>';
}
echo '</tr></table>';
Generator with winner:
$roundCount = 1;
$totalRounds = count($rounds);
echo '<table border="1"><tr>';
for($i = 1;$i <= $totalRounds;$i++)
{
echo '<td>Round'.$i.'</td>';
}
echo '<td>Winner</td>';
echo '</tr><tr>';
foreach($rounds as $round)
{
$matches = count($round);
echo '<td>';
if($roundCount == $totalRounds)
{
if($round[0]['s1'] > $round[0]['s2']) {
$finalist = array($round[0]['c1'],$round[0]['s1']);
} else {
$finalist = array($round[0]['c2'],$round[0]['s2']);
}
}
foreach($round as $match)
{
echo '<table border="1">';
echo '<tr><td>'.$match['c1'].'</td><td>'.$match['s1'].'</td></tr>';
echo '<tr><td>'.$match['c2'].'</td><td>'.$match['s2'].'</td></tr>';
echo '</table>';
}
$roundCount++;
echo '</td>';
}
echo '<td>';
echo '<table border="1">';
echo '<tr><td>'.$finalist[0].'</td><td>'.$finalist[1].'</td></tr>';
echo '</table>';
echo '</td>';
echo '</tr></table>';

Related

Implode multidimensional array with different glue in php

I have array like below:
Array
(
[22] => Array
(
[0] => 60
[29] => Array
(
[0] => 6
)
[30] => Array
(
[0] => 5
[1] => 8
)
[31] => Array
(
[0] => 7
[1] => 9
[2] => 14
[3] => 26
)
)
[23] => 12
[35] =>10
[42] =>22
)
now i want to implode array like
60[6][5||8][7||9||14||26]|12|10|22
I have tried below code:
$arr = array_map(function($el){ return $el['tag_id']; }, $arr);
$str = implode(',', $arr);
But it is not implode with required glue
How can i do it?
you can use this code
<?php
$a= Array(
22 => Array(
0 => 60,
29 => Array(
0 => 6
),
30 => Array
(
0 => 5,
1 => 8
),
31 => Array
(
0 => 7,
1 => 9,
2 => 14,
3 => 26
),
),
23 => 12,
35 =>10,
42 =>22,
);
$string='';
foreach($a as $arr){
if(is_array($arr)){
foreach($arr as $array){
if(is_array($array)){
$string .= '['.implode("||",$array).']';
}else{
if($string!==''){ $string .= '|';}
$string .= $array;
}
}
}else{
if($string!==''){ $string .= '|';}
$string .= $arr;
}
}
echo $string;die;
?>
Out put wil be
60[6][5||8][7||9||14||26]|12|10|22
Desired result without foreach.
$array = [
22 => [
0 => 60,
29 => [
0 => 6
],
30 => [
0 => 5,
1 => 8
],
31 => [
0 => 7,
1 => 9,
2 => 14,
3 => 26
]
],
23 => 12,
35 => 10,
42 => 22
];
$result = implode('|', array_map(function($item)
{
return is_array($item) // convert sub array into string
? implode('', array_map(function($inner_item)
{
return is_array($inner_item) // convert inner array into string
? '[' . implode('||', $inner_item) . ']'
: $inner_item;
}, $item))
: $item;
}, $array));
var_dump($result);
So, we have 3 types of delimiters: '|' - first level, ''(empty) - second level, '||' - third level.
You can use foreach and implode to achieve your pattern:
<?php
header('Content-Type: text/plain');
$arr = array();
$arr22 = array();
$arr22[0] = 60;
$arr22[29] = array(6);
$arr22[30] = array(5,8);
$arr22[31] = array(7,9,14,26);
$arr[22] = $arr22;
$arr[23] = 12;
$arr[35] = 10;
$arr[42] = 22;
$output = '';
foreach($arr as $entry) {
if(!is_array($entry)) {
$output .= $entry;
} else {
// array
foreach($entry as $inner) {
if(is_array($inner)) {
$output .= '[' . implode('||', $inner) . ']';
} else {
$output .= $inner;
}
}
}
$output .= '|';
}
echo substr($output, 0, strlen($output) - 1);
?>
which outputs:
60[6][5||8][7||9||14||26]|12|10|22
This should work for you:
Here the glue is configurable as you desired and this logic is built on the recursive call hence this will work for any level of multidimensional array:
<?php
$arrVal = array (
'22' => array(
'0' => 60,
'29' => array(
'0' => 6
),
'30' => array (
'0' => 5,
'1' => 8
),
'31' => array (
'0' => 7,
'1' => 9,
'2' => 14,
'3' => 26
)
),
'23' => 12,
'35' => 10,
'42' => 22
);
//60[6][5||8][7||9||14||26]|12|10|22
$constructedValue = "";
$glue = "||";
echo $constructedValue = implodeMultiArr($arrVal,$glue);
function implodeMultiArr($arrVal,$glue) {
$i = 0;
$constructedValue = "";
foreach ( $arrVal as $k=>$v) {
if ( is_array($v) ) {
$constructedValue .= !empty($constructedValue) ? "[".implodeMultiArr($v,$glue)."]" : implodeMultiArr($v,$glue)."]" ;
} else {
$constructedValue .= !empty($constructedValue) ? $glue.$v : $v ;
}
$i++;
}
return $constructedValue;
}

Multidimensional Array to table cells

I have an array and I am trying to output the information into a table that is 4 cells wide. The formatting of the table works great, but I am not able to echo the array information. Anybody knows how it can be done?
Array looks like this:
[1] => Array
(
[photo_id] => 76
[photo_name] => 1284d830b198f21ee851334ab87140e1
)
[2] => Array
(
[photo_id] => 75
[photo_name] => 6ef145dd3f52e807f3d7afb3810a2ef8
)
[3] => Array
(
[photo_id] => 74
[photo_name] => f116f3a778d5e900a9e4f7b60c530b67
)
[4] => Array
(
[photo_id] => 73
[photo_name] => 87b2091970c85254391f1245f1826604
)
[5] => Array
(
[photo_id] => 72
[photo_name] => 549c75f04964d219974d4ceffa039e89
)
)
PHP Code:
echo "<table border=\"1\">\n";
for ($counter=0; $counter<$results; ) {
echo "<tr>\n";
for ($colcount=0; $colcount<4 AND $counter<$results; $colcount++,$counter++) {
echo "<td>1</td>";
}
if ($counter == $results) {
for ($extra = ($results % 4); $extra<4 AND $extra!=0; $extra++) {
echo "<td>0</td>";
}
}
echo "\n</tr>\n";
}
echo "</table>";
I only see three cells but here is how you could do it.
<?php
$test = array(1 => array('photo_id' => '76', 'photo_name' => '1284d830b198f21ee851334ab87140e1'),
'2' => array('photo_id' => '75', 'photo_name' => '6ef145dd3f52e807f3d7afb3810a2ef8'),
'3' => array('photo_id' => '74', 'photo_name' => 'f116f3a778d5e900a9e4f7b60c530b67'),
4 => array('photo_id' => '73', 'photo_name' => '1284d830b198f21ee851334ab87140e1'),
'5' => array('photo_id' => '72', 'photo_name' => '6ef145dd3f52e807f3d7afb3810a2ef8'),
'6' => array('photo_id' => '71', 'photo_name' => 'f116f3a778d5e900a9e4f7b60c530b67'),
7 => array('photo_id' => '70', 'photo_name' => '1284d830b198f21ee851334ab87140e1'),
'8' => array('photo_id' => '69', 'photo_name' => '6ef145dd3f52e807f3d7afb3810a2ef8'),
'9' => array('photo_id' => '68', 'photo_name' => 'f116f3a778d5e900a9e4f7b60c530b67')
);
echo '<table border="1">' . "\n";
echo '<tr><td>ID</td><td>PhotoId</td><td>Location/name</td></tr>' . "\n";
$count = 0;
foreach($test as $id => $photoinfo) {
foreach($photoinfo as $key => $value) {
if ($count % 4 == 0 && !empty($pid)) {
echo '<tr>';
}
if($key == 'photo_id') {
$pid = $value;
}
if ($key == 'photo_name') {
$name = $value;
}
if(!empty($pid) && !empty($name)) {
echo "<td>" . $pid . " " . $name . "</td>\n";
$count++;
if ($count % 4 == 0) {
echo '</tr>';
}
unset($name, $pid);
}
}
}
if ($count % 4 != 0) {
//we finished the table generation with less than 4 cells
while($count % 4 != 0) {
echo '<td>empty cell</td>';
$count++;
}
echo '</tr>';
}
echo "</table>\n";
Output:
<table border="1">
<tr><td>ID</td><td>PhotoId</td><td>Location/name</td></tr>
<tr><td>76 1284d830b198f21ee851334ab87140e1</td>
<td>75 6ef145dd3f52e807f3d7afb3810a2ef8</td>
<td>74 f116f3a778d5e900a9e4f7b60c530b67</td>
<td>73 1284d830b198f21ee851334ab87140e1</td>
</tr><tr><td>72 6ef145dd3f52e807f3d7afb3810a2ef8</td>
<td>71 f116f3a778d5e900a9e4f7b60c530b67</td>
<td>70 1284d830b198f21ee851334ab87140e1</td>
<td>69 6ef145dd3f52e807f3d7afb3810a2ef8</td>
</tr><tr><td>68 f116f3a778d5e900a9e4f7b60c530b67</td>
<td>empty cell</td><td>empty cell</td><td>empty cell</td></tr></table>

Creating select option value list from an array

I have an array like this (stored in the $optDir variable):
Array
(
[0] => Array
(
[id] => 8
[title] => Atasan Pria
[idparent] => 5
)
[1] => Array
(
[id] => 5
[title] => Fashion
[idparent] => 0
)
[2] => Array
(
[id] => 7
[title] => Motor
[idparent] => 0
)
[3] => Array
(
[id] => 6
[title] => Mobil
[idparent] => 0
)
[4] => Array
(
[id] => 9
[title] => Hem
[idparent] => 8
)
)
And i have PHP Codes like this :
function optCatAds($pos=0, $level=0){
global $optDir; $opt = "";
$n = count($optDir);
for($i=0;$i<$n;$i++){
$l = $level*3;
$sign=""; for ($z=0;$z<=$l;$z++){$sign.=" ";}
if($optDir[$i]['idparent']==$pos){
$opt .= '<option value="'.$optDir[$i][id].'">'.$sign.$optDir[$i][title].'</option>';
optCatAds($optDir[$i][id], $level + 1);
}
}
return $opt;
}
When i call the optCatAds function, give output like below:
<option value="5">Fashion</option>
<option value="6">Mobil</option>
<option value="7">Motor</option>
But, I want to made output like below :
<option value="5">Fashion</option>
<option value="8"> Atasan Pria</option>
<option value="9"> Hem</option>
<option value="6">Mobil</option>
<option value="7">Motor</option>
Conditions :
Fashion, Mobil, Motor <-- parent
Fashion have child Atasan Pria
Atasan Pria have child Hem
Can someone help me? Thank to your help.
This will work fine for you
$optDir = Array(Array("id"=>8,"title"=>"Atasan Pria","idparent"=>5),
Array("id"=>5,"title"=>"Fashion","idparent"=>0),
Array("id"=>7,"title"=>"Motor","idparent"=>0),
Array("id"=>6,"title"=>"Mobil","idparent"=>0),
Array("id"=>9,"title"=>"Hem","idparent"=>8)
);
function optCatAds($pos=0, $level=0)
{
global $optDir;$opt;
for($i=0;$i<count($optDir);$i++)
{
$l = $level*3;
$sign="";
for ($z=0;$z<$l;$z++){$sign.=" ";}
if($optDir[$i]['idparent']==$pos)
{
$opt .= '<option value="'.$optDir[$i]['id'].'">'.$sign.$optDir[$i]['title'].'</option>';
$opt .= optCatAds($optDir[$i]['id'], $level + 1);
}
}
return $opt;
}
$res = optCatAds($pos=0, $level=0);
echo "<select>{$res}</select>";
Can you try foreach instead of for loop ,
foreach($optDir as $k=>$value){
$opt .= '<option value="'.$value->id.'">'.$sign.$value->title.'</option>';
}
Replace $optDir[$i][title] with $optDir[$i]['title']
and $optDir[$i][id] with $optDir[$i]['id']
Here you are making recursive call so you can not get complete
function optCatAds($pos=0, $level=0){
global $optDir; $opt = "";
foreach($optDir as $each){
if($each['id']==8) $level = 2;
if($each['id']==9) $level = 3;
$l = $level*3;
$sign=""; for($z=0;$z<=$l;$z++){$sign.=" ";}
if($each['idparent']==$pos){
$opt .= '<option value="'.$each['id'].'">'.$sign.$each['title'].'</option>';
}
}
return $opt;
}
<?php
$optDir = Array(
Array(
'id' => 8,
'title' => 'Atasan Pria',
'idparent' => 5
),
Array(
'id' => 5,
'title' => 'Fashion',
'idparent' => 0
),
Array(
'id' => 7,
'title' => 'Motor',
'idparent' => 0
),
Array(
'id' => 6,
'title' => 'Mobil',
'idparent' => 0
),
Array(
'id' => 9,
'title' => 'Hem',
'idparent' => 8
)
);
function getOptionsRecursive($idParent = 0, $spaces = '') {
global $optDir;
$s = '';
for($i = 0 ; $i < sizeof($optDir) ; $i++) {
$current = $optDir[$i];
if($current['idparent'] == $idParent) {
$s .= '<option value="'.$current['id'].'">'.$spaces.$current['title'].'</option>';
$s .= getOptionsRecursive($current['id'], $spaces.' ');
}
}
return $s;
};
echo getOptionsRecursive();
?>

PHP, array iteration

i have this array structure:
$ar = [product_info] => Array
(
[pname] => Array
(
[0] => Выделенный сервер DE-22
[1] => Hello 4
[2] => Hello World
)
[pid] => Array
(
[0] => 217
[1] => 342
[2] => 343
)
i want to iterate it like this (in one loop):
foreach ($ar['product_info'] as $item) {
echo $item['pname'];
echo $item['pid']
}
How can i do this? need help
i think a normal for solve it.
$total = count($ar['product_info']['pid']);
for($i=0; $i<$total; $i++){
echo $ar['product_info']['pname'][$i];
echo $ar['product_info']['pid'][$i];
}
<?php
$ar = array(
'product_info' => array(
'pname' => array(
'Product 1',
'Product 2',
'Product 3',
'Product 4 > test <',
),
'pid' => array(
217,
342,
343,
666,
),
),
);
foreach($ar['product_info']['pid'] as $productIndex => $productId)
{
$productName = $ar['product_info']['pname'][$productIndex];
echo '<a href="?product_id=' . intval($productId) . '">';
echo htmlspecialchars($productName);
echo '</a>';
echo '<br />' . PHP_EOL;
}
You need to iterate over the inner arrays, which are int-indexed:
$size=sizeof($ar['product_info']['pname'];
for ($i=0;$i<$size;$i++) {
echo $ar['product_info']['pname'][$i];
echo $ar['product_info']['pid'][$i];
}

How to create a multidimensional array out of an array and perform a filtering?

My goal is to keep all my array values in another array but I don't know how can I do that.
Here's my simple array:
$grand_total = $total + $cost;
$cost_ratio = ($cost/$grand_total) * 100;
$paid_ratio = ($total/$grand_total) * 100;
$info[] = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);
Here's my sample array output
Array
(
[0] => Array
(
[id] => 53
[ratio] => 100
[status] => C
)
[1] => Array
(
[id] => 57
[ratio] => 100
[status] => I
)
[2] => Array
(
[id] => 60
[ratio] => 1.3157894736842
[status] => I
)
After creating an array the next thing to do is to filter this by getting the ratio.
As you have seen above I have a variable $cost_ratio, this variable is the one that will filter my array.
Here's the continuation of my code:
//declare a variable
$var1 = 0;
$var2 = 0;
$var3 = 0;
$var4 = 0;
$var5 = 0;
$var6 = 0;
$var7 = 0;
$var8 = 0;
$var9 = 0;
$var10 = 0;
After that
for($x = 0; $x < sizeof($info); $x++){
$status_info = $info[$x]['status'];
$temp = $info[$x]['ratio'];
/*this will set my filter*/
/*if temp <= 9, i need to put all of my array that has a ratio <= 9*/
if($temp <= 9){
$var1++;
}else if($temp <= 19){
$var2++;
}else if($temp <= 29){
$var3++;
}else if($temp <= 39){
$var4++;
}else if($temp <= 49){
$var5++;
}else if($temp <= 59){
$var6++;
}else if($temp <= 69){
$var7++;
}else if($temp <= 79){
$var8++;
}else if($temp <= 89){
$var9++;
}else{
$var10++;
}
}
And lastly the table for displaying my records
echo "<table border='1'>";
echo "<tr>";
echo "<td>PERCENTAGE RANGE</td>";
echo "<td>VOLUME</td>";
echo "<td>RATIO</td>";
echo "<td>BACKORDERED</td>";
echo "<td>CANCELED</td>";
echo "<td>COD APPROVED</td>";
echo "<td>COD PENDING</td>";
echo "<td>COD SHIPPED</td>";
echo "<td>COMPLETED</td>";
echo "<td>DECLINED</td>";
echo "<td>FAILED</td>";
echo "<td>FRAUD CHECKING</td>";
echo "<td>PAID & WAITING SHIPPING</td>";
echo "<td>PENDING</td>";
echo "<td>SHIPPED</td>";
echo "<td>TEST</td>";
echo "<td>WAITING FOR APPROVAL</td>";
echo "<td>WAITING PAYMENT</td>";
echo "</tr>";
echo "<tr>";
echo "<td>10% - 90%</td>";
echo "<td>".$var1."</td>"; <!-- IF ratio has a value between 1 - 9 go here -->
echo "<td></td>";
echo "<td>".$x1."</td>";
echo "<td>".$x2."</td>";
echo "<td>".$x3."</td>";
echo "<td>".$x4."</td>";
echo "<td>".$x5."</td>";
echo "<td>".$x6."</td>";
echo "<td>".$x7."</td>";
echo "<td>".$x8."</td>";
echo "<td>".$x9."</td>";
echo "<td>".$x10."</td>";
echo "<td>".$x11."</td>";
echo "<td>".$x12."</td>";
echo "<td>".$x13."</td>";
echo "<td>".$x14."</td>";
echo "<td>".$x15."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>20% - 80%</td>"; <!-- IF ratio has a value between 10 - 19 go here -->
echo "<td>".$var2."</td>";
echo "<td></td>";
echo "<td>".$x1."</td>";
echo "<td>".$x2."</td>";
echo "<td>".$x3."</td>";
echo "<td>".$x4."</td>";
echo "<td>".$x5."</td>";
echo "<td>".$x6."</td>";
echo "<td>".$x7."</td>";
echo "<td>".$x8."</td>";
echo "<td>".$x9."</td>";
echo "<td>".$x10."</td>";
echo "<td>".$x11."</td>";
echo "<td>".$x12."</td>";
echo "<td>".$x13."</td>";
echo "<td>".$x14."</td>";
echo "<td>".$x15."</td>";
echo "</tr>";
.
.
Here's what I want to do:
[ratio between 1 - 9 value] = array(
[0] = array(
'id' => 1,
'status' => 'C'
),
[1] = array(
'id' => 3,
'status' => 'D'
),
[2] = array(
'id' => 6,
'status' => 'J'
),
),
[ratio between 10 - 19 value] = array(
[0] = array(
'id' => 1,
'status' => 'C'
),
[1] = array(
'id' => 3,
'status' => 'D'
),
[2] = array(
'id' => 6,
'status' => 'J'
),
),
.
.
.
That's what I want to do but I am having a hard time with that.
You can easily group array by doing like the following:
$array = array(
array( 'id' => 53, 'ratio' => 100, 'status' => 'I'),
array( 'id' => 54, 'ratio' => 50, 'status' => 'C'),
array( 'id' => 56, 'ratio' => 42, 'status' => 'D')
);
// Grouping array by ratio
$group = array();
foreach($array as $value) {
$groupIndex = min($value['ratio'] / 10, 9);
$group[$groupIndex][] = $value;
}
var_dump($group);
Here is the sample output
array (size=3)
9 =>
array (size=1)
0 =>
array (size=3)
'id' => int 53
'ratio' => int 100
'status' => string 'I' (length=1)
5 =>
array (size=1)
0 =>
array (size=3)
'id' => int 54
'ratio' => int 50
'status' => string 'C' (length=1)
4 =>
array (size=1)
0 =>
array (size=3)
'id' => int 56
'ratio' => int 42
'status' => string 'D' (length=1)
Noted that the index 9 means from 90+, 5 means from 50 to 59, and 4 means from 40 to 49. If you are confused with the index convention, here is the full list:
$group[0] <--- from 0 to 9
$group[1] <--- from 10 to 19
$group[2] <--- from 20 to 29
$group[3] <--- from 30 to 39
$group[4] <--- from 40 to 49
$group[5] <--- from 50 to 59
$group[6] <--- from 60 to 69
$group[7] <--- from 70 to 79
$group[8] <--- from 80 to 89
$group[9] <--- from 90+

Categories