1. Registered User
Join Date
Jun 2014
Posts
2

need help in php

Dear All php master guys

Can you please write a code for me situation is ;-

I have a six digit value i.e "958469" and i want all 6 digit values which will result by changing digit by 0-9 of each position. Program should result 54 unique value.

Result Should be like this

058469
158469
258469
358469
458469
558469
658469
758469
858469
958469
908469
918469
928469
938469
948469
958469
968469
978469
988469
998469
950469
951469
952469
953469
954469
955469
956469
957469
959469
958069
958169
958269
958369
958469
958569
958669
958769
958869
958969
958409
958419
958429
958439
958449
958459
958469
958479
958489
958499
958460
958461
958462
958463
958464
958465
958466
958467
958468
958469

I know how to do it in excel but please somebody create a code in php to print these values.

2. 1. The example as given contains duplicates. E.g. 958469. Please clarify how the series should actually proceed...

2. There is no need to use PHP, or any calculation to generate a fixed series. Just display the numbers!!!

I suspect you wanted the seed number to be input/variable in some way?
Last edited by jedaisoul; 06-04-2014 at 02:59 PM.

3. Seems like such a trivial reason to be writing code for, I almost think it is a homework assignment. If it is a homework assignment, don't you think you should do your homework in order to learn something instead of asking someone here to write it for you?

4. By the way, there are 55 unique numbers in the sequence, not 54, as follows:

958469

058469
158469
258469
358469
458469
558469
658469
758469
858469

968469
978469
988469
998469
908469
918469
928469
938469
948469

959469
950469
951469
952469
953469
954469
955469
956469
957469

958569
958669
958769
958869
958969
958069
958169
958269
958369

958479
958489
958499
958409
958419
958429
958439
958449
958459

958460
958461
958462
958463
958464
958465
958466
958467
958468

5. Still looks and feels and sounds like a homework assignment. One that involves how to use loops and some controls to keep from duplicating the original value in each loop.

If I were the instructor I would have probably worded it:
Given a single 6 digit number produce for me a list of all the different 6 digit numbers one can have by modifying each position using the values 0-9 but without using the original value of each position.

Have fun!

6. I get 59 unique values, not 54.
PHP Code:
``` <?php\$test = '958469';\$result= uniqueCombos(\$test);print_r(\$result);function uniqueCombos(\$string){    \$string = (string) \$string; // gotta love that :)    if(!ctype_digit(\$string)) {        throw new Exception("Must be only digits");    }    \$values = array();    for(\$ix = 0; \$ix < strlen(\$string); \$ix++) {        foreach(range(0, 9) as \$num) {            \$values[] = substr_replace(\$string, (string) \$num, \$ix, 1);        }    }    return array_unique(\$values);} ```
(I don't really care any more if it's homework or not. If it is, you're only hurting yourself if you don't take the time to study this and understand how it works.)

7. Registered User
Join Date
Jun 2014
Posts
2
Originally Posted by NogDog
I get 59 unique values, not 54.
PHP Code:
``` <?php \$test = '958469'; \$result= uniqueCombos(\$test); print_r(\$result); function uniqueCombos(\$string) {     \$string = (string) \$string; // gotta love that :)     if(!ctype_digit(\$string)) {         throw new Exception("Must be only digits");     }     \$values = array();     for(\$ix = 0; \$ix < strlen(\$string); \$ix++) {         foreach(range(0, 9) as \$num) {             \$values[] = substr_replace(\$string, (string) \$num, \$ix, 1);         }     }     return array_unique(\$values); } ```
(I don't really care any more if it's homework or not. If it is, you're only hurting yourself if you don't take the time to study this and understand how it works.)
Your code is good it doesn't get 59 values but 55 values. You are counting it wrong. The one thing missing is that in output list the original value should not be shown. hence there should be 54 unique values excluding the original value.

Dear All, i am not a student. I need to use this code somewhere in my profession.

Thanks all

8. Ah, I was just looking at the array keys, but per php.net/array_unique :
[quote]Note that keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.[quote]
So, if by any chance you need consecutive array indexes, you could change the return from the function to:
PHP Code:
``` return array_values(array_unique(\$values));  ```
If you don't want the original value in the list, you could add a step before the return to get rid of it, perhaps using array_filter()?

9. Originally Posted by NogDog
If you don't want the original value in the list, you could add a step before the return to get rid of it, perhaps using array_filter()?
I still think that it s not worth computing the values. That is, unless you need to output the values for different seed values.