How to check that specific value exists in multidimension array - php

I have session with multidimensions array and this session is update by every form send.
I would like to check if the product_id already exists in my multidimensional array, so as not to add it several times, but only to increase the number of pieces. I create something like a basket. So when somebody add the same product to cart in my session have to check, that this product exists and if exists update just quantity field in array.
I do everything in ajax
array(2) {
[0]=>
array(15) {
["quantity"]=> string(1) "1"
["final_price"]=> string(3) "0.5"
["do_wash"]=> string(2) "on"
["final_wash_price"]=> string(3) "0.2"
["do_bringing"]=> string(2) "on"
["final_bringing_price"]=> string(3) "0.4"
["deposit_price"]=> string(1) "5"
["product_name"]=> string(24) "Talerz płytki Ø 160 mm"
["product_id"]=> string(2) "12"
["product_price"]=> string(3) "0.5"
["product_person_to_bringing"]=> string(1) "1"
["product_wash_price"]=> string(3) "0.2"
["product_bringing_price"]=> string(3) "0.4"
["product_calculate_bringing"]=> string(0) ""
["action"]=> string(9) "send_form"
}
[1]=>
array(15) {
["quantity"]=> string(1) "1"
["final_price"]=> string(3) "0.5"
["do_wash"]=> string(2) "on"
["final_wash_price"]=> string(3) "0.2"
["do_bringing"]=> string(2) "on"
["final_bringing_price"]=> string(3) "0.4"
["deposit_price"]=> string(1) "5"
["product_name"]=> string(24) "Talerz płytki Ø 160 mm"
["product_id"]=> string(2) "12"
["product_price"]=> string(3) "0.5"
["product_person_to_bringing"]=> string(1) "1"
["product_wash_price"]=> string(3) "0.2"
["product_bringing_price"]=> string(3) "0.4"
["product_calculate_bringing"]=> string(0) ""
["action"]=> string(9) "send_form"
}
}
if (isset($_POST['action'])) {
if (!isset($_SESSION['products'])) {
$_SESSION['products'] = [];
}
if (!isset($_SESSION['product'])) {
$_SESSION['product'] = [];
}
$product_object = $_POST;
$_SESSION['product'] = [];
$_SESSION['product'][] = $product_object;
foreach ($_SESSION['product'] as $single_product_array) {
$_SESSION['products'][] = $single_product_array;
}
echo '<pre style="color:#fff;">';
var_dump( $_SESSION['products']);
echo '</pre>';
}
I try to use functions from: PHP multidimensional array search by value and from php.net
$key = array_search(40489, array_column($userdb, 'uid')); but not working for me

Related

Add key-value pair in every array element in multidimension array

Is it possible without using foreach to add key-value to the every array element?
I need to add at the end of all arrays type => image.
I have array like this:
array(5) {
[0]=>
array(7) {
["idfile"]=>
string(2) "71"
["iduser"]=>
string(1) "1"
["name"]=>
string(60) "11745552_873843849371628_3215645663726390853_n-57335133dee48"
["extension"]=>
string(3) "jpg"
["isImage"]=>
string(1) "1"
["is_deleted"]=>
string(1) "0"
["add_date"]=>
string(19) "2016-05-11 15:35:15"
}
[1]=>
array(7) {
["idfile"]=>
string(2) "72"
["iduser"]=>
string(1) "1"
["name"]=>
string(19) "black-57335133e1ded"
["extension"]=>
string(3) "jpg"
["isImage"]=>
string(1) "1"
["is_deleted"]=>
string(1) "0"
["add_date"]=>
string(19) "2016-05-11 15:35:15"
}
[2]=>
array(7) {
["idfile"]=>
string(2) "73"
["iduser"]=>
string(1) "1"
["name"]=>
string(21) "Canon_1-57335133e4394"
["extension"]=>
string(3) "jpg"
["isImage"]=>
string(1) "1"
["is_deleted"]=>
string(1) "0"
["add_date"]=>
string(19) "2016-05-11 15:35:15"
}
}
You can use array_walk() and define the first argument of your callback as reference, but I assume foreach is a bit faster.
array_walk($array, function (&$value) {
$value['type'] = 'image';
});

How to compare two multidimensional array and add variabile to one if values found?

I have a shopping cart and I use two array: one for getting the cart products from session, and one to display the products. I need to get the count value from cart array, to corespondent product from products array. I need the count value in products->barrels array, becouse I will use a placeholder to show existing value.
The chemID and catID values makes the product unique.
I don't want to change the array structure, only to add the count value to products array....Please help
Cart array:
array(2) {
[0]=>
object(stdClass)#224 (9) {
["chemID"]=>
string(3) "657"
["product_number"]=>
string(8) "14004015"
["size"]=>
string(6) "15 GAL"
["catID"]=>
string(2) "24"
["list_price"]=>
string(6) "459.00"
["count"]=>
string(1) "2"
["attribute"]=>
string(6) "Yellow"
}
[1]=>
object(stdClass)#225 (9) {
["chemID"]=>
string(3) "658"
["product_number"]=>
string(9) "14004015C"
["size"]=>
string(6) "15 GAL"
["catID"]=>
string(2) "24"
["list_price"]=>
string(3) "434"
["count"]=>
string(1) "3"
}
}
Products array:
array(2) {
[657]=>
array(4) {
["attribute"]=>
string(6) "Yellow"
["barrels"]=>
array(3) {
[0]=>
object(stdClass)#293 (9) {
["product_number"]=>
string(8) "14004005"
["size"]=>
string(5) "5 Gal"
["catID"]=>
string(2) "13"
["list_price"]=>
string(6) "169.00"
["chemID"]=>
string(3) "657"
["attribute"]=>
string(6) "Yellow"
}
[1]=>
object(stdClass)#294 (9) {
["product_number"]=>
string(8) "14004015"
["size"]=>
string(6) "15 GAL"
["catID"]=>
string(2) "24"
["list_price"]=>
string(6) "459.00"
["chemID"]=>
string(3) "657"
["attribute"]=>
string(6) "Yellow"
}
[2]=>
object(stdClass)#295 (9) {
["product_number"]=>
string(8) "14004030"
["size"]=>
string(6) "30 Gal"
["catID"]=>
string(1) "2"
["list_price"]=>
string(6) "874.00"
["chemID"]=>
string(3) "657"
["attribute"]=>
string(6) "Yellow"
}
}
}
[658]=>
array(4) {
["attribute"]=>
string(5) "Clear"
["barrels"]=>
array(3) {
[0]=>
object(stdClass)#296 (9) {
["product_number"]=>
string(9) "14004005C"
["size"]=>
string(5) "5 Gal"
["catID"]=>
string(2) "13"
["list_price"]=>
string(6) "159.00"
["chemID"]=>
string(3) "658"
["attribute"]=>
string(5) "Clear"
}
[1]=>
object(stdClass)#297 (9) {
["product_number"]=>
string(9) "14004015C"
["size"]=>
string(6) "15 GAL"
["catID"]=>
string(2) "24"
["list_price"]=>
string(3) "434"
["chemID"]=>
string(3) "658"
["attribute"]=>
string(5) "Clear"
}
[2]=>
object(stdClass)#298 (9) {
["product_number"]=>
string(9) "14004030C"
["size"]=>
string(6) "30 Gal"
["catID"]=>
string(1) "2"
["list_price"]=>
string(6) "799.00"
["chemID"]=>
string(3) "658"
["attribute"]=>
string(5) "Clear"
}
}
}
}
To do this, you'll need to loop through both objects and count the matches.
$cnt = 0;
foreach($products as $pkey=>$pobj) {
foreach($carts as $ckey=>$cobj) {
if($cobj->chemID == $pobj->barrels->chemID && $cobj->catID == $pobj->barrels->catID) {
$cnt++;
}
}
}
echo 'Count of matches: ' . $cnt;
Here is a PHP Fiddle demo.
If you want the count value from cart_array written into the objetcs in the procucts array you could try this:
foreach($cart_array as $cart_value){
foreach($products_array[$cart_value->chemID]]["barrels"] as $products_value){
if($products_value->catID == $cart_value->catID){
$products_value->count=$cart_value->count;
}
}
}
#Set count=0 in all barrels in products_array
foreach($products_array as $value1){
foreach($value1["barrels"] as $value2){
if(!isset($value2->count)){
$value2->count = 0;
}
}
}

how do I extract a value from a JSON object Array of Arrays

I am having a hard time extracting a value from the following JSON object
array(3) { [0]=> object(stdClass)#1 (11) { ["Group"]=> string(2) "18" ["GroupName"]=> string(8) "Wireline" ["Region"]=> string(2) "15" ["RegionName"]=> string(8) "Atlantic" ["Province"]=> string(1) "1" ["ProvinceName"]=> string(13) "New Brunswick" ["City"]=> string(2) "11" ["CityName"]=> string(10) "Campbelton" ["Site"]=> string(2) "37" ["SiteName"]=> string(16) "Campbellton PNCT" ["Year"]=> string(4) "2016" }
[1]=> object(stdClass)#2 (5) { ["PlatformID"]=> string(1) "1" ["PlatformTag"]=> string(6) "Access" ["Rack"]=> string(24) "23" Width 36" Depth Rack" ["RackValue"]=> string(1) "2" ["Comments"]=> string(0) "" }
[2]=> object(stdClass)#3 (12) { ["Rack"]=> string(31) "23" Width 36" Depth Rack Access" ["RackValue"]=> string(1) "2" ["RackComments"]=> string(0) "" ["Manufacturer"]=> string(6) "werwer" ["Name"]=> string(6) "werwer" ["Quantity"]=> string(1) "1" ["RackUnits"]=> string(1) "1" ["Power"]=> string(1) "1" ["ActivePassive"]=> string(6) "Active" ["ACDC"]=> string(2) "AC" ["ConnectivityIDs"]=> array(1) { [0]=> string(1) "2" } ["Connectivity"]=> array(1) { [0]=> string(5) "Fiber" } } }
I am trying to extract each item in a foreach loop within PHP Above is the var_dump of the $data[0] JSON object it demonstrates what the Array item looks like.
My foreach is the following
$data = json_decode($_POST["submitdata"]);
$Forecasts = $data[0];
foreach($Forecasts as $Forecast){
echo($Forecast->PlatformID);}
but it returns nothing as a result. Can someone explain to me how to get this from the second Array in the object?
simply place the Index of the inner array along with the object as shown below. This will check for the sub item for the PlatformID and return it to the screen.
foreach($Forecasts as $Forecast){
echo($Forecast[1]->PlatformID);}

How do i get the value of a obj label

here is the obj :
array(2) {
[0]=> object(stdClass)#538 (9)
{
["term_id"]=> string(3) "152"
["name"]=> string(19) "Éducation physique"
["slug"]=> string(18) "education-physique"
["term_group"]=> string(1) "0"
["term_taxonomy_id"]=> string(3) "159"
["taxonomy"]=> string(11) "product_cat"
["description"]=> string(0) ""
["parent"]=> string(3) "123"
["count"]=> string(1) "3"
}
[1]=> object(stdClass)#540 (9)
{
["term_id"]=> string(3) "123"
["name"]=> string(5) "Sport"
["slug"]=> string(5) "sport"
["term_group"]=> string(1) "0"
["term_taxonomy_id"]=> string(3) "123"
["taxonomy"]=> string(11) "product_cat"
["description"]=> string(0) ""
["parent"]=> string(1) "0"
["count"]=> string(2) "49"
}
}
mam :
i try to get the value : [term_id] of 152. what i need it the "152" value in a variable. i try : $product_category->term_id it return "nothing"
and i try : $product_category['term_id'] it return "nothing"
How is the "proper" way to retreive value from object
thanks in advance !
If I'm reading you right, and $product_category is the entire variable, you have two objects in an array. So you'll need to tell PHP which array item you're after before trying to access the object.
Something like $product_category[0]->term_id should work.
Here is another solution :
Convert this object to an array using json_decode, then get the values from array
$array = json_decode($json_string, true);
$term_id = $array[0]['term_id'];
OR to get both values :
foreach($array as $val){
echo $val['term_id'];
}

PHP results not displayed in smarty template

I have a variable $my_arr whose var_dump($my_arr) value is
array(3) { [0]=> object(stdClass)#6 (9) { ["prod_index"]=> string(1) "1" ["prod_code"]=> string(4) "amp1" ["prod_name"]=> string(11) "Amplifier A" ["prod_price"]=> string(6) "100.00" ["frequency"]=> string(1) "2" ["gain"]=> string(1) "2" ["gain_variation"]=> string(3) "0.2" ["noise"]=> string(1) "2" ["power"]=> string(2) "10" } [1]=> object(stdClass)#7 (9) { ["prod_index"]=> string(1) "3" ["prod_code"]=> string(4) "amp1" ["prod_name"]=> string(13) "Amplifire1.33" ["prod_price"]=> string(6) "133.00" ["frequency"]=> string(4) "2.12" ["gain"]=> string(1) "2" ["gain_variation"]=> string(3) "0.2" ["noise"]=> string(1) "2" ["power"]=> string(2) "10" } [2]=> object(stdClass)#8 (9) { ["prod_index"]=> string(1) "4" ["prod_code"]=> string(4) "amp1" ["prod_name"]=> string(13) "Amplifier1.45" ["prod_price"]=> string(6) "150.00" ["frequency"]=> string(4) "2.99" ["gain"]=> string(1) "2" ["gain_variation"]=> string(3) "0.2" ["noise"]=> string(1) "2" ["power"]=> string(2) "10" } }
In smarty I tried
{foreach name = fe1 item = k from = arr}
{$k->prod_index}
{/foreach}
But it is not showing any result .
Assuming you're already assigning the array to smarty like:
$smarty->assign( "arr", $my_arr );
You need to use the "." syntax, like:
{foreach name = fe1 item = k from = arr}
{$k.prod_index}
{/foreach}

Categories