This question already has answers here:
Fastest way of deleting a value in a comma separated list
(4 answers)
Closed 6 months ago.
I'm doing a cart system, when a client select various items the data is sent as string with IDs and comma as delimiter, example: 1,2,3
I save that to the database and I can show each item description using a custom function and foreach. The problem is if a client want to remove a item, how should I proceed to update the new changes to the DB? Because I tried with str_replace and I can only remove the ID but keeps the delimiter (comma), example: 1,2, or 1,,3
Any ideas or suggestions how can I improve this? Thank you for your time.
Regards.
You should really normalise the schema, but...
There are many ways to do this. Here's one using using explode() and implode().
function removeIdFromString(string $str, string$id):string {
$arr = explode(',',$str);
if (($key = array_search($id, $arr)) !== false) {
unset($arr[$key]);
return implode(',',$arr);
}
}
$str = "3,5,7,9";
echo removeIdFromString($str,7); // 3,5,9
echo removeIdFromString($str,9); // 3,5,7
Good for PHP7+
Demo:https://3v4l.org/qBP9q
Related
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 3 months ago.
There is scenaria when I fetch results from mysql table using PHP. The results have a column named as status. That column has multiple values like follow up, won and lost. I want to show the results in this order using PHP
Follow up
Won
Lost
How can I achieve this?
I am looking at PHP sort functions but not getting the result I want. So need a solution to get the results in the same way I mentioned.
This is the trick for the above
$sortvalue = 'Follow up';
$result = array_filter($results, function ($item) use ($sortvalue) {
if(stripos($item['status'], $sortvalue) !== false) {
return true;
}
return false;
});
var_dump($result);
This question already has answers here:
What kind of data format is that?
(2 answers)
Closed 4 years ago.
I'm having a hard time with this....
So, I have a load of text:
a:13:{s:9:"live_odds";i:0;s:14:"show_matchbook";i:0;s:12:"show_betfred";i:1;s:16:"show_williamhill";i:1;s:12:"betfair_show";i:1;s:16:"betfair_username";N;s:16:"betfair_password";s:9:"";s:20:"betfair_affiliate_id";s:3:"888";
Now, what I am trying to do is search for betfair_affiliate_id";s:3: within that bulk of text and then display the text between the speech marks.
I was trying
$betfred_show = 'betfair_affiliate_id";s:3:"';
$betfred_show_value = substr($string, strpos($string, $betfred_show) + strlen($betfred_show), 3);
which does work, it brings back 888... but it's not really future proof as this just gets the 3 next digits. However, these could change to 4 or 5 digits.
Any suggestions?
It is a serialised array. You can simply unserialize it and access the key
$array = unserialize($input);
$output = $array['betfair_affiliate_id'];
This question already has answers here:
Separate space-delimited words in a string [duplicate]
(4 answers)
Closed 11 months ago.
I am working on a enrollment system for a judo club. I am using a string to store the ID's of people, who showed up. It looks somthing like this:
0,3,4,7,8,10,
Now, I want to seperate each number into an integer array in PHP. What would the simplest solution to this be? :)
Use explode()
$ids = explode(',', '0,3,4,7,8,10');
$str = '0,3,4,7,8,10';
$arr = explode(',',$str);
use EXPLODE
$num = "0,3,4,7,8,10";
$pieces = explode(",", $num );
Explode Manual
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Remove elements from array?
how would I remove all array elements but last 5 ones? The array is a log, but the log will become extensive so I just want to see the five recent items (a.k.a. last five elements)
Use array_slice:
$a5 = array_slice($arr, -5);
$new = array_slice($old, -5)
if you are getting this array from the text file, you shouldn't read the whole file into array.
either use a command line utility to get 5 last lines,
$last5 = `tail $logfile`;
or at least read only last chunk of it, of considerable side of, say 1Kb and than get last 5 out of it.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php explode and array index
I am new to PHP and would like to ask if there is a way to
write a php code like
$lengthofstay = ($duration <= 0 ? 'optional' : explode("_", $duration )[0]);
i mean calling the explode function and at the sametime reading the first element of resulting array.
it would be possible in 5.4
but your code is as ugly as hell.
there is nothing good in writing all the code in one line.
Write in in three lines and when you come across it in a week, you wouldn't stumble upon it, puzzled.
if ($duration <= 0)
$lengthofstay = 'optional';
} else {
list($lengthofstay) = explode("_", $duration, 1);
}
nothing wrong with it.
if you want to make it strictly one-liner - create a function. and then call it
$lengthofstay = get_length_of_stay($duration);
it's shorter than your cunning construct and way more readable
You might want to use the limit-parameter from explode:
explode("_", $duration, 1)