PHP in_array associative array - php

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?

Related

Creating an array with specific keys and strings into one array

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

search and unset array in array by value in php

My array look like this :
[cart_seller] => Array
(
[3] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
)
)
[4] => Array
(
[# 801] => Array
(
)
)
)
[cart_product] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
[id] => 1705
[name] => ทดสอบสินค้า
[image] => p1534937865-VASAVAT LAB N MEDIA LOGO W.png
[price] => 1111
[option] => หมวด1เลือก1 หมวด2เลือก1
[amount] => 1
)
[# 801] => Array
(
[id] => 801
[name] => โบว์แพรแถบ ร.9 ชนมพรรษา 84 พรรษา ปีพุทธศักราช 2554
[image] => p1498062217-ส.jpg
[price] => 90
[option] =>
[amount] => 1
)
)
I want unset '# 801' in cart_seller and cart_product
in cart_product use unset($cart['cart_product'][# 801]);
but in cart_seller it in array [4] what can i do without reference value (4) ?
exapmle unset($cart['cart_seller'][xxxx][# 801]);
Just loop cart seller array til you find #801.
foreach($cart['cart_seller'] as $key => $c){
if(array_key_exists("#801",$c)){
unset($cart['cart_seller'][$key]['#801']);
}
}

array_reduce to use dynamic variables pass in second function

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

php delete specific value from array

I have an array $products that looks like this
Array
(
[services] => Array
(
[0] => Array
(
[id] => 1
[icon] => bus.png
[name] => Web Development
[cost] => 500
)
[1] => Array
(
[id] => 4
[icon] => icon.png
[name] => Icon design
[cost] => 300
)
)
)
I am trying to delete the part of array that matches [id] => 1 and for this I am using the following code
$key = array_search('1', $products);
unset($products['services'][$key]);
However it is not working and I am not getting any error either.
What am i doing wrong?
This should work for you:
$key = array_search('1', $products["services"]);
//^^^^^^^^^^^^ See here i search in this array
unset($products['services'][$key]);
print_r($products);
Output:
Array ( [services] => Array ( [1] => Array ( [id] => 4 [icon] => icon.png [name] => Icon design [cost] => 300 ) ) )
And if you want to reindex the array, so that it starts again with 0 you can do this:
$products["services"] = array_values($products["services"]);
Then you get the output:
Array ( [services] => Array ( [0] => Array ( [id] => 4 [icon] => icon.png [name] => Icon design [cost] => 300 ) ) )
//^^^ See here starts again with 0
This will loop through $products['services'] and delete the array whose 'id' key has value 1. array_values just re-indexes the array from 0 again.
foreach($products['services'] as $key => $service)
{
if($product['id'] == 1)
{
unset($products['services'][$key]);
array_values($products['services']);
break;
}
}

How to loop through subarray to see if value is return in string

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
}

Categories