2012-08-09 44 views
1

我認爲這很容易,但變得很難。所有我想要做的是替換對象中的值,然後打印出更改的字符串。順便說一句,這在Joomla內。這並不重要,只是爲了解釋代碼中的所有JHTML /和JURi內容。替換對象數組中的字符串PHP

,我一直在努力的代碼是...

<?php 

// Display the child select box. 
if (isset($this->containers) && count($this->containers)): 
    $items = str_replace("Your Favorite Places", "Browse By Region", $this->containers); 
    echo JHtml::_('select.genericlist', $items, 'finder-containers','onchange="document.location=this.value; return false;"', 'link', 'indented', JUri::getInstance()->toString(array('path'))); 
endif; 

?> 

所以我str_replace線就是我有問題。 $this->containers只是一個狀態數組和其他東西回聲出下拉框。我試圖在最後一行發出回聲之前進行替換,但「你最喜歡的地方」這個詞仍然存在。我必須把這個放在foreach循環或類似的東西嗎?

這裏是一個局部的print_r(其實我想替換字符串是它。分類標題=>你喜歡的地方)

Array (
    [0] => stdClass Object (
     [category_id] => 1 
     [title]  => Gallery 
     [alias]  => gallery 
     [slug]  => 1:gallery 
     [level]  => 0 
     [my_items] => 0 
     [url]   => index.php?option=com_gallery&view=images&category_id=1 
     [route]  => index.php?option=com_gallery&view=images&category_id=1:gallery&Itemid=1766 
     [link]  => /your-favorite-places/categories/gallery.html 
     [indented] => Gallery 
    ) 

    [1] => stdClass Object (
     [category_id] => 164 
     [title]  => Your Favorite Places 
     [alias]  => your-favorite-places 
     [slug]  => 164:gallery/your-favorite-places 
     [level]  => 1 
     [my_items] => 0 
     [url]   => index.php?option=com_gallery&view=images&category_id=164 
     [route]  => index.php?option=com_gallery&view=images&category_id=164:gallery/your-favorite-places&Itemid=3711 
     [link]  => /your-favorite-places/gallery.html 
     [indented] => Your Favorite Places 
    ) 

    [2] => stdClass Object (
     [category_id] => 87 
     [title]  => North America 
     [alias]  => north-america 
     [slug]  => 87:gallery/your-favorite-places/north-america 
     [level]  => 2 
     [my_items] => 0 
     [url]   => index.php?option=com_gallery&view=images&category_id=87 
     [route]  => index.php?option=com_gallery&view=images&category_id=87:gallery/your-favorite-places/north-america&Itemid=1775 
     [link]  => /your-favorite-places/north-america.html 
     [indented] =>  North America 
    ) 
+2

我們可以看到一個var_dump($ this-> containers)嗎? – ernie 2012-08-09 22:40:56

+0

你正在使用'str_replace'和一個數組? – alfasin 2012-08-09 23:12:45

+0

我認爲這是做到這一點的方法。無論如何,我會採取行動,只是用區域瀏覽來代替這三個詞 – decaye 2012-08-09 23:26:18

回答

1

物業$this->containers是對象的數組。

您將需要遍歷此數組,訪問每個對象的title屬性,並替換該屬性的字符串值(如果它是正確的字符串)。

所以......

擺脫這個塊:

$items = array(); // Create an array to load the objects in as values. 
foreach($this->containers as $object) { // Iterate through $this->containers, which is an array. Load the object into temporary variable $object. 
    if (strpos($object->title, 'Your Favorite Places') !== false) { // If the title property contains a string you're looking for, replace it with the value you want. 
     $object->title = str_replace('Your Favorite Places', 'Browse By Region', $object->title); 
    } 
    if (strpos($object->indented, 'Your Favorite Places') !== false) { // Should work with any property value with whitespaces also. 
     $object->indented = str_replace('Your Favorite Places', 'Browse By Region', $object->indented); 
    } 
    $items[] = $object; // Load the object into array $items. 
} 

$items = str_replace("Your Favorite Places", "Browse By Region", $this->containers); 

這條線將其更換210

編輯:我添加了一種方法來檢查部分字符串而不是整個字符串,並且替換部分字符串匹配以保留空格。

+0

這將完成工作。謝謝,Stegrex和其他人。然而,儘管它對沒有空間的物體起作用。所以當我將標題中的值更改爲縮進並嘗試這種方式時,它並未取代。我最初的想法是它似乎並不喜歡這個空間......如果你看着轉儲......在「你最喜歡的地方」的「縮進」對象中有一點空白,那可能是爲什麼? (對不起,這是一個print_r,我放在那裏......在我的var_dump中,你可以在某些地方看到空間,而在其他地方看不見) – decaye 2012-08-10 16:04:09

+0

@decaye是的,空格是我匹配的條件不會爲'$ object->縮進'工作。我修改了我的代碼,以舉例說明如何檢查子字符串,然後僅替換該部分,從而保留您擁有的任何空白。 – Stegrex 2012-08-10 16:17:01

+0

謝謝,工作很好。 – decaye 2012-08-13 17:13:21

0

如果我猜的話,我想說的字符串「你最喜歡的地方「在$ this-> containers的關鍵字中。雖然str_replace()將對數組進行迭代,但它會遍歷值而不是鍵。

我想嘗試這樣的:從Stegrex

foreach($this->containers as $key => $value) { 
    if ($key == "Your Favorite Places") { 
     $items["Browse By Region"] = $value; 
    } else { 
     $items[$key] = $value; 
    } 
} 

編輯:在循環中變更=來=>

+0

嗨,厄尼。我得到一個解析錯誤與該片段 – decaye 2012-08-09 22:55:49

+0

你不需要'else'部分... – alfasin 2012-08-09 23:13:30

+0

好吧,我拿出了其他的東西,也使$ key = Svalue成爲$ key => $ value。發生了什麼事是下拉框只填充了單詞'畫廊'。其他的東西都沒有了:) – decaye 2012-08-09 23:25:32

相關問題