Searching through exploded string based on variable combined with string

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Searching through exploded string based on variable combined with string



In my database I have a string called "Cart".


$cart = "1+10,2+10,3+10,5+10,6+10"
$openCart = explode(",", $cart);



Inside $openCart I want to search for the number and plus signs before adding new information to this array.
I have tried array_search and in_array but neither let me search for a value like "3+".
array_search only allowed me to search for complete strings, such as "3+10" while in_array didnt like it when I ended my search string on the plus sign.



Further context:
In the end, the ideal code for this function would be somewhere along the following:


$pID = 3;
$pOrder = 4;
$openCart = explode(',', $cart);

//Search where $pID . "+" matches a value inside $openCart
$specProd = explode ('+' $openCart[/*Location that got matched*/]);
$specProd[1] = $specProd[1] + $pOrder;

$impProd = implode ('+', $specProd);
$specProd = impProd;
$newCart = implode (',', $specProd);

//upload the string of $newCart back to the database.



Currently I am just stuck on searching through the array and any help would be appreciated.





i think you should used preg_grep function instead of array_search and also this example will help you coderwall.com/p/u0foua/…
– Bilal Ahmed
30 mins ago



preg_grep





or use array_map and call a function on each item of your exploded string
– gogaz
28 mins ago


array_map





Why do you need to explode the string in the first place? Just use strpos or preg_match and search the string instead. I'm quite sure it's quicker than looping each element of an srray
– Andreas
15 mins ago





Each combination stands for a product and amount of orders. I just store it all as a string which I explode when I need access to specifics. This method was advised to me in the early construction of the site and now its too late to go back, haha! In order to avoid specific ID's to appear multiple times in the string, I want to loop through the $openCart to add or deduct the $pOrder from the second part of the string, before collapsing everything back into one string again.
– Daniel de Ridder
12 mins ago




2 Answers
2



You can use preg_match.



To find integer followed by '+':


$string = '423+10';
if(preg_match('/d++/',$string))
echo 'match found';



For dynamic integer:


$string = '6+10';
$d = 6;
preg_match('/'.$d.'++/',$string);
echo 'match found';



It finds an element's key


<?php
$cart = "1+10,2+10,3+10,5+10,6+10";
$openCart = explode(",", $cart);
$needle = "3+";
$return = array_keys(array_filter($openCart, function($var) use ($needle)
return strpos($var, $needle) !== false;
));
if(!empty($return))
// do something






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard