0

The following is the code

<?php
$id ="202883-202882-202884-0";
$str = implode('-',array_unique(explode('-', $id)));
echo $str;
?>

The result is

202883-202882-202884-0

for $id ="202883-202882-202882-0";, result is 202883-202882-0

I would like to replace the duplicate value with zero, so that the result should be like 202883-202882-0-0, not just remove it.

and for $id ="202883-0-0-0";, result should be 202883-0-0-0. zero should not be replaced, repeating zeros are allowed. How can I archive that?

More info:
I want to replace every duplicate numbers. Because this is for a product comparison website. There will be only maximum 4 numbers. each will be either a 6 digit number or single digit zero. all zero means no product was selected. one 6 digit number and 3 zero means, one product selected and 3 blank.

Each 6 digit number will collect data from database, I dont want to allow users to enter same number multiple times (will happen only if the number is add with the URL manually.).

Update: I understand that my question was not clear, may be my English is poor. Here is more explanation, this function is for a smartphone comparison website. The URL format is sitename.com/compare.html?id=202883-202882-202889-202888. All three numbers are different smartphones(their database product ID). I dont want to let users to type in the same product ID like id=202883-202882-202882-202888. It will not display two 202882 results in the website, but it will cause some small issues. The URL will be same without change, but the internal PHP code should consider it as id=202883-202882-202888-0. The duplicates should be replaced as zero and added to the end. There will be only 4 numbers separated by "-". The following examples might clear the cloud!

if pid=202883-202882-202889-202888 the result should be 202883-202882-202889-202888 if pid=202883-202883-202883-202888 the result should be 202888-0-0-0 if pid=202883-202882-202883-202888 the result should be 202883-202882-202888-0 if pid=202882-202882-202882-202882 the result should be 202882-0-0-0

I want to allow only either 6 digit numbers or single digit zero through the string.

if pid=rgfsdg-fgsdfr4354-202883-0 the result should be 202883-0-0-0 if pid=fasdfasd-asdfads-adsfds-dasfad the result should be 0-0-0-0 if pid=4354-45882-445202882-202882 the result should be 202882-0-0-0

It is too complicated for me create, I know there are bright minds out there who can do it much more efficiently than I can.

21
  • Do you want to replace every duplicate numbers or just consecutive duplicates? Commented Aug 16, 2020 at 4:51
  • I also want to hear the response to Pilan's question. Tim's answer assumes that the duplicates are always consecutive. If they are not consecutive, Tim's answer will fail you. Commented Aug 16, 2020 at 5:20
  • Maybe both of you should stop pointing out the same (possible) edge case multiple times :-) Commented Aug 16, 2020 at 5:34
  • 1
    I think the question is Unclear (you could vote that way) and is more of an XY problem. I have voiced my concerns about the data structure in an earlier comment. @Funk Commented Aug 17, 2020 at 3:40
  • 1
    @mickmackusa I'll cast as "unclear" but I am pretty sure it'll be the hammer that will show. I'll edit this comment either way. Edit: Ok, it did take my unclear vote. But pretty sure it would have been the dupe if I would have voted as "not working". It happened to me before. Commented Aug 17, 2020 at 4:12

3 Answers 3

4

You can do a array_unique (preserves key), then fill the gaps with 0. Sort by key and you are done :)

+ on arrays will unify the arrays but prioritizes the one on the left.

Code

$input = "0-1-1-3-1-1-3-5-0";
$array = explode('-', $input);
$result = array_unique($array) + array_fill(0, count($array), 0);
ksort($result);

var_dump(implode('-',$result));

Code (v2 - suggested by mickmackusa) - shorter and easier to understand

Fill an array of the size of the input array. And replace by leftover values from array_unique. No ksort needed. 0s will be replaced at the preserved keys of array_unique.

$input = "0-1-1-3-1-1-3-5-0";
$array = explode('-', $input);
$result = array_replace(array_fill(0, count($array), 0), array_unique($array));
var_export($result);

Working example.

Output

string(17) "0-1-0-3-0-0-0-5-0"

Working example.

references

  • ksort - sort by key
  • array_fill - generate an array filled with 0 of a certain length
Sign up to request clarification or add additional context in comments.

Comments

1

This is another way to do it.

$id = "202883-202882-202882-0-234567-2-2-45435";

From the String you explode the string into an array based on the delimiter which in this case is '-'/

$id_array = explode('-', $id);

Then we can loop through the array and for every unique entry we find, we can store it in another array. Thus we are building an array as we search through the array.

$id_array_temp = [];
// Loop through the array
foreach ($id_array as $value) {
    if ( in_array($value, $id_array_temp)) {
        // If the entry exists, replace it with a 0
        $id_array_temp[] = 0;
    } else {
        // If the entry does not exist, save the value so we can inspect it on the next loop.
        $id_array_temp[] = $value;
    }
}

At the end of this operation we will have an array of unique values with any duplicates replaced with a 0.

To recreate the string, we can use implode...

$str = implode('-', $id_array_temp);
echo $str;

Refactoring this, using a ternary to replace the If,else...

$id_array = explode('-', $id);
$id_array_temp = [];
foreach ($id_array as $value) {
    $id_array_temp[] = in_array($value, $id_array_temp) ? 0 : $value;
}
$str = implode('-', $id_array_temp);
echo $str;

Output is

202883-202882-0-0-234567-2-0-45435

Comments

0

This appears to be a classic XY Problem.

The essential actions only need to be:

  1. Separate the substrings in the hyphen delimited string.
  2. Validate that the characters in each substring are in the correct format AND are unique to the set.
  3. Only take meaningful action on qualifying value.

You see, there is no benefit to replacing/sanitizing anything when you only really need to validate the input data. Adding zeros to your input just creates more work later.

In short, you should use a direct approach similar to this flow:

if (!empty($_GET['id'])) {
    $ids = array_unique(explode('-', $_GET['id']));
    foreach ($ids as $id) {
        if (ctype_digit($id) && strlen($id) === 6) {
        // or: if (preg_match('~^\d{6}$~', $id)) {
            takeYourNecessaryAction($id);
        }
    }
}

7 Comments

I never got the time to test the answers. I will test all of them today and mark the answer. Please check the additional details added in the question.
If your application is going to explode on hyphens anyhow, then it doesn't make sense to execute the sanitization with regex. If your script is only taking meaningful action on qualifying numbers, it doesn't make sense to bother replacing substrings in the input data with zeros.
if the string detects either a 6 digit number or single digit zero, it will work preoperly. But if it detects anything else, no data will be shows. but some design elements work incorrectly.
Then it seems, you need to repair the scripts that follow this one. Again, this is an XY problem. You have cascading problems to solve. This generally dictates that you need to refactor parts of your application to be more direct and purposeful. Writing hacky code to suit hacky code will end up turning your code base into garbage that you yourself won't want to maintain -- not to mention other developers that will see your code.
Thank you for your time. may be it is an XY problem! I was just trying to avoid a bug by removing unwanted characters from the string. You are suggesting to fix that bug ? which can be avoidable! This 4 numbers can be managed easily, these 4 product IDs are used in multiple sections of the script. I thought it will be easier to fix just one string rather than multiple areas?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.