Make product comparison table from the array - php

I have array like:
Array
(
[0] => Array
(
[product_id] => 19
[attributes] => Array
(
[0] => Array
(
[label] => Hard Disk
[name] => harddisk
[value] => 500 GB
)
[1] => Array
(
[label] => RAM
[name] => ram
[value] => 16 GB
)
[2] => Array
(
[label] => Graphics
[name] => graphics
[value] => 2 GB
)
[3] => Array
(
[label] => Graphics Card
[name] => graphicscard
[value] => 2 GB
)
)
)
[1] => Array
(
[product_id] => 20
[attributes] => Array
(
[0] => Array
(
[label] => Hard Disk
[name] => harddisk
[value] => 1 TB
)
[1] => Array
(
[label] => RAM
[name] => ram
[value] => 8 GB
)
[2] => Array
(
[label] => Graphics
[name] => graphics
[value] => 1 GB
)
[3] => Array
(
[label] => Graphics Card
[name] => graphicscard
[value] => 1 GB
)
)
)
)
I want to extract this array to tabular form as :
Product ID | 19 | 20
Hard Disk | 500 GB | 1 TB
RAM | 16 GB | 8 GB
Graphics | 2 GB | 1 GB
Graphics Card | 2 GB | 1 GB
There may come another product id also as it is dynamic. I am able to get first column but how to get other column. My code is:
<table class="table">
<?php
$count = count($all);
if (!empty($all)) {
foreach ($all as $value) {
?>
<?php foreach ($value['attributes'] as $val) { ?>
<tr>
<td><?php echo $val['label']; ?></td>
</tr>
<?php
} exit();
}
}
?>
</table>

I'm always completely baffled by the mixing of HTML and PHP. I prefer a clearer look.
I would solve your problem in two steps:
1: Turn your array into an array that can easily be rendered as a table.
2: Render the table from that array.
if (is_array($all)) {
// step 1: create table
foreach ($all as $one) {
$id = $one['product_id'];
$table['Product Id'][$id] = $id;
foreach ($one['attributes'] as $attribute) {
$label = $attribute['label'];
$value = $attribute['value'];
$table[$label][$id] = $value;
}
}
// step 2: render the table
echo '<table class="table">'.PHP_EOL;
foreach ($table as $label => $row) {
echo "<tr><td>$label</td>";
foreach ($table['Product Id'] as $id) {
echo '<td>'.(isset($row[$id]) ? $row[$id] : '-').'</td>';
}
echo '</tr>'.PHP_EOL;
}
echo '</table>'.PHP_EOL;
}
This routine can cope with missing attributes.
This code is not perfect, but it gives you a point to start. The challenge is to reduce the complexity even more. I think that's possible.

Related

generating an array out of another array by using a unique key

I have an array that contains 3 products with attributes of "color" and "size" and the products are identified by a number ( ['code'] ).
My problem is that when i pull the data from the database I get this array that is in 6 pieces because "color" and "size" get stored in separate arrays.
My question is, how do I generate the data into an array of these 3 products with all their data in the same array.
Array(
[0] => Array
(
[code] => 123
[name] => box
[stock] => 2.00
[price] => 10.00
[color] => brown
)
[1] => Array
(
[code] => 123
[name] => box
[stock] => 2.00
[price] => 10.00
[size] => L
)
[2] => Array
(
[code] => 1234
[name] => box
[stock] => 3.00
[price] => 11.00
[color] => brown
)
[3] => Array
(
[code] => 1234
[name] => box
[stock] => 3.00
[price] => 11.00
[size] => XL
)
[4] => Array
(
[code] => 12345
[name] => box
[stock] => 4.00
[price] => 12.00
[size] => XL
)
[5] => Array
(
[code] => 12345
[name] => box
[stock] => 4.00
[price] => 12.00
[color] => gray
)
)
Expected output:
[0] => Array
(
[code] => 123
[name] => box
[stock] => 4.00
[price] => 12.00
[color] => gray
[size] => XL
)
What i'm looking to do is just combine the doubled array into one. Dont want to mess with SQL anymore - it gets attribute name in one table, attribute values from another table, code,stock,price from another table, name from another table. I know something can be done with just this array even if it will be just a temporary solution.
You can do this:
assuming your array is $products
$merged = array();
foreach($products as $product) {
if (!isset($sorted[$product['code']])) {
$merged[$product['code']] = $product;
} else {
$merged[$product['code']] = array_merge($sorted[$product['code']], $product);
}
}
Use the product code as array key and then merge the array.
If the two rows are always direct successors, with the first one holding the color field, while the latter holes the size field, you can make it easily by iterating over them in a for loop:
$maxCount = count($array);
for ($i = 1; $i < $maxCount; $i += 2) {
$array[$i - 1]['size'] = $array[$i]['size'];
unset($array[i]);
}
This will iterate over each second element of the array and add the size field to the preceding field.
If you need to have succeeding array keys afterwards you can call $arry = array_values($array);.
In case the the associated rows might not be successors you need to map them based on their code field (in case thats the primary key). You can use array_reduce() for that:
$desiredOutput = array_reduce($array, function($output, $element) {
if (!array_key_exists($element['code'], $output)) {
$output[$element['code']] = $element;
} elseif (array_key_exists('size', $element)) {
$output[$element['code']]['size'] = $element['size'];
} elseif (array_key_exists('color', $element)) {
$output[$element['code']]['color'] = $element['color'];
}
return $output;
}, []);

PHP Nested Array is misaligned

I have a PHP loop that is pushing data into an array that will eventually be used to create a list of select options in my dropdown.
I feel like it is kinda close but I am doing something wrong somewhere.
The array of types needs to be part of the category it is associated with.
// Get the list of behavior types
public function _behavior_types()
{
$cat = $this->global_model->get_behavior_categories();
$typ = $this->global_model->get_behavior_types();
$output = array();
// Loop over our categories
foreach($cat as $c)
{
// Push the category name to an array
$output[] = $c['categoryName'];
// Loop over our types
foreach($typ as $t)
{
// If this type belongs to the category we are currently in, add it to the array
if($t['categoryID'] == $c['categoryID'])
{
array_push($output, $t);
}
}
}
return $output;
}
I have something out of place however and its causing the array to not be set up in the correct way.
Here is the current output:
Array
(
[0] => Negative
[1] => Array
(
[categoryID] => 2
[points] => -3
[typeID] => 4
[typeName] => Bad School Day
)
[2] => Positive
[3] => Array
(
[categoryID] => 1
[points] => 2
[typeID] => 1
[typeName] => Ate Dinner
)
[4] => Array
(
[categoryID] => 1
[points] => 2
[typeID] => 3
[typeName] => Brushed Teeth
)
[5] => Array
(
[categoryID] => 1
[points] => 3
[typeID] => 2
[typeName] => Completed Homework
)
)
Here is my desired output:
Array
(
[0] => Negative
[0] => Array
(
[categoryID] => 2
[points] => -3
[typeID] => 4
[typeName] => Bad School Day
)
[1] => Positive
[0] => Array
(
[categoryID] => 1
[points] => 2
[typeID] => 1
[typeName] => Ate Dinner
)
[1] => Array
(
[categoryID] => 1
[points] => 2
[typeID] => 3
[typeName] => Brushed Teeth
)
[2] => Array
(
[categoryID] => 1
[points] => 3
[typeID] => 2
[typeName] => Completed Homework
)
)
The dropdown in turn will look like:
Negative
Bad day at school
Positive
Ate Dinner
Brushed Teeth
Completed Homework
Your desired output is not really a valid array structure, at least how you have it typed. $output[0] cannot be both a string Negative and an array of options. I suggest making the category the key something like this:
// Get the list of behavior types
public function _behavior_types()
{
$cat = $this->global_model->get_behavior_categories();
$typ = $this->global_model->get_behavior_types();
$output = array();
// Loop over our categories
foreach($cat as $c)
{
// Push the category name to an array
$output[$c['categoryName']] = array();
// Loop over our types
foreach($typ as $t)
{
// If this type belongs to the category we are currently in, add it to the array
if($t['categoryID'] == $c['categoryID'])
{
array_push($output[$c['categoryName']], $t);
}
}
}
return $output;
}

PHP Session Array Update

I wish to update a single product quantity from a session with a new value using php.
How can I do this.
Array data is as displayed
Array
(
[products] => Array
(
[0] => Array
(
[name] => Ford AA Flatbed
[code] => IWV001
[qty] => 1
[price] => 15.00
[weight] => 0.12
)
[1] => Array
(
[name] => Ford AA Stakebed
[code] => IWV003
[qty] => 1
[price] => 15.00
[weight] => 0.21
)
)
)
any help would be most appreciated.
I want to be able to search through the session and find the product by code and update the quantity within that product.
Something like this:
foreach($products as &product) {
if ($product[code] == $codeToUpdate) {
$product[qty] = $newQty;
}
}
Of course, this is a sequential search so it will be slow for large data sets.

Two Arrays, One Output (How to ForEach?)

Is there a way to foreach() through one array based on a matching value with a different key in another array? In this example, I have a category array ($cat_data) with cat_id as a key and an image array ($img_data) with category_id as a key.
Array (
[0] => Array (
[cat_id] => 1
[cat_name] => Category 1
)
[1] => Array (
[cat_id] => 2
[cat_name] => Category 2
)
)
Array (
[0] => Array (
[img_id] => 2
[img_name] => demo1.jpg
[img_label] => Demo 1
[category_id] => 2
[img_order] => 1
)
[1] => Array (
[img_id] => 3
[img_name] => demo2.jpg
[img_label] => Demo 2
[category_id] => 2
[img_order] => 2
)
[2] => Array (
[img_id] => 4
[img_name] => demo3.jpg
[img_label] => Demo 3
[category_id] => 1
[img_order] => 1
)
)
What I want is to output my display so it looks like the following:
Category 1
demo3.jpg
Category 2
demo1.jpg
demo2.jpg
Since I'm really not great at fully grasping arrays, I thought I'd try Stack, and I haven't been able to find an answer to my question, partially because I'm not sure what to ask for precisely. Any help??
The naïve way:
foreach ($cat_data as $cat) {
echo $cat['cat_name'];
foreach ($img_data as $img) {
if ($img['category_id'] != $cat['cat_id']) {
continue;
}
echo $img['img_name'];
}
}
This is rather inefficient, since it loops through the $imgs array several times, but easy and works.
More efficient:
$images = array();
foreach ($img_data as $img) {
$images[$img['category_id']][] = $img;
}
foreach ($cat_data as $cat) {
echo $cat['cat_name'];
if (isset($images[$cat['cat_id']])) {
foreach ($images[$cat['cat_id']] as $img) {
echo $img['img_name'];
}
}
}
This first groups all images by category into a new array, which you can then loop over directly once.
I would urge you to redesign your array when you fill them with data to instead look something like this.
Array (
[0] => Array (
[cat_id] => 1
[cat_name] => Category 1
[images] = Array(
[0] => Array (
[img_id] => 4
[img_name] => demo3.jpg
[img_label] => Demo 3
[category_id] => 1
[img_order] => 1
)
)
)
[1] => Array (
[cat_id] => 2
[cat_name] => Category 2
[images] = Array(
[0] => Array (
[img_id] => 4
[img_name] => demo3.jpg
[img_label] => Demo 3
[category_id] => 1
[img_order] => 1
)
[1] => Array (
[img_id] => 2
[img_name] => demo1.jpg
[img_label] => Demo 1
[category_id] => 2
[img_order] => 1
)
)
)
)
Then you would have all the relational data connected and would just have to loop through your array of categories and print the images associated with each one in turn. The numbered indexes could even be changed to associative names if the id of the catagory weren't important for example. Then the array could be indexed with the name of the category and just contain the images of that category.
If the images are to be used in other places where you initial layout of those fits better you could still use this layout for your main data graph. Just replace the actual data of the images in the images array under each category with a reference to the actual image object.

How do I make these fields autopopulate from the database?

I have an array, which holds values like this:
$items_pool = Array (
[0] => Array ( [id] => 1 [quantity] => 1 )
[1] => Array ( [id] => 2 [quantity] => 1 )
[2] => Array ( [id] => 72 [quantity] => 6 )
[3] => Array ( [id] => 4 [quantity] => 1 )
[4] => Array ( [id] => 5 [quantity] => 1 )
[5] => Array ( [id] => 7 [quantity] => 1 )
[6] => Array ( [id] => 8 [quantity] => 1 )
[7] => Array ( [id] => 9 [quantity] => 1 )
[8] => Array ( [id] => 19 [quantity] => 1 )
[9] => Array ( [id] => 20 [quantity] => 1 )
[10] => Array ( [id] => 22 [quantity] => 1 )
[11] => Array ( [id] => 29 [quantity] => 0 )
)
Next, I have a form that I am trying to populate. It loops through the item database, prints out all the possible items, and checks the ones that are already present in $items_pool.
<?php foreach ($items['items_poolpackage']->result() as $item): ?>
<input type="checkbox" name="measure[<?=$item->id?>][checkmark]" value="<?=$item->id?>">
<?php endforeach; ?>
I know what logically I'm trying to accomplish here, but I can't figure out the programming.
What I'm looking for, written loosely is something like this (not real code):
<input type="checkbox" name="measure[<?=$item->id?>][checkmark]" value="<?=$item->id?>" <?php if ($items_pool['$item->id']) { echo "SELECTED"; } else { }?>>
Specifically this conditional loop through the array, through all the key values (the ID) and if there's a match, the checkbox is selected.
<?php if ($items_pool['$item->id']) { echo "SELECTED"; } else { }?>
I understand from a loop structured like this that it may mean a lot of 'extra' processing.
TL;DR - I need to loop within the array, check for the key 'id', then print a string.
If I understand correctly, you need something like this?
array_walk($items_pool, create_function('$array', 'global $item; if( in_array($item->id, $array) ) { echo "checked=\"checked\""; }'));
Conside this though, if you stored your products in a single dimensional array (presuming the ids will always be unique
$items_pool = array(id, quantity)
You will not end up with having duplicated entries of products and can incremement/decrement the quantity easier.
$items_pool[id]++; /* or */ $items_pool[id] = $items_pool[id] + 2;
Both techniques should work if the id exists in the array or not.
If you are hoping to store other attritubes in this array other than quantity, you can
$items_pool = array(id=>array(quantity=>3, colour=>"red"));
$items_pool[id][quantity]--;
The proper attribute is checked="checked"...
if (isset($item_pool[$item->id]) && $item_pool[$item->id] > 0) {
echo ' checked="checked"';
}
<?php echo ($items_pool[$item->id] ? ' checked="checked" ' : ''); ?>
Another option would be to use array_keys to grab the keys of the selected items and then do something like in_array($item->id, $item_keys). All depends on how you feel like doing the work. The tertiary operators are the easiest way to do what you want to inline. You can also use a simple if statement to echo the attribute if you want it.
if($items_pool[$item->id]) echo ' checked="checked"';

Categories