1

I have a problem I have two arrays, 1 created using php explode function. What I want is to display the number for each word in the array. Below is the code I'm using. The reason I want it so that I can link a mp3 file to each word in the list. The file link format to the mp3 are link this: 0000001.mp3, 0000002.mp3, etc.

Currently the arrays are producing starting key values of zero for every array:

$a1=array(0=>"Cat",1=>"Dog",2=>"Horse",3=>"House");
$a2=array(0=>"Bird",1=>"Rat",2=>"Fish");
$a3=array(0=>"Horse",1=>"Dog",2=>"Bird");

I want the arrays to have keys that continue so that I can link them to a mp3 file e.g

$a1=array(0=>"Cat",1=>"Dog",2=>"Horse",3=>"House");
$a2=array(4=>"Bird",5=>"Rat",6=>"Fish");
$a3=array(7=>"Horse",8=>"Dog",9=>"Bird");

check line 15 p.s I am not a pro at php and I definitely know there are a couple of mistakes in the php code. http://www.deen-ul-islam.org/quran-player/quran.php

foreach ($suraText as $aya) {
    $trans = $transText[$ayaNum- 1];
    // remove bismillahs, except for suras 1 and 9
    if (!$showBismillah && $ayaNum == 1 && $sura !=1 && $sura !=9) {
        $aya = preg_replace('/^(([^ ]+ ){4})/u', '', $aya);
        // display waqf marks in different style
        // $aya = preg_replace('/ ([ۖ-۩])/u', '<span class="sign">&nbsp;$1</span>', $aya);
        $surah2 = leading_zeros($sura, 3);
        $ayaNum2 = leading_zeros($ayaNum, 3);
        $aya = explode(' ',$aya);
        echo "<div class=aya>";
        echo "<div class=quran><a href='http://www.everyayah.com/data/Ghamadi_40kbps/$surah2$ayaNum2.mp3' class='sm2_link'><span class=ayaNum>$ayaNum. </span></a>";
        foreach($aya as $key => $aya) { 
        $key = $key+1; ?>
            <a href="http://audio.allahsquran.com/wbw/<?php echo $key ?>.mp3" class="sm2_link"><span class="word"><?php echo $aya ?></span></a>
            <?php }

            echo  "</div>";
            //echo "<div class=trans>$trans </div>";
            echo "</div>";
            $ayaNum++;
        }
3
  • but you have some sort of arrayed results now? which don't have key as num, so your key is the value? Commented Jun 1, 2011 at 8:24
  • 2
    I read your question three times, and I still don't understand what you've written. Please edit. Commented Jun 1, 2011 at 8:25
  • 2
    Please, put only in your question the code relevant to the arrays your asking about. Showing us all of the code is making us to waste time looking through it and after we're identified the bit of code with the problem, we can now work on getting a solution. People here doesn't have all the spare time of the world (although it may seem so sometimes), so please, put only the relevant code. Commented Jun 1, 2011 at 8:26

3 Answers 3

3

Use keys in your foreach loop:

foreach($array as $i=>$item) {
     echo $i.' = '.$item.'<br/>'
}

http://php.net/manual/en/language.types.array.php

Sign up to request clarification or add additional context in comments.

Comments

1

As far I undestand your question, you should use a code similar to this:

foreach ($suraWords as &$word) {
    $word = substr('0000000'.array_search($word, $dictionary),-7).'.mp3';
}

where $dictionary is an array with all the word you would recognize and $suraWords is the array containing the exploded sura.

In this way all your words are translated to the corresponding filename (though I don't assume nothing on efficiency of this solution).

Comments

0

Why don't you just do an array_merge of the three arrays? Is there any reason you need to have the information in separate arrays?

That way you would have all the sequential keys as you need them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.