www.webdeveloper.com
Results 1 to 9 of 9

Thread: need help in php

  1. #1
    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. #2
    Join Date
    Mar 2012
    Posts
    1,639
    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. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    780
    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. #4
    Join Date
    Mar 2012
    Posts
    1,639
    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. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    780
    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. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    I get 59 unique values, not 54.
    PHP Code:
    <?php
    $test 
    '958469';
    $resultuniqueCombos($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(09) as $num) {
                
    $values[] = substr_replace($string, (string) $num$ix1);
            }
        }
        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.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jun 2014
    Posts
    2
    Quote Originally Posted by NogDog View Post
    I get 59 unique values, not 54.
    PHP Code:
    <?php
    $test 
    '958469';
    $resultuniqueCombos($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(09) as $num) {
                
    $values[] = substr_replace($string, (string) $num$ix1);
            }
        }
        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. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    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()?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Mar 2012
    Posts
    1,639
    Quote Originally Posted by NogDog View Post
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles