Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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_Loops_Stdclass - Fatal编程技术网

用php操作对象数组中的字符串

用php操作对象数组中的字符串,php,loops,stdclass,Php,Loops,Stdclass,我有一个stdClass数组: array (size=2) 0 => object(stdClass)[2136] public 'id' => string '1946' (length=4) public 'office' => string 'test' (length=4) public 'level1' => string 'test level 1' (length=12) 1 => o

我有一个stdClass数组:

array (size=2)
  0 => 
    object(stdClass)[2136]
      public 'id' => string '1946' (length=4)
      public 'office' => string 'test' (length=4)
      public 'level1' => string 'test level 1' (length=12)

  1 => 
    object(stdClass)[2135]
      public 'id' => string '1941' (length=4)
      public 'office' => string 'test' (length=4)
如何用span标记包装每个“测试”值?

foreach($stdClass形式的数组)
foreach ($array as $stdClass)
    foreach ($stdClass as &$value) // reference
        if ($value === "test")
            $value = "<span>".$value."</span>";
foreach($stdClass as&$value)//引用 如果($value==“测试”) $value=“”.$value。”;

只需迭代数组和类,因为它们都可以使用foreach进行迭代。(通过引用迭代类,否则不会更改)

要将所有与单词“test”匹配的对象值包装在一个范围内,您需要迭代对象属性以及数组本身。您可以使用foreach执行此操作:

foreach ($object in $array) {
    foreach ($property in $object) {
        if ($object->$property == 'test') {
            $object->$property = "<span>{$object->property}</span>";
        }
    }
}
foreach($array中的对象){
foreach($对象中的属性){
如果($object->$property=='test'){
$object->$property=“{$object->property}”;
}
}
}
如果要将单词test的所有实例包装在带有span的属性值中,可以使用preg_replace如下所示:

foreach ($object in $array) {
    foreach ($property in $object) {
        $object->$property = preg_replace('/\b(test)\b/', '<span>$1</span>', $object->$property);
    }
}
foreach($array中的对象){
foreach($对象中的属性){
$object->$property=preg_replace(“/\b(测试)\b/”、“$1”、$object->$property);
}
}
给定字符串“ThisTestisfortestpurposesas-test”,上述调用将给出以下内容:

This <span>test</span> is for testing purposes as a <span>test</span>.
此测试作为测试用于测试目的。

您遇到了哪些具体问题?我找不到使用foreach或Loop的方法。他不想打印,但想更改数据。我意识到,就在我单击“提交”按钮时。:)美丽的它工作!!!