如何在模板端的一个foreach中将数组拆分为两个表行“”
问题描述:
Due to working with a restricted working environment, I need to put half of this array into one table row and another half into another .
This is the array (already sorted in order of what I need):
Array
(
[product] => KFE-1221
[description] => Classic Blue
[current_stock] => 630
[purchase_order] =>
[total_sales] => 0
[avail_to_sell] => 630
[size_run1] => 15
[size_run2] => 62
[size_run3] => 122
[size_run4] => 113
[size_run5] => 102
[size_run6] => 92
[size_run7] => 63
[size_run8] => 61
[size_run9] => 0
[size_run10] => 0
)
I need to split the array at "size_run1" - "size_run10" into a separate table row.
Previously, I had this to display the array into a table (standard foreach) when I didn't need the size_run. Due to request I am adding it in. (And yes I am using deprecated php)
<?php
while($data = mssql_fetch_assoc($result_ats)) {
if ($data['avail_to_sell'] <= 0){
echo '<tr class="error">';
}
else {
echo '<tr>';
}
foreach($data as $k => $v){
echo '<td>'.$v.'</td>';
echo '</tr>';
}
}
?>
The reason I want to keep it as one array is because I am using one query for all the information, and with the huge database that I'm working with, it makes more sense for me to split this up on template side rather than have separate arrays and add a process of matching the product to the size_run.
What is the best way of doing this without having the split the array up into different arrays?
My desired output, which is to split up array '$v' into two separate tables like below:
<tr>
<th>product</th>
<th>description</th>
<th>current_stock</th>
<th>purchase_order</th>
<th>total_sales</th>
<th>avail_to_sell</th>
</tr>
<tr>
<th>size_run1</th>
<th>size_run2</th>
<th>size_run3</th>
<th>size_run4</th>
<th>size_run5</th>
<th>size_run6</th>
<th>size_run7</th>
<th>size_run8</th>
<th>size_run9</th>
<th>size_run10</th>
</tr>
答
Something like this:
$firstRow = array('product', 'description', 'current_stock', 'purchase_order', ... );
$secondRow = array('size_run1', 'size_run2', 'size_run3', ... );
while($data = mssql_fetch_assoc($result_ats)) {
echo '<tr>';
foreach($firstRow as $key){
echo '<td>' . $data[$key] . '</td>';
}
echo '</tr>';
echo '<tr>';
foreach($secondRow as $key){
echo '<td>' . $data[$key] . '</td>';
}
echo '</tr>';
}
Still maintainable if you want to change the keys.
答
I would still make a new array and then destroy it if you don't need it.
$newArray = array();
foreach($array as $key=>$value){
$pos = strpos($key, "size_run");
if($pos!==false){
$newArray[$key] = $value;
}
}
Due to working with a restricted working environment, I need to put half of this array into one table row and another half into another .
This is the array (already sorted in order of what I need):
Array
(
[product] => KFE-1221
[description] => Classic Blue
[current_stock] => 630
[purchase_order] =>
[total_sales] => 0
[avail_to_sell] => 630
[size_run1] => 15
[size_run2] => 62
[size_run3] => 122
[size_run4] => 113
[size_run5] => 102
[size_run6] => 92
[size_run7] => 63
[size_run8] => 61
[size_run9] => 0
[size_run10] => 0
)
I need to split the array at "size_run1" - "size_run10" into a separate table row.
Previously, I had this to display the array into a table (standard foreach) when I didn't need the size_run. Due to request I am adding it in. (And yes I am using deprecated php)
<?php
while($data = mssql_fetch_assoc($result_ats)) {
if ($data['avail_to_sell'] <= 0){
echo '<tr class="error">';
}
else {
echo '<tr>';
}
foreach($data as $k => $v){
echo '<td>'.$v.'</td>';
echo '</tr>';
}
}
?>
The reason I want to keep it as one array is because I am using one query for all the information, and with the huge database that I'm working with, it makes more sense for me to split this up on template side rather than have separate arrays and add a process of matching the product to the size_run.
What is the best way of doing this without having the split the array up into different arrays?
My desired output, which is to split up array '$v' into two separate tables like below:
<tr>
<th>product</th>
<th>description</th>
<th>current_stock</th>
<th>purchase_order</th>
<th>total_sales</th>
<th>avail_to_sell</th>
</tr>
<tr>
<th>size_run1</th>
<th>size_run2</th>
<th>size_run3</th>
<th>size_run4</th>
<th>size_run5</th>
<th>size_run6</th>
<th>size_run7</th>
<th>size_run8</th>
<th>size_run9</th>
<th>size_run10</th>
</tr>
Something like this:
$firstRow = array('product', 'description', 'current_stock', 'purchase_order', ... );
$secondRow = array('size_run1', 'size_run2', 'size_run3', ... );
while($data = mssql_fetch_assoc($result_ats)) {
echo '<tr>';
foreach($firstRow as $key){
echo '<td>' . $data[$key] . '</td>';
}
echo '</tr>';
echo '<tr>';
foreach($secondRow as $key){
echo '<td>' . $data[$key] . '</td>';
}
echo '</tr>';
}
Still maintainable if you want to change the keys.
I would still make a new array and then destroy it if you don't need it.
$newArray = array();
foreach($array as $key=>$value){
$pos = strpos($key, "size_run");
if($pos!==false){
$newArray[$key] = $value;
}
}