Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换PHP对象数组中的字符串_Php_Joomla - Fatal编程技术网

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

替换PHP对象数组中的字符串,php,joomla,Php,Joomla,我原以为这很容易,但结果却很难。我所要做的就是替换对象中的值,然后打印出更改后的字符串。顺便说一下,这是在Joomla内部。这并不重要,只是解释代码中所有的JHTML/和JURi内容 我一直在尝试的代码是 <?php // Display the child select box. if (isset($this->containers) && count($this->containers)): $items = str_replace("Your

我原以为这很容易,但结果却很难。我所要做的就是替换对象中的值,然后打印出更改后的字符串。顺便说一下,这是在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;

?>

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

我想试试这样的东西:

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

从Stegrex编辑:Changed=to=>in-loop

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

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

所以

清除此块:

将其替换为以下行:

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

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

我们可以看到一个var_dump($this->containers)吗?您正在使用数组来使用
str_replace
?我认为这是一种方法。无论如何,我都要这样做,这样它就用Browse by RegionOK替换了这三个词。我取出了else,并将$key=Svalue转换为$key=>$value。发生的情况是下拉框中只填充了单词“Gallery”。其他一切都消失了:)OK,else语句返回到代码中……下拉列表重新填充,但字符串仍然存在。没有替换。这将完成工作。谢谢,Stegrex和其他人。然而,尽管它对没有空间的对象有效。因此,当我将值从title更改为indented并尝试这种方式时,它没有替换。我最初的想法是,它似乎不喜欢这个空间……如果你在垃圾堆中查看……在“缩进”对象中有一个小空格表示“你最喜欢的地方”,这是为什么?(对不起,那是一个打印文件,我放在那里……在我的变量转储中,你可以在某些地方看到空格,而在其他地方看不到。)@decaye是的,空白是我的匹配条件对
$object->indented
无效的原因。我修改了我的代码,给出了一个示例,说明如何检查子字符串,然后只替换该部分,保留所有的空白。
$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.
}