Display array of array in view page PHP - php

I am getting the result of an array of array from controller.
Here one is the main array and under "bom_dt" is the array of array.
I know that how to display the main array, but not sure on the array under main array. I am stuck here. Please give me some suggestion on this,
Array
(
[0] => Array
(
[tih_sku] => 10001
[tih_style] => RHH/110/02
[tih_color] => BLACK
[tih_leather] => PERU
[bom_dt] => Array
(
[0] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 5, 22.5 CM-*-*-*-*-*-*-*-*-*-*-*-METAL-*-*-*-*-*-*-*-*-*-*-*-*-BLACK-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 2
[mi_sku] => RHH/110/02 BLACK
)
[1] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 3-*-*-*-*-*-*-*-*-*-*-*-NYLON-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 8.75
[mi_sku] => RHH/110/02 BLACK
)
[2] => Array
(
[mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-MD1-NA-NA-NA-NA-NA-NA-NA-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 1
[mi_sku] => RHH/110/02 BLACK
)
[3] => Array
(
[mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-CCF2-NA-NA-NA-NA-NA-NA-NA-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 1
[mi_sku] => RHH/110/02 BLACK
)
[4] => Array
(
[mm_spec_fld] => *-*-GROSSGRAIN-GRANITE-*-*-*-*-*-*-*-*-*-----*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 0.9
[mi_sku] => RHH/110/02 BLACK
)
)
)
[1] => Array
(
[tih_sku] => 10009
[tih_style] => RHH/110/04
[tih_color] => BLACK
[tih_leather] => PERU
[bom_dt] => Array
(
[0] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 5, 44.5 CM-*-*-*-*-*-*-*-*-*-*-*-METAL-*-*-*-*-*-*-*-*-*-*-*-*-BLACK-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 1
[mi_sku] => RHH/110/04 BLACK
)
[1] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 3-*-*-*-*-*-*-*-*-*-*-*-NYLON-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 7.75
[mi_sku] => RHH/110/04 BLACK
)
[2] => Array
(
[mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-FRAME-RECTANGULAR-16MM-NA-NA-NA-NA-PIECES-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 2
[mi_sku] => RHH/110/04 BLACK
)
[3] => Array
(
[mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-R.S.FITTINGS-NA-NA-NA-NA-NA-NA-PIECES-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 2
[mi_sku] => RHH/110/04 BLACK
)
[4] => Array
(
[mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-MD1-NA-NA-NA-NA-NA-NA-PIECES-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 1
[mi_sku] => RHH/110/04 BLACK
)
)
)
[2] => Array
(
[tih_sku] => 10230
[tih_style] => RHN/208/02
[tih_color] => BLACK
[tih_leather] => OSCAR
[bom_dt] => Array
(
[0] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 5, 68 CM-*-*-*-*-*-*-*-*-*-*-*-METAL-*-*-*-*-*-*-*-*-*-*-*-*-BLACK-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 1
[mi_sku] => RHN/208/02 BLACK
)
[1] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 5-*-*-*-*-*-*-*-*-*-*-*-NYLON-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 17
[mi_sku] => RHN/208/02 BLACK
)
[2] => Array
(
[mm_spec_fld] => *-*-*-*-*-NO 3-*-*-*-*-*-*-*-*-*-*-*-NYLON-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 8
[mi_sku] => RHN/208/02 BLACK
)
[3] => Array
(
[mm_spec_fld] => *-*-*-*-*-*-BLG-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-BRANDED ADJUSTER-NA-25MM-NA-NA-NA-NA-PIECES-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
[tbid_qnty] => 1
[mi_sku] => RHN/208/02 BLACK
)
)
)
Main array's result will be the common for the corresponding sub-array.
Can you suggest how to display it on view page using foreach loop.

You would want to implement a nested foreach as Raymond Nijland alluded.
<?php foreach ($items as $item): ?>
<tr>
<th>SKU</th>
<td><?php echo escape($item['tih_sku']); ?></td>
...
<th>BOM DT</th>
<td>
<?php if (count($item['bom_dt]) > 0): ?>
<?php foreach ($item['bom_dt'] as $bom_dt): ?>
//display the bom_dt details
<?php endforeach; ?>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
There are many ways to accomplish the above example, but please check the documentation for the php framework you are using. If you aren't using a php framework, you should absolutely implement something to escape the data you are writing to the page to protect yourself from attack.

try it like following
<?php
$var = array(
array(
'tih_sku' => '10001',
'tih_style' => 'RHH/110/02',
'tih_color' => 'BLACK',
'tih_leather' => 'PERU',
'bom_dt' => array(
array(
'mm_spec_fld' => 'NO 5, 44.5 CM - METAL - BLACK',
'tbid_qnty' => '1',
'mi_sku' => 'RHH/110/04 BLACK',
),
array(
'mm_spec_fld' => 'NO 3 - NYLON',
'tbid_qnty' => '7.75',
'mi_sku' => 'RHH/110/04 BLACK',
)
),
),
array(
'tih_sku' => '10009',
'tih_style' => 'RHH/110/04',
'tih_color' => 'BLACK',
'tih_leather' => 'PERU',
'bom_dt' => array(
array(
'mm_spec_fld' => 'NO 5, 68 CM - METAL - BLACK',
'tbid_qnty' => '1',
'mi_sku' => 'RHN/208/02 BLACK',
),
array(
'mm_spec_fld' => 'NO 5',
'tbid_qnty' => '17',
'mi_sku' => 'RHN/208/02 BLACK',
)
),
)
);
?>
<?php if(array_filter($var)){ ?>
<style>
table{width:50%;}
td{border:1px solid #999;padding:5px;}
</style>
<table>
<tr><td>Specification</td><td>Quantity</td><td>Code</td></tr>
<?php
$returnArray = array_map(function($v){ return $v['bom_dt'];},$var);
array_walk($returnArray, function($v2){array_walk($v2, function($v3){
?>
<tr><td><?=$v3['mm_spec_fld'];?></td><td><?=$v3['tbid_qnty'];?></td><td><?=$v3['mi_sku'];?></td></tr>
<?php
});});
?>
</table>
<?php } ?>
demo: https://paiza.io/projects/bhrSiwQi5zxtSMAz7ev0Wg?language=php

Related

Print associative array to table

I have an associative array like this, i want to generate a table with these data, like breakfast, snacks, lunch, supper, dinner in row the following is the code I've tried, but I am stuck where to break the table row because when the array contains more than one item.i wants to Print associative array to table
*--------------------------------------------------*
| **Breakfast Snacks Lunch Supper Dinner** |
| test test test testfrom
|testfrom
*--------------------------------------------------*
array output is as follow
Array
(
[meal_plan_id] => 17
[calorie_limit] => 1
[total_calorie] => 0
[date] => 2017-12-29
[meal_plan] => Array
(
[0] => Array
(
[meal_type] => bf
[label] => Breakfast
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 107
[label] => test
[quantity] => 10
[unit] => g
[status] => bf
)
[1] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
)
)
[1] => Array
(
[meal_type] => sn
[label] => Snacks
[calorie_limit] => 10
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 108
[label] => test
[quantity] => 121
[unit] => g
)
)
)
[2] => Array
(
[meal_type] => lu
[label] => Lunch
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[status] => su
)
)
)
[3] => Array
(
[meal_type] => su
[label] => Supper
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[status] => sn
)
[1] => Array
(
[id] => 116
[label] => test
[quantity] => 200
[unit] => oz
)
)
)
[4] => Array
(
[meal_type] => dn
[label] => Dinner
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 113
[label] => test500
[quantity] => 20
[unit] => oz
[status] => dn
)
)
)
)
)
below is the code I've tried
// $daily_meals = show_daily_meals() // contains the array
if(!empty($daily_meals['meal_plan'])){
echo '<tr>';
foreach($daily_meals['meal_plan'] as $meal_plan){
foreach ($meal_plan['data'] as $data){
if(!empty($data['id']))
echo '<td class="'.$meal_type.'" data-meals-id="'.$data['id'].'"><span class="left">'.$data['label'].'</span> <span class="right">'.$data['quantity'].' <a class="delete_row"><i class="fa fa-trash" aria-hidden="true"></i></a></span><div class="row_loader"></div></td>';
else echo '<td></td>';
}
$i++;
if($i%5 == 0) echo '</tr>';
}
}
Here is your solution
Input
<?php
$array = array(
array(
'meal_type' => 'bf',
'label' => 'Breakfast',
'calorie_limit' => 30,
'total_calorie' => 0,
'data' => array(
array(
'id' => 107,
'label' => 'test',
'quantity' => 10,
'unit' => 'g',
'status' => 'bf'
),
array(
'id' => 109,
'label' => 'testfrom',
'quantity' => 12,
'unit' => 'g'
)
)
),
array(
'meal_type' => 'sn',
'label' => 'Snacks',
'calorie_limit' => 10,
'total_calorie' => 0,
'data' => array(
array(
'id' => 108,
'label' => 'test',
'quantity' => 121,
'unit' => 'g'
)
)
),
array(
'meal_type' => 'lu',
'label' => 'Lunch',
'calorie_limit' => 20,
'total_calorie' => 0,
'data' => array(array('status' => 'su'))
),
array(
'meal_type' => 'su',
'label' => 'Supper',
'calorie_limit' => 30,
'total_calorie' => 0,
'data' => array(
array('status' => 'sn'),
array(
'id' => 116,
'label' => 'test',
'quantity' => 200,
'unit' => 'oz'
),
)
),
array(
'meal_type' => 'dn',
'label' => 'Dinner',
'calorie_limit' => 20,
'total_calorie' => 0,
'data' => array(
array(
'id' => 113,
'label' => 'test500',
'quantity' => 20,
'unit' => 'oz',
'status' => 'dn'
)
)
)
);
Solution
1st foreach for parent array. Then second for its child..
foreach($array as $r){
$$r['label'] = '<table width="100%">';
foreach($r['data'] as $s){
if(isset($s['label']))$$r['label'] .= '<tr><td align="center">'.$s['label'].'</td></tr>'; // The if condition check weather the label is exist or not if yes then add that in particular label's table
}
$$r['label'] .= '</table>';
}
echo '
<table width="100%" border="1">
<thead>
<tr>
<th>Breakfast</th>
<th>Snacks</th>
<th>Lunch</th>
<th>Supper</th>
<th>Dinner</th>
</tr>
</thead>
Here all labels canverted into variable by $$r['label']. And all have there own table Now we add all these tables into the master table to get the output.
<tbody>
<tr>
<td>'.$Breakfast.'</td>
<td>'.$Snacks.'</td>
<td>'.$Lunch.'</td>
<td>'.$Supper.'</td>
<td>'.$Dinner.'</td>
</tr>
</tbody>
</table>';
//?// echo "<pre>";print_r($array);
?>
Output
Try storing all your column names in an array and create a table with it:
foreach($array as $row) {
$column[$i]['label'] = $row['label'];
$column[$i]['data'] = $this->getdata($row['data']);
$i++;
}
function getdata($array) {
$data = '';
foreach($array as $row) {
$data. = $row['label'].',';
}
return $data;
}
Create a table with the column names and try inserting data using a loop with respect to insert into

Remove array keys that aren't listed in a second array

The following is my result array
Array (
[0] => Array
(
[ProductID] => 220
[TextID] => 477
[ProductName] => Hugo Woman
[Price] => 43.91
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/874229/300_0/Hugo_Boss-Hugo_Woman-EdP_30ml_GRATIS_Nail_Polish_4ml.jpg
[CategoryID] => 1
[SubCategoryID] => 1
[View] => 0
)
[1] => Array
(
[ProductID] => 616
[TextID] => 959
[ProductName] => Hugo XY
[Price] => 44.95
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/333660/300_0/Hugo_Boss-Hugo_XY.jpg
[CategoryID] => 2
[SubCategoryID] => 2
[View] => 0
)
[2] => Array
(
[ProductID] => 650
[TextID] => 991
[ProductName] => Hugo Just Different
[Price] => 45.76
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/617162/300_0/Hugo_Boss-Hugo_Just_Different.jpg
[CategoryID] => 2
[SubCategoryID] => 2
[View] => 0
)
)
I have a second array with subcategories, in which the key is referencing to the SubCategoryID:
Array
(
[1] => Array
(
[EN] => Ladies
[NL] => Dames
)
[2] => Array
(
[EN] => Men
[NL] => Heren
)
)
I want to loop through the result array and remove the keys who don't have a SubCategoryID listed in the second array. I looked at http://php.net/manual/en/function.array-filter.php, but can't figure out the best way to do this.
Thank you!
There are two solutions to the above problem, one with using simple for loop and one with using array_walk() function.
Here, $result_array is your result array and $subcategory_array is your subcategory array.
Solution(1):
$subcategory_ids = array_keys($subcategory_array);
$arrLength = count($result_array);
for($i = 0; $i < $arrLength; ++$i){
if(!in_array($result_array[$i]['SubCategoryID'], $subcategory_ids)){
unset($result_array[$i]);
}
}
// display $result_array
echo "<pre>"; print_r($result_array);
Solution(2):
$subcategory_ids = array_keys($subcategory_array);
function filter_arr($item, $key){
global $result_array, $subcategory_ids;
if(!in_array($item['SubCategoryID'], $subcategory_ids)){
unset($result_array[$key]);
}
}
array_walk($result_array, "filter_arr");
// display $result_array
echo "<pre>"; print_r($result_array);
Please try i thnik this help to you..
$array = array (
0 => array
(
'ProductID' => 220,
'TextID' => 477,
'ProductName' => 'Hugo Woman',
'Price' => 43.91,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/874229/300_0/Hugo_Boss-Hugo_Woman-EdP_30ml_GRATIS_Nail_Polish_4ml.jpg',
'CategoryID' => 1,
'SubCategoryID' => 1,
'View' => 0
),
1 => array
(
'ProductID' => 616,
'TextID' => 959,
'ProductName' => 'Hugo XY',
'Price' => 44.95,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/333660/300_0/Hugo_Boss-Hugo_XY.jpg',
'CategoryID' => 1,
'SubCategoryID' => 2,
'View' => 0
),
'2' => array
(
'ProductID' => 650,
'TextID' => 991,
'ProductName' => 'Hugo Just Different',
'Price' => 45.76,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/617162/300_0/Hugo_Boss-Hugo_Just_Different.jpg',
'CategoryID' => 2,
'SubCategoryID' => 1,
'View' => 0
),);
$array1 = array (
1 => array
(
'EN' => 'Ladies',
'NL' => 'Dames'
),
2 => array
(
'EN' => 'Men',
'NL' => 'Heren'
),);
foreach($array as $newArray){
if (array_key_exists($newArray['SubCategoryID'], $array1)) {
echo '<pre>';
print_r($newArray);
echo '</pre>';}}

PHP Two Arrays Into One Table. First Array Vertical, Second Horizontal

i want result like this in view.
This is (part of) the array:
Array ( [0] => Array ( [car_id] => ferarri [total] => 15 ) [1] => Array ( [car_id] => lamborgini [total] => 10 ) [2] => Array ( [car_id] => ford [total] => 32 ) [3] => Array ( [car_id] => toyota [total] => 45 ) [4] => Array ( [car_id] => viar [total] => 1 ) ) 1
how do I make it look. horizontally displays the total. and vertical displays car brand.
You can use this
<?php
$car_tot = array(
'0' => array (
'car_id' => 'ferarri',
'total' => 15
),
'1' => array (
'car_id' => 'lamborgini',
'total' => 10
),
'2' => array (
'car_id' => 'ford',
'total' => 32
),
'3' => array (
'puskesmas_id' => 'toyota',
'total' => 45
),
'4' => array (
'car_id' => 'viar',
'total' => 1
)
);
echo '<pre>';
print_r( $car_tot );
echo '</pre>';
?>
<table>
<tr>
<th>Type</th>
<th>Total</th>
</tr>
<?php
foreach( $car_tot as $key=>$ctRow )
{
?>
<tr>
<td>
<?= !empty( $ctRow['car_id'] ) ? $ctRow['car_id'] : '';?>
</td>
<td>
<?= !empty( $ctRow['total'] ) ? $ctRow['total'] : '';?>
</td>
</tr>
<?php
}
?>
</table>

Extracting values from JSON in PHP

I am currently experimenting with the Discogs API, using PHP. My query against their API is returned as JSON, which I can decoding using:
$trackMeta = json_decode($trackMeta, true);
I'd then like to be able to access certain elements within the data that is returned. Using print_r($trackMeta);outputs the below data:
Array ( [pagination] => Array ( [per_page] => 1 [pages] => 7 [page] => 1 [urls] => Array ( [last] => http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=7 [next] => http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=2 ) [items] => 7 ) [results] => Array ( [0] => Array ( [style] => Array ( [0] => Drum n Bass ) [thumb] => http://api-img.discogs.com/cf1HxM29IXQKNsSdrwYioa0uEeI=/fit-in/150x150/filters:strip_icc():format(jpeg):mode_rgb()/discogs-images/R-3581480-1336678182-5777.jpeg.jpg [format] => Array ( [0] => Vinyl [1] => 12" [2] => 45 RPM ) [country] => UK [barcode] => Array ( ) [uri] => /Siren-21-Vicious-Circle-Snorkel-SPY-Remix-Solitude/master/433189 [community] => Array ( [want] => 66 [have] => 81 ) [label] => Array ( [0] => Siren Records ) [catno] => SIREN001 [year] => 2012 [genre] => Array ( [0] => Electronic ) [title] => Siren (21) / Vicious Circle (3) - Snorkel (S.P.Y. Remix) / Solitude [resource_url] => http://api.discogs.com/masters/433189 [type] => master [id] => 433189 ) ) )
When trying to use a foreach loop on $trackMeta, I can only return 171 which I believe relates to per_page pages and page.
How can I access data deeper in this array? For example values such as thumb or year or genre?
$trackMeta is an associative array:
$trackMeta = array (
'pagination' => array (
'per_page' => 1,
'pages' => 7,
'page' => 1,
'urls' => array (
'last' => 'http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=7',
'next' => 'http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=2'
),
'items' => 7
),
'results' => array (
0 => array (
'style' => array (
0 => 'Drum n Bass'
),
'thumb' => 'http://api-img.discogs.com/cf1HxM29IXQKNsSdrwYioa0uEeI=/fit-in/150x150/filters:strip_icc():format(jpeg):mode_rgb()/discogs-images/R-3581480-1336678182-5777.jpeg.jpg',
'format' => array (
0 => 'Vinyl',
1 => '12"',
2 => '45 RPM'
),
'country' => 'UK',
'barcode' => array ( ),
'uri' => '/Siren-21-Vicious-Circle-Snorkel-SPY-Remix-Solitude/master/433189',
'community' => array (
'want' => 66,
'have' => 81
),
'label' => array (
0 => 'Siren Records'
),
'catno' => 'SIREN001',
'year' => 2012,
'genre' => array (
0 => 'Electronic'
),
'title' => 'Siren (21) / Vicious Circle (3) - Snorkel (S.P.Y. Remix) / Solitude',
'resource_url' => 'http://api.discogs.com/masters/433189',
'type' => 'master',
'id' => 433189
)
)
);
For example, you can access thumb with the following code:
print_r($trackMeta['results'][0]['thumb']);

Convert PHP "grouped" array to "segmented" array

Trying to convert a "grouped" multidimensional array to a "segmented" multidimensional array. Not sure how to explain it exactly so hopefully these print_r results explain it better.
Current Array
Array
(
[qty] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[item_id] => Array
(
[0] => RR332
[1] => FF586
[2] => QW865
)
[item_name] => Array
(
[0] => Widget 1
[1] => Widget 2
[2] => Widget 3
)
[price] => Array
(
[0] => 1.00
[1] => 1.50
[2] => 1.50
)
)
Wanted Array
Array
(
[0] => Array
(
[qty] => 1
[item_id] => RR332
[item_name] => Widget 1
[price] => 1.00
)
[1] => Array
(
[qty] => 1
[item_id] => FF586
[item_name] => Widget 2
[price] => 1.50
)
[2] => Array
(
[qty] => 1
[item_id] => QW865
[item_name] => Widget 3
[price] => 1.50
)
)
This seems like it should be a simple task but I haven't been able to crack it yet.
I would preferably like to loop through the Current Array to create the Wanted Array if possible.
Any help is greatly appreciated!
Thanks.
You mean this?:
$data = array(
'qty' => array(
0 => 1,
1 => 1,
2 => 1,
),
'item_id' => array(
0 => 'RR332',
1 => 'FF586',
2 => 'QW865',
),
'item_name' => array(
0 => 'Widget 1',
1 => 'Widget 2',
2 => 'Widget 3',
),
'price' => array(
0 => '1.00',
1 => '1.50',
2 => '1.50',
),
);
$result = array();
foreach($data as $key => $values)
{
foreach($values as $number => $value)
$result[$number][$key] = $value;
}
print_r($result);
Output:
Array
(
[0] => Array
(
[qty] => 1
[item_id] => RR332
[item_name] => Widget 1
[price] => 1.00
)
[1] => Array
(
[qty] => 1
[item_id] => FF586
[item_name] => Widget 2
[price] => 1.50
)
[2] => Array
(
[qty] => 1
[item_id] => QW865
[item_name] => Widget 3
[price] => 1.50
)
)
I think this will do the job for you, though I don't have a good data set to test against.
$wanted_array = array();
// Iterate over the outer array to get the text keys & inner arrays
foreach ($current_array as $txtkey=>$arr) {
// Iterate over each inner array to get the numeric key
foreach ($arr as $numkey=>$val) {
// Set a numeric key pointing to a new array
// onto the new outer array if it isn't already there
if (!isset($wanted_array[$numkey])) $wanted_array[$numkey] = array();
// Swap the keys of the outer and inner arrays.
$wanted_array[$numkey][$txtkey] = $val;
}
}
Update: Using test array from #hakre's answer, here it is in practice:
$current_array = array(
'qty' => array(
0 => 1,
1 => 1,
2 => 1
),
'item_id' => array(
0 => 'RR332',
1 => 'FF586',
2 => 'QW865'
),
'item_name' => array(
0 => 'Widget 1',
1 => 'Widget 2',
2 => 'Widget 3'
),
'price' => array(
0 => '1.00',
1 => '1.50',
2 => '1.50'
)
);
// Output:
Array
(
[0] => Array
(
[qty] => 1
[item_id] => RR332
[item_name] => Widget 1
[price] => 1.00
)
[1] => Array
(
[qty] => 1
[item_id] => FF586
[item_name] => Widget 2
[price] => 1.50
)
[2] => Array
(
[qty] => 1
[item_id] => QW865
[item_name] => Widget 3
[price] => 1.50
)
)
Like you said, just loop through it to build it.
$wanted = array();
foreach($current['item_id'] as $key => $value) {
$wanted[$key] = array(
'qty' = $current['qty'][$key],
'item_id' = $current['item_id'][$key],
'item_name' = $current['item_name'][$key],
'price' = $current['price'][$key]
);
}
print_r($wanted);
You can loop over your original array using the foreach loop, which gives you the $key, and $value for each item in your array. From there, construct your new array as you like.
$array2 = array();
foreach ($array1 as $key => $value)
{
$array2[] = array('key' => 'value');
}

Categories