Multidimensional Array to table cells - php

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>

Related

How do I add <tr> and complete </tr> tag in PHP foreach loop

I have this kind of array in $form_all_data variable.
Array
(
[0] => stdClass Object
(
[id] => 111
[form_id] => 1
[entry_id] => 38
[meta_key] => 6
[meta_value] => Student 1
[item_index] =>
)
[1] => stdClass Object
(
[id] => 112
[form_id] => 1
[entry_id] => 38
[meta_key] => 3
[meta_value] => Not sure
[item_index] =>
)
[2] => stdClass Object
(
[id] => 113
[form_id] => 1
[entry_id] => 38
[meta_key] => 5
[meta_value] => 55
[item_index] =>
)
[3] => stdClass Object
(
[id] => 129
[form_id] => 1
[entry_id] => 43
[meta_key] => 6
[meta_value] => Student 3
[item_index] =>
)
[4] => stdClass Object
(
[id] => 130
[form_id] => 1
[entry_id] => 43
[meta_key] => 3
[meta_value] => Yes
[item_index] =>
)
[5] => stdClass Object
(
[id] => 131
[form_id] => 1
[entry_id] => 43
[meta_key] => 5
[meta_value] => 55
[item_index] =>
)
)
I am showing all this data inside <table>. These entries are differentiated as per entry_id.
I want to add <tr> tag when every time new entry_id is there in the loop and want to complete </tr> tag when every time same entry_id is last in the loop.
Something like this. Please check this image
This means I want to show entry_id 38 data in one <tr> and entry_id 43 data in another <tr>
How can I do this ?
In my code , it is adding <tr> with every new iteration
<?php
$i = 1;
foreach ( $form_all_data as $check_form_all_data_key => $check_form_all_data_value ) { ?>
<tr>
<?php
if ( $check_form_all_data_value->form_id == 1 ) {
if ( $check_form_all_data_value->meta_key == 6 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
if ( $check_form_all_data_value->meta_key == 3 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
} else {
} ?>
</tr>
<?php $i++; } ?>
Here is the working code.
<?php
$form_all_data = [
(object)['entry_id' => 1, 'value' => '1 1', 'type' => '1'],
(object)['entry_id' => 1, 'value' => '1 2', 'type' => '2'],
(object)['entry_id' => 2, 'value' => '2 1', 'type' => '1'],
(object)['entry_id' => 2, 'value' => '2 2', 'type' => '2'],
];
$currentEntryId = null;
$needCloseTr = false;
echo '<table>';
foreach ($form_all_data as $row) {
//is next entry?
if ($currentEntryId !== $row->entry_id) {
//close previous tr
if ($needCloseTr) {
echo '</tr>';
$needCloseTr = false;
} else {
$needCloseTr = true;
}
$currentEntryId = $row->entry_id;
echo '<tr>';
}
echo sprintf('<td>%s</td>', $row->value);
}
//close last tr
if ($needCloseTr) {
echo '</tr>';
}
echo '</table>';
But what you are doing is not correct.
It's better to process the $form_all_data array first and get the data that doesn't need tricky processing, like this:
<?php
$form_all_data = [
(object)['entry_id' => 1, 'value' => '1 1', 'type' => '1'],
(object)['entry_id' => 1, 'value' => '1 2', 'type' => '2'],
(object)['entry_id' => 2, 'value' => '2 1', 'type' => '1'],
(object)['entry_id' => 2, 'value' => '2 2', 'type' => '2'],
];
$result = [];
foreach ($form_all_data as $row) {
$result[$row->entry_id][$row->type] = $row->value;
}
var_dump($result);
Detect entry_id change with as following code
<?php
$i = 1;
$prev_entry_id = 0;
foreach ( $form_all_data as $check_form_all_data_key => $check_form_all_data_value ) { ?>
<?php if($prev_entry_id == 0){
echo "<tr>";
}else if($prev_entry_id != $check_form_all_data_value->entry_id){
echo "</tr><tr>";
} ?>
<?php
if ( $check_form_all_data_value->form_id == 1 ) {
if ( $check_form_all_data_value->meta_key == 6 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
if ( $check_form_all_data_value->meta_key == 3 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
} else {
} ?>
<? $prev_entry_id = $check_form_all_data_value->entry_id; ?>
<?php $i++; } ?>
</tr> // Don't forget this at the end
Here is working solution as suggested by #Kevin
<?php
$new_data = array();
foreach ( $form_all_data as $k=> $v ) {
$new_data[$v->entry_id][] = $v;
}
foreach ( $new_data as $new_data_key => $new_data_value ) { ?>
<tr>
<?php
foreach ( $new_data_value as $check_form_all_data_key => $check_form_all_data_value ) {
if ( $check_form_all_data_value->form_id == 1 ) {
if ( $check_form_all_data_value->meta_key == 6 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
if ( $check_form_all_data_value->meta_key == 3 ) { ?>
<td class=""><?php echo $check_form_all_data_value->meta_value ?></td>
<?php }
} else {
} ?>
<?php } ?>
</tr>
<?php } ?>
$rows = [];
foreach ($form_all_data as $row) {
if (!isset($rows[$row->id])) {
$rows[$row->id] = [];
}
$rows[$row->id][] = $row;
}
$html = '';
foreach ($rows as $row) {
$html .= '<tr>';
foreach ($row as $cell) {
$html .= '<td>'.$cell->meta_value.'</td>';
}
$html .= '</tr>';
}
I think you should use switch case for form_id and if clause for meta_key. For example :
<?php
$row = "";
foreach ($form_all_data as $check_form_all_data_key => $check_form_all_data_value)
{
$row .= "<tr>";
switch ($check_form_all_data_key->form_id)
{
case 1:
if ($check_form_all_data_value->meta_key == 6 || $check_form_all_data_value > meta_key = 3)
$row .="<td>".$check_form_all_data_value->meta_value."</td>";
break;
case 2:
if ($check_form_all_data_value->meta_key == 1 || $check_form_all_data_value > meta_key = 5 || $check_form_all_data_value > meta_key = 2)
$row .="<td>".$check_form_all_data_value->meta_value."</td>";
break;
default:
// more case form_id you want check
break;
}
$row .= "</tr>";
}
echo $row;
?>

Dynamic Table from PHP Array

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>';

Hierarchy in the array РHP

I have a function that build a Tree Array. Example:
Array
(
[0] => Array
(
[id] => 12
[address] => 'Ukraine'
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 11
[address] => Crimea
[parent_id] => 12
[children] => Array
(
[0] => Array
(
[id] => 16
[address] => Yalta
[parent_id] => 11
)
)
)
)
)
I have function to print Tree (I want to get output with levels indented):
function printTree($data) {
foreach ($data as $item) {
if ($item['parent_id'] != 0)
echo ' - ' . $item['address'] . "<br>";
else
echo $item['address'] . "<br>";
if (isset($item['children'])) {
printTree($item['children']);
}
}
}
But my result is only with one levels indets, because my if is not correct:
Ukraine
- Crimea
- Yalta
I need to get with all levels indents. What do I need to change in my if statement?
Ukraine
- Crimea
- Yalta
Rewrite your code this way:
function printTree($data, $level = 0) {
foreach ($data as $item) {
if ($item['parent_id'] != 0) {
/* here we corrects indent: */
echo str_repeat(' ', $level) . ' - ' . $item['address'] . "<br>";
} else {
echo $item['address'] . "<br>";
}
if (isset($item['children'])) {
printTree($item['children'], $level + 1);
}
}
}
You just add a depth variable to control how many blanks you want add before each child item:
<?php
$data = array(
0 => array(
'id' => 12,
'address' => 'Ukraine',
'parent_id' => 0,
'children' => array(
0 => array(
'id' => 11,
'address' => Crimea,
'parent_id' => 12,
'children' => array(
0 => array(
'id' => 16,
'address' => Yalta,
'parent_id' => 11,
)
)
)
)
)
);
printTree($data);
function printTree($data, $depth = 0) {
foreach ($data as $item) {
if ($item['parent_id'] != 0)
echo str_repeat(' ', $depth), "- {$item['address']}\n";
else
echo "{$item['address']}\n";
if (isset($item['children'])) {
printTree($item['children'], ++$depth);
}
}
}
Output:
Ukraine
- Crimea
- Yalta
Working demo.

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