I created a function that gets the product_id and its category_id where the product_id = the product_id supplied like so:
public function getCategorys($product_id){
$sql = "SELECT category_id,product_id FROM oc_product_to_category WHERE product_id = '$product_id'";
$result = $this->query($sql);
$categories = $this->getResult($result);
return $categories;
}
This is function is called with the following code:
foreach($product_ids as $product_id){
$categories[] = $data->getCategorys($product_id);
}
I then create an array containing the product_id and each category_id that product is found in, I did this with the following code:
$product_categories = array();
foreach($categories as $category){
foreach($category as $cat){
if($cat['product_id'])
$product_categories[] = array(
'product_id' => $cat['product_id'],
'category_id' => $cat['category_id']
);
}
}
Now when I print_r($product_categories) I get the following result (this is just part of it I wont put the full array in because its large):
Array
(
[0] => Array
(
[product_id] => 9319
[category_id] => 293
)
[1] => Array
(
[product_id] => 9319
[category_id] => 313
)
[2] => Array
(
[product_id] => 11969
[category_id] => 395
)
[3] => Array
(
[product_id] => 11969
[category_id] => 396
)
Now what I'm trying to achieve is: I want an array that has the product ID and each category_id that it is found in so for example the first two arrays with product id 9319 and 11969 I would want that to look like this:
[0] => Array
(
[product_id] => 9319
[category_id] => 293,313
)
[1] => Array
(
[product_id] => 11969
[category_id] => 395,396
)
I know this isn't articulated very well but I'm not sure how else to phrase this question! I essentially want all the duplicate product id's removed and all their category_id's as one comma separated string in the same array.
EDIT:
I tried the answer provided Group array by subarray values
However I'm getting this output instead now:
Array
(
[22] => Array
(
[133] => Array
(
[product_id] => 22
[category_id] => 313
)
)
[59] => Array
(
[320] => Array
(
[product_id] => 59
[category_id] => 294
)
[321] => Array
(
[product_id] => 59
[category_id] => 331
)
)
[63] => Array
(
[368] => Array
(
[product_id] => 63
[category_id] => 292
)
[369] => Array
(
[product_id] => 63
[category_id] => 302
)
)
[87] => Array
(
[51] => Array
(
[product_id] => 87
[category_id] => 293
)
[52] => Array
(
[product_id] => 87
[category_id] => 313
)
)
This is nearly close to what I want but I want all the category ids in array for that product_id.
Would you not be better off with an array that has the product_id as the key which contained an array of category_id's Comma delimited lists mark the entry point to the road to hell
Make you foreach loop do this instead
$product_categories = [];
foreach($categories as $cat){
$product_categories[$cat['product_id']][] = $cat['category_id'];
}
Related
Array
(
[0] => Array
(
[cart_id] => 24763
[product_id] => 740
[name] => Samsung S20
[model] => Samsung S20
[shipping] => 1
[image] => catalog/samsung/samsungs20.jpg
[option] => Array
(
)
[download] => Array
(
)
)
[1] => Array
(
[cart_id] => 24799
[product_id] => 749
[name] => Huawei P40
[model] => Huawei P40
[shipping] => 1
[image] => catalog/huawei/huaweip40.jpg
[option] => Array
(
)
[download] => Array
(
)
)
)
^ Above is the output of the array
$productdata = $this->cart->getProducts();
^ Above is the code for query out array
is there anyway to check product_id of each array ? Let say that if
product_id = 749 , alert ('P40');
Tried to do
foreach ($productdata as $productdatas) {
if ($productdata['product_id'] = 749)
{ alert('P40');
}
}
nothing happen
This part sets the key, not read it. You need to replace the = with ==:
// From
if ($productdata['product_id'] = 749)
// To
if ($productdata['product_id'] == 749)
I have the following $items PHP array...
Array (
[44567] => Order_Item Object (
[extra_data:protected] => Array (
[product_id] => 0
[variation_id] => 0
)
[data:protected] => Array (
[order_id] => 4432
[name] => Test Product
[product_id] => 99
)
[cache_group:protected] => order-items
[object_read:protected] => 1
)
)
I am trying to use in_array to find out if it contains the product_id 99, I have tried...
if (in_array('99', $item['product_id'], true)) {
echo 'DETECTED';
}
Does anybody have a similar working example I can look at?
I have made researches and havent fount any solutions for this yet. So final thought is come to Stackoverflow and ask the question.
I have 2 array like below:
BigArray
Array
(
[0] => Array
(
[id] => 1
[category_name] => Accountancy
[category_name_vi] => Kế toán
[category_id] => 1
)
[1] => Array
(
[id] => 2
[category_name] => Armed forces
[category_name_vi] => Quân đội
[category_id] => 2
)
[2] => Array
(
[id] => 3
[category_name] => Admin & Secretarial
[category_name_vi] => Thư ký & Hành chính
[category_id] => 3
)
[3] => Array
(
[id] => 4
[category_name] => Banking & Finance
[category_name_vi] => Tài chính & Ngân hàng
[category_id] => 4
)
)
and SmallArray:
Array
(
[0] => Array
(
[id] => 7
[category_id] => 2
[jobseeker_id] => 1
)
[1] => Array
(
[id] => 8
[category_id] => 3
[jobseeker_id] => 1
)
)
Ok, now I wanted to match each category_id from SmallArray link with respectively category_name from BigArrayand the output I only need matched values between SmallArray and BigArraywhere category_id of SmallArray is key and category_name of BigArray is value like below:
Matched array:
Array
(
[0] => Array
(
[2] => Armed forces
)
[1] => Array
(
[3] => Admin & Secretarial
)
)
So far, I have tried array_intersect, 2 foreach loops but no luck. Any advise would be very appreciated :(
Thanks
This should do that:
foreach ($smallArray as $smallKey => $smallElement) {
foreach ($bigArray as $bigKey => $bigElement) {
if ($bigElement['id'] == $smallElement['category_id']) {
$smallArray[$smallKey] = array(
$bigElement['id'] => $bigElement['category_name'],
);
break; // for performance and no extra looping
}
}
}
After these loops, you have what you want in $smallArray.
I have below $test array
Array
(
[0] => Array
(
[quantity] => 3
[stock_id] => _PHONE
)
[1] => Array
(
[quantity] => 3
[stock_id] => 102
)
[2] => Array
(
[quantity] => 4
[stock_id] => _PHONE
)
[3] => Array
(
[quantity] => 3
[stock_id] => 102
)
[4] => Array
(
[quantity] => 4
[stock_id] => _PHONE
)
[5] => Array
(
[quantity] => 6
[stock_id] => _PHONE
)
[6] => Array
(
[quantity] => 2
[stock_id] => 102
)
)
and to sum same stock_id keys to one, i use below functions:
function sum($array, $key){
isset($array[$key['stock_id']]) ? $array[$key['stock_id']]['quantity'] += $key['quantity'] : $array[$key['stock_id']] = $key;
return $array;
};
//merge same stock_id and sum the quantity same stock id
$sum_same_stock_id = array_reduce($test, "sum");
and the result went well like below:
$sum_same_stock_id:
Array
(
[_PHONE] => Array
(
[quantity] => 17
[stock_id] => _PHONE
)
[102] => Array
(
[quantity] => 8
[stock_id] => 102
)
)
So the question here is, I wanted to pass an dynamic keys values not just fixed values stock_id & quantity in sum function above. Have tried variety ways but still can't figured out the way. And can we put those functions into class as well?
Any advise is appreciated!
Maybe you can use the "use" function for the callback?
See https://www.php.net/manual/en/functions.anonymous.php
Here is an example of an array that is output:
Array ( [CART] => Array ( [ITEMS] => Array ( [0] => Array ( [product_id] => 269194 [variation_id] => 0 [options] => Array ( ) [quantity] => 1 [product_name] => 15 Top Hits for Easy Piano [product_code] => HL102668 [product_price] => 14.9900 [original_price] => 14.9900 [default_currency] => 1 [customer_group] => [product_fields] => Array ( ) ) [1] => Array ( [product_id] => 266421 [variation_id] => 0 [options] => Array ( ) [quantity] => 1 [product_name] => Whistle [product_code] => HD245839 [product_price] => 3.9900 [original_price] => 3.9900 [default_currency] => 1 [customer_group] => [product_fields] => Array ( ) ) ) [LAST_UPDATED] => 1349829499 [NUM_ITEMS] => 2 ) [JustAddedProduct] => [CHECKOUT] => Array ( ) )
There is an array for each unique product (in this example there are 2 unique products.) Sometimes there will be just one, sometimes there could be 20 or more unique products.
The value that is important to me is [product_code]. You can see that in the first array, there is [product_code] => HL102668. In the second there is [product_code] => HD245839.
How can I check to see if 'HD' exists in any of the [product_code] values? If it does, I need to return false.
Thank you for your help!
Access your sub array :
$products = $array['CART']['ITEMS'];
Loop through your sub array :
foreach ($products as $product)
Check if HD exists in your product_code, with either simple strstr, or with regex using preg_match (if you are comfortable with it).
if (strstr($product['product_code'], "HD")) {
// Do your actions
}