/    Sign up×
Community /Pin to ProfileBookmark

Dear All php master guys

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

I have a six digit value i.e [B]“958469”[/B] 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.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@jedaisoulJun 04.2014 — 
  • 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?
    Copy linkTweet thisAlerts:
    @ginerjmJun 04.2014 — 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?
    Copy linkTweet thisAlerts:
    @jedaisoulJun 04.2014 — 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
    Copy linkTweet thisAlerts:
    @ginerjmJun 04.2014 — 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!
    Copy linkTweet thisAlerts:
    @NogDogJun 04.2014 — I get 59 unique values, not 54. ?
    [code=php]
    <?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);
    }
    [/code]

    (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.)
    Copy linkTweet thisAlerts:
    @kuldeephello2uauthorJun 05.2014 — I get 59 unique values, not 54. ?
    [code=php]
    <?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);
    }
    [/code]

    (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.)[/QUOTE]


    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
    Copy linkTweet thisAlerts:
    @NogDogJun 05.2014 — Ah, I was just looking at the array keys, but per php.net/array_unique :
    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.
    So, if by any chance you need consecutive array indexes, you could change the return from the function to:
    [code=php]
    return array_values(array_unique($values));
    [/code]

    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()?
    Copy linkTweet thisAlerts:
    @jedaisoulJun 06.2014 — 
    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()?
    [/QUOTE]

    I still think that it s not worth computing the values. That is, unless you need to output the values for [i]different[/i] seed values.
    ×

    Success!

    Help @kuldeephello2u spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...