替换对象数组 PHP 中的字符串


Replace string in Object Array PHP

我以为这很容易,但结果很难。我要做的就是替换对象中的一个值,然后打印出更改的字符串。顺便说一下,这是在Joomla内部。这并不重要,只是解释代码中的所有JHTML/和JURi内容。

我一直在尝试的代码是...

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

属性$this->containers是一个对象数组。

您需要循环访问此数组,访问每个对象的 title 属性,并替换该属性的字符串值(如果它是正确的字符串)。

所以。。。

摆脱此块:

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

将其替换为以下行:

$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.
}

编辑:我添加了一种检查部分字符串而不是整个字符串的方法,并替换部分字符串匹配以保留空格。

如果我猜,我会说字符串"你最喜欢的地方"在$this->容器的键中。 虽然 str_replace() 将迭代数组,但它迭代值而不是键。

我会尝试这样的事情:

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

从 Stegrex 编辑:在循环中更改为 =>