2014-02-11 54 views
0

好的,所以過去幾天我一直在爲這件事而頭疼。while while循環訪問一個數組,並在此期間向前移動然後向前移動?

所以我有這三個串

$string_one = 'Some text'; 
$string_two = 'Some more text'; 
$string_three = 'Even more text'; 

哪個正在經歷imagettfbbox()設置其高度和寬度。

我想我的GD圖像的輸出是Some text Some more text Even more text,所以基本上我希望他們都在一行上。所以我試圖根據以前的字符串長度來計算每個字符串的x位置,這必須是相對於和位置的。

目前,我正在努力做到以下幾點:

  1. 添加兩個(或更多)寬度一起
  2. 負荷蘭國際集團從$x的答案除以總,這給出了線上第一個字符串的起點。
  3. 要設置第二個字符串的起點,我將第一個字符串的長度添加到起點。

但是,爲了做到這一點,我不得不回到先前的值,獲取寬度,將其添加到當前寬度併除以二,然後設置前一個和當前數組的值變量。因此,每當while循環,我想要移回數組指針,獲取寬度值,確定x位置,然後向前移動數組指針並設置x位置。所以我試圖用prev()next()遍歷數組。

我做了以下內容:

$gb是根據for循環會在哪個字符串設定。所以如果是第二種情況,它會回到第一個。如果它是第五回3,但在這裏並不重要。

$o是,在while循環的結束迭代,這兩個for循環坐在計數器

$text是一個2維陣列啓動是這樣的:

Array 
(
    [ven-pub] => Array 
     (
      [value] => 
      [font-size] => 30 
     ) 

    [More array values].... 
) 

但我m目的是爲了看起來像這樣到底

Array 
(
    [ven-pub] => Array 
     (
      [value] => 
      [font-size] => 30 
      [h] => 38 
      [w] => 514 
      [x] => 1514 
      [y] => 1611 
     ) 

    [More array values].... 
) 

我會在代碼中評論我想要做的,但假設這是循環遍歷第二個數組變量。

$gb = 1;//In this instance $gb is going back once. 
$o = 1;//Set the counter to one, because count will not include 0, right? 
$arr_c = count($text);//Set the maximum number of times we should loop. 
while($o <= $arr_c){//Meanwhile... 
    $key = key($text);//Getting the current key 
    $text[$key]['font-size'] = $text[$key]['font-size']*3;//Text related stuff 
    //Getting the height and width of my string 
    $dimensions = imagettfbbox($text[$key]['font-size'], 0, $font, $text[$key]['value']); 
    $text[$key]['h'] = ($dimensions[7]-$dimensions[1])*-1;//Setting the height... 
    $text[$key]['w'] = ($dimensions[2]-$dimensions[0]);//...and width 

    for($gb;$gb<=$o;$gb++){ 
     $key = key($text);//I've found getting the key again helps.. 
     /* Here I'm looping backward just once using prev() 
      and adding the widths into one variable */ 
     $widths[] = $text[$key]['w']; 
     prev($text); 
    } 

    $total_width = array_sum($widths); 
    $x = ($new_x - ($total_width/2)); //Dividing it by two to get my start position. 
    /*Setting the current $key's ['x'] position, which should be 
     the first array variable, not the second.*/ 
    $text[$key]['x'] = $x; 


    $gb = $cgb;//Resets $gb to its original number. 
    $combined_widths[] = $text[$key]['w'];//Saving the starting width for the next array variable 

    for($gb;$gb<=$o;$gb++){ 
     next($text);//Moving forwards to the next array variable. 
     /* Adding the previous array variables width to $x to generate the 
      second array variables ['x'] position. 
     */ 
     $text[$key]['x'] = $x+array_sum($combined_widths); 
     $combined_widths[] = $text[$key]['w'];//Adding the new variables width to the previous   

    } 
//By here, the array pointer should be back where it started for this loop. 

next($text)//Move the array pointer forward ready for the next loop. 
$o++ 
} 

我的問題出現時,它開始要麼回到很遠,要麼告訴我索引是未定義的。由此可笑地困惑。

回答

0

好的,說實話,我不明白你的代碼。它在很多方面都被打破了,我無法馬上修復它。另外我不太明白你想達到什麼。請查看我根據您的代碼創建的以下代碼以及我目前瞭解的內容。也許我們可以接近你想達到的目標,或者只是不同的編碼風格足以讓你達到目標。

<?php 

// Ensure we start from the beginning of the array. 
reset($text); 

// Helpers... 
$widths = $next_x = 0; 

foreach ($text as $key => $attributes) { 
    $text[$key]["font-size"] *= 3; 

    // Where is $font coming fromt? 
    $dimensions = imagettfbbox($text[$key]["font-size"], 0, $font, $text[$key]["value"]); 

    $text[$key]["h"] = abs($dimensions[7] - $dimensions[1]); 
    $text[$key]["w"] = $dimensions[2] - $dimensions[0]; 

    // Directly add it, we iterate forward, no need for your complicated and slow loop in a loop. 
    $widths += $text[$key]["w"]; 

    // Where is $new_x coming from? 
    $text[$key]["x"] = $next_x - $widths/2; 

    // All we wanted, isn't it. 
    $next_x = $text[$key]["x"] + $text[$key]["w"]; 
} 

?> 

一般建議:總是試着寫易於理解的代碼。不要開始擺弄prev()next()或一個循環在一個循環,除非它不是完全必要的(無論什麼原因)。不要猶豫,介紹幫助你和其他人理解你的代碼的中間變量(相應地命名它們,所以每個人都知道他們在做什麼而沒有任何評論)。

+0

嗨,對不起,我不是那麼簡潔。 '$ new_x'是繪圖區域的中間部分;所以我在該圖像上有一個圖像和一個盒子,專門用於寫文本,'$ new_x'是位於盒子中心的座標,我想寫我的文本。因爲我不知道該行的總長度,因爲循環未查看未來變量的寬度,所以我不能給出第一個變量的x位置。 – Gary