0

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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
NCTI
  • 37
  • 4
  • `$key = array_search(40489, array_column($userdb, 'uid'));` didn't work for you? You realize you'd need to adjust the values to make it work for you? Is your array named `$userdb`? Do you have a column `uid`? – brombeer Sep 27 '21 at 15:01
  • yes, I just copy from example... :) – NCTI Sep 27 '21 at 15:10

0 Answers0