Splitting letters in string into mult. array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Splitting letters in string into mult. array

Post by nigma »

Say I have the following text

abcdefg
hijklmn


And I want to put it into a multidimensional array so that doing

Code: Select all

print $arrayName[1][2];
would print out "b" (b is 2nd char in 1st row).

Anyone be able to point me in the right direction?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Code: Select all

<pre>
<?php
    $text[] = 'abcdefg';
    $text[] = 'hijklmn';
    
    function printit($row,$char) {
        global $text;
        return substr($text[$row-1],$char-1,1);
    }
    echo printit(2,2); // i
?>
Note that I'm bypassing that array and first letter actually is 0.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Thanks a lot JAM. After reading your reply I went to php.net and checked out their man page for substr. Apreciate your help.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Welcome. Thinking that there might be another function for this, but I can't... find... it...

If noone posts something else, pretend there isn't...
Post Reply