English 中文(简体)
阵列上方
原标题:Using str_replace on array

我提出一个问题,我似乎无法找到解决办法。 In

foreach( $results->result as $item )

the following code

$item->title = str_replace( " - Home", "", $item->title );

总是犯错误

<>可见的致命错误:> 不能将等级块的物体改装为地块。

我想str_replace能够处理阵列? 我需要作什么改动才能完成这项工作?

感谢任何建议!

最佳回答

它不是阵列,而是标的,,正如“<>m>......”的错误所言,你是否得到任何机会的<代码>json_decode(?

然而,解决办法很简单,因为它涉及一系列问题:

$item->title = str_replace( " - Home", "", (array) $item->title );

Another point about this is that if you are wanting to modify the data held in $results->result, and not just a copy of it, you will need your foreach to be:

foreach( $results->result as &$item )

......并作为参考而不是复制件......

问题回答

。 见var_dump($item-> entitled),以了解物体所看的情况。

Also, you loop won t do what you expect, after the loop has finished, all objects in $results->result will still have the same values (use foreach($results->result as &$item) (pass by reference))

var_dump on $item-> ,因为它是一个阵列,是一个物体。 只能处理实际阵列和护卫。 如果你重新期望这成为一阵,那么你在别处可能会有一些问题,我将研究这个问题。

If you re just looking for a patch, you might be able to get away with casting it. If everything else is working, I would do this because it really has the fewest side-effects:

// convert it to an array before passing it through str_replace
// (array) $item->title
// then convert it back to its original form by casting the result back
// (object) str_replace
$item->title = (object) str_replace( " - Home", "", (array) $item->title );

如果<代码><><>> > > /代码>必须是一阵,你可以不必填写<代码>(目标),但我认为,在设定<代码><<<>>><<<>>/代码>时,跟踪这一条,其价值是正确的。


BTW——如果你在休息室外重新规划使用<代码>$-> ,你还需要确保你使用提及方式更新项目本身:

foreach($results->result as &$item)
{
     $item->title = (object) str_replace( " - Home", "", (array) $item->title );
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...