开发者

Display php array with sequential indexes

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

fo开发者_StackOverflow社区reach ($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++;
        }


Use keys in your foreach loop:

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

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


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜