Delete Multidimensional Array Item By Values In the Item - php

Hey guys I have this array :
Array
(
[qty] => 1
[id] => 2
[name] => sallate me gjera plot
[price] => 8
[category_id] => 25
[dish_name] => sallate me gjera plot
[dish_id] => 2
[dish_category_id] => 25
[dish_qty] => 1
[dish_price] => 8
)
Array
(
[qty] => 1
[id] => 1
[name] => sallate cezar
[price] => 12
[category_id] => 25
[dish_name] => sallate cezar
[dish_id] => 1
[dish_category_id] => 25
[dish_qty] => 1
[dish_price] => 12
)
And what I'm trying to do is to unset the item by dish_id.
This is the way I intend to do that :
if(isset($_SESSION["cart_products"]) && count($_SESSION["cart_products"])>0)
{
foreach ($_SESSION["cart_products"] as $key =>$cart_itm)
{
if($cart_itm["dish_id"]==$removable_id)
{
unset($cart_itm[$key]);
}
}
}
Can anyone tell me what I'm doing worng..thanks :D

Actually you need to unset data from actual array which is $_SESSION["cart_products"] not $cart_itm.
So change unset($cart_itm[$key]); to unset($_SESSION["cart_products"][$key])

As alternative, you can use array_filter() with an anonimous function:
$removable_id = 1;
$_SESSION["cart_products"] = array_filter
(
$_SESSION["cart_products"],
function( $row ) use( $removable_id )
{
return $row['dish_id'] != $removable_id;
}
);
print_r( $_SESSION["cart_products"] );
will print:
Array
(
[0] => Array
(
[qty] => 1
[id] => 2
[name] => sallate me gjera plot
[price] => 8
[category_id] => 25
[dish_name] => sallate me gjera plot
[dish_id] => 2
[dish_category_id] => 25
[dish_qty] => 1
[dish_price] => 8
)
)

Related

How to find and replace specific value in multiple array

I want to find flag key from array and replace new value. for example if i found flag values == 2 then i'll put new value instead of old.
Following is my array.
Array
(
[0] => Array
(
[id] => 863
[flag] => 1
[qty] => 2
[size] => 8
[fly_name] => Bead Headed Prince Nymphs size 18
)
[1] => Array
(
[id] => 861
[flag] => 1
[qty] => 2
[size] => 8
[fly_name] => Bead Headed Prince Nymphs size 14
)
[2] => Array
(
[id] => 671
[flag] => 1
[qty] => 4
[size] => 8
[fly_name] => Royal Wulff size 12
)
[3] => Array
(
[id] => 661
[flag] => 1
[qty] => 2
[size] => 3
[fly_name] => Hare's Ear Tan size 16
)
)
I want to find if flag key == 2 then replace new value of flag key , i have tried following way but not work.
if(in_array(2, $fliesUserColumn)) { // search value in the array
foreach($fliesUserColumn as $key => $val)
{
if ($val == '2') $fliesUserColumn[$key] = 'search4';
}
}else{
echo "not";
}
echo "<pre>";
print_r ($flyOfStores);
echo "</pre>";
die;
And out put like with new values in array
Array
(
[0] => Array
(
[id] => 863
[flag] => newvalue
[qty] => 2
[size] => 8
[fly_name] => Bead Headed Prince Nymphs size 18
)
[1] => Array
(
[id] => 861
[flag] => newvalue
[qty] => 2
[size] => 8
[fly_name] => Bead Headed Prince Nymphs size 14
)
...
)
Try below example
foreach ($fliesUserColumn as $key => $val) {
if ($fliesUserColumn[$key]['flag'] == '2') {
$fliesUserColumn[$key]['flag'] = 'Newvalue';
}
}
Try this
foreach ($fliesUserColumn as $key => $val) {
if ($fliesUserColumn[$key]['flag'] == '2') {
$fliesUserColumn[$key] = 'search4';
} else {
echo "not";
}
}

Cannot get item from array

hi im new to api calling and i seem to have a problem with getting an item called price from my array. The following is the array that I am supposed to extract price from.
Array
(
[prodId] => ROC-PRD-2
[prodName] => iphone 6
[projectId] => 8
[categoryIds] => Array
(
[0] => ROC-CAT-1
)
[prodParentSku] => iph6a1
[prodMetaTitle] => iphone 6
[visible] => 1
[prodStatus] => 1
[modifiedDate] => 1443472415
[createDate] => 1443472193
[productImages] => Array
(
[0] => Array
(
[id] => 89
[imageName] => iphone-ipad hi res.png
[imagePath] => http://tos-staging-web-server-s3.s3.amazonaws.com/8/products/ROC-PRD-2/iphone_ipad_hi_res.png
[visible] => 1
[featured] =>
[modifiedDate] => 1443472390
[createDate] => 1443472390
)
[1] => Array
(
[id] => 90
[imageName] => ipad 2.jpg
[imagePath] => http://tos-staging-web-server-s3.s3.amazonaws.com/8/products/ROC-PRD-2/ipad_2.jpg
[visible] => 1
[featured] =>
[modifiedDate] => 1443472397
[createDate] => 1443472397
)
)
[pricing] => Array
(
[price] => 1000
[memberGroupPrices] => Array
(
)
)
)
I am able to get the product images information such as id, imagepath, using the following for loop
foreach ( $product['productImages'] as $key => $data){
foreach ($data as $key => $eachImage){
}
}
However for price my code is as follow:
foreach ( $product['pricing'] as $key => $price){
}
If i echo the $price i would get "1000Array"
If i echo $price['price'], nothing comes out.
You Dont need to look for $pricing as its its single;
$price = $product["pricing"]["price"];
$memberGroupPrices = $product["pricing"]["memberGroupPrices"];
Just print the price with out looping
$price = $array["pricing"]["price"];
Please check with this
foreach ( $product['pricing'] as $key => $price){
if($key=='price')
$price_val=$price;
}

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

Recursive function outputting a wild multidimentional array, would like it one level deep

I am trying to write a recursive function that drills back to the root category of a nest of unknown depth.
[TABLE]
cat_id | cat_name | cat_parent | cat_slug
//each category with a cat_parent of 0 is a root category
[/TABLE]
Example SQL result:
Array
(
[0] => Array
(
[cat_id] => 17
[cat_name] => another-test-category
[cat_parent] => 16
[cat_slug] => Another test category
)
)
Function:
function breadcrumb($cat_id){
$cat_nest =
SELECT *
FROM table
WHERE cat_id = '$cat_id'
//returns 1 row;
$cat_array[$cat_id] = $cat_nest[0];
if($cat_nest[0]['cat_parent'] != 0){
$cat_array[] = breadcrumb($cat_nest[0]['cat_parent']);
}
return $cat_array;
}
It is outputting:
Array
(
[17] => Array
(
[cat_id] => 17
[cat_name] => test.example.1
[cat_parent] => 16
[cat_slug] => Test Example 1
)
[18] => Array
(
[16] => Array
(
[cat_id] => 16
[cat_name] => test.example.2
[cat_parent] => 15
[cat_slug] => Test Example 2
)
[17] => Array
(
[15] => Array
(
[cat_id] => 15
[cat_name] => test.example.3
[cat_parent] => 6
[cat_slug] => Test Example 3
)
[16] => Array
(
[6] => Array
(
[cat_id] => 6
[cat_name] => test.example.4
[cat_parent] => 2
[cat_slug] => Test Example 4
)
[7] => Array
(
[2] => Array
(
[cat_id] => 2
[cat_name] => test.example.5
[cat_parent] => 0
[cat_slug] => Test Example 5
)
)
)
)
)
)
Desired output:
Array
(
[17] => Array
(
[cat_id] => 17
[cat_name] => test.example.1
[cat_parent] => 16
[cat_slug] => Test Example 1
)
[16] => Array
(
[cat_id] => 16
[cat_name] => test.example.2
[cat_parent] => 15
[cat_slug] => Test Example 2
)
[15] => Array
(
[cat_id] => 15
[cat_name] => test.example.3
[cat_parent] => 6
[cat_slug] => Test Example 3
)
[6] => Array
(
[cat_id] => 6
[cat_name] => test.example.4
[cat_parent] => 2
[cat_slug] => Test Example 4
)
[2] => Array
(
[cat_id] => 2
[cat_name] => test.example.5
[cat_parent] => 0
[cat_slug] => Test Example 5
)
)
I ran some tests and I think the solution can be implemented this way:
function breadcrumb($cat_id){
$cat_nest =
SELECT *
FROM table
WHERE cat_id = '$cat_id'
//returns 1 row;
$cat_array[$cat_id] = $cat_nest[0];
if($cat_nest[0]['cat_parent'] != 0){
$cat_array = array_merge( $cat_array, breadcrumb($cat_nest[0]['cat_parent']) );
}
return $cat_array;
}
This would maintain the IDs exactly as you need them, since this would only be merging arrays and not creating new indexes with $cat_array[]
Consider using a do-while-loop:
function breadcrumb($cat_id) {
$cat_array = array();
do {
$cat_nest =
SELECT *
FROM table
WHERE cat_id = '$cat_id';
$cat_array[$cat_id] = $cat_nest[0];
$cat_id = $cat_nest[0]['catparent'];
} while ($cat_id != 0)
return $cat_array;
}
This way you'll also be able to easily identify the 'first' breadcrumb, and add extra code for it, like declaring the array.

Flatten Nested Array to a certain Key

I have a data structure like this
Array
(
[0] => Array
(
[actionResult] => Array
(
[show_page] => Array
(
[15] => Array
(
[section_page_id] => 15
[metadata_id] => 62
[section_id] => 7
[display_order] => 0
[current_layout] => 15
[behaviors] => a:1:{i:0;a:1:{i:0;a:0:{}}}
[options] => a:1:{s:13:"defaultLayout";i:63;}
[section_title] => Ask Study
)
[16] => Array
(
[section_page_id] => 16
[metadata_id] => 66
[section_id] => 7
[display_order] => 1
[current_layout] => 16
[behaviors] => a:0:{}
[options] => a:1:{s:13:"defaultLayout";i:67;}
[section_title] => Ask Study
)
[17] => Array
(
[section_page_id] => 17
[metadata_id] => 69
[section_id] => 7
[display_order] => 2
[current_layout] => 17
[behaviors] => a:0:{}
[options] => a:1:{s:13:"defaultLayout";i:70;}
[section_title] => Ask Study
)
[18] => Array
(
[section_page_id] => 18
[metadata_id] => 72
[section_id] => 7
[display_order] => 3
[current_layout] => 18
[behaviors] => a:0:{}
[options] => a:1:{s:13:"defaultLayout";i:73;}
[section_title] => Ask Study
)
)
)
)
[1] => Array
(
[actionResult] => Array
(
[view_page] => 18
)
)
)
What i need is the ability to flatten this to an array structure to a point where it stops at "actionResult" where all the actionResult will become ONE array rather than nested like this...
How can I go about by doing this in PHP???
if i have understood what you want correctly this should work:
$arr2=array();
foreach($arr as $tmp){
foreach($tmp as $actionRequest){
foreach($actionRequest as $key=>$val){
$arr2[$key]=$val;
}
}
}
where $arr is what you already have and $arr2 will be an array including 2 values , Show_Page and view_page
Best solution is:
$new_arr = array_map(function ($a) {
return $a['actionResult'];
}, $old_arr);

Categories