TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-23-2010, 03:25 PM   #1 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default looping through the alphabet

I'm trying to mess around with arrays right now relating to letters in the alphabet. Is there anyway to code this better:

PHP Code:
<?php
header
("Content-type: text/plain");
        
$arr = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");

        for (
$t=0$t<26$t++){
            for (
$t1=0$t1<26$t1++){
                if((
$arr[$t].$arr[$t1]) == "MA"){
                    for (
$t2=0$t2<26$t2++){
                        echo 
$arr[$t].$arr[$t1].$arr[$t2]. "\n";
                    }
                }else{
                        echo 
$arr[$t].$arr[$t1]. "\n";
                }

            }
        }
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Old 09-23-2010, 07:29 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I'm not really sure "better" would be the correct term to use, but there are a few things I'd do differently.

First would be to use the range() (docs) function to generate the array of letters: range('A', 'Z') I'd also use foreach since you simple want to loop over all of the values present in the array.

Is there anything you're unsure about, when using or looping through arrays?
Salathe is offline  
Reply With Quote
Old 09-23-2010, 08:30 PM   #3 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default

OK... I did take out a function I wrote which counted results per number... Inside the each loop.. there is a count function that would count the amount of values and if it exceeded 200, it would go two letters deep and if needed then three letters deep. Maybe I'm overthinking this... but i been trying to figure this out for a couple hours now. I was able to do it manually like in the code sample above. But I was thinking how it could be possible to make it automatically detect any character result count and automatically go an extra level deep if needed. is this possible to do this automatically?

PHP Code:
<?php
header
("Content-type: text/plain");
$arr = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");

        for (
$t=0$t<26$t++){
        
$oneLetter countData($blah);
        if(
$oneLetter >= 200){ //if the results for oneLetter is 200 or more then go one extra letter
            
for ($t1=0$t1<26$t1++){
                
$twoLetter countData($blah);
                if(
$twoLetter >= 200){ //if the results for twoLetter is 200 or more then go one extra letter
                    
for ($t2=0$t2<26$t2++){
                        echo 
$arr[$t].$arr[$t1].$arr[$t2]. "\n";
                    }
                }else{
                        echo 
$arr[$t].$arr[$t1]. "\n";
                }

            }
        }
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
feedback on my class please frostyboy33 Advanced PHP Programming 7 10-22-2012 09:12 AM
looping Matt Absolute Beginners 1 06-16-2008 07:03 AM
looping problem kororo203 Absolute Beginners 2 04-29-2008 02:48 PM
About PHP on blur - alphabet image satimis Absolute Beginners 3 03-02-2008 09:51 AM


All times are GMT. The time now is 01:04 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design