替换数组PHP中的值

时间:2017-07-26 18:23:08

标签: php replace

我需要在多层数组中进行搜索和替换。我只是无法让它工作。我知道这很简单。我应该看一个步行或字符串替换。似乎没什么用。

我这个数组:

ATWS\AutotaskObjects\QueryResponse Object
(
    [queryResult] => ATWS\AutotaskObjects\ATWSResponse Object
        (
            [EntityResults] => ATWS\AutotaskObjects\ArrayOfEntity Object
                (
                    [Entity] => Array
                        (
                            [0] => ATWS\AutotaskObjects\Entity Object
                                (
                                    [Fields] =>
                                    [UserDefinedFields] => ATWS\AutotaskObjects\ArrayOfUserDefinedField Object
                                        (
                                            [UserDefinedField] =>
                                        )

                                    [id] => 35
                                    [AccountID] => 218
                                    [AlertTypeID] => 1
                                    [AlertText] => test
                                )

                            [1] => ATWS\AutotaskObjects\Entity Object
                                (
                                    [Fields] =>
                                    [UserDefinedFields] => ATWS\AutotaskObjects\ArrayOfUserDefinedField Object
                                        (
                                            [UserDefinedField] =>
                                        )

                                    [id] => 36
                                    [AccountID] => 218
                                    [AlertTypeID] => 2
                                    [AlertText] => test2
                                )

                            [2] => ATWS\AutotaskObjects\Entity Object
                                (
                                    [Fields] =>
                                    [UserDefinedFields] => ATWS\AutotaskObjects\ArrayOfUserDefinedField Object
                                        (
                                            [UserDefinedField] =>
                                        )

                                    [id] => 37
                                    [AccountID] => 218
                                    [AlertTypeID] => 3
                                    [AlertText] => test3
                                )

                        )

                )

            [EntityResultType] => accountalert
            [EntityReturnInfoResults] => ATWS\AutotaskObjects\ArrayOfEntityReturnInfo Object
                (
                    [EntityReturnInfo] => Array
                        (
                            [0] => ATWS\AutotaskObjects\EntityReturnInfo Object
                                (
                                    [DatabaseAction] => None
                                    [DuplicateStatus] => ATWS\AutotaskObjects\EntityDuplicateStatus Object
                                        (
                                            [Found] =>
                                            [Ignored] =>
                                            [MatchInfo] =>
                                            [Fields] =>
                                            [UserDefinedFields] =>
                                        )

                                    [EntityId] => 35
                                    [Message] =>
                                    [Fields] =>
                                    [UserDefinedFields] =>
                                )

                            [1] => ATWS\AutotaskObjects\EntityReturnInfo Object
                                (
                                    [DatabaseAction] => None
                                    [DuplicateStatus] => ATWS\AutotaskObjects\EntityDuplicateStatus Object
                                        (
                                            [Found] =>
                                            [Ignored] =>
                                            [MatchInfo] =>
                                            [Fields] =>
                                            [UserDefinedFields] =>
                                        )

                                    [EntityId] => 36
                                    [Message] =>
                                    [Fields] =>
                                    [UserDefinedFields] =>
                                )

                            [2] => ATWS\AutotaskObjects\EntityReturnInfo Object
                                (
                                    [DatabaseAction] => None
                                    [DuplicateStatus] => ATWS\AutotaskObjects\EntityDuplicateStatus Object
                                        (
                                            [Found] =>
                                            [Ignored] =>
                                            [MatchInfo] =>
                                            [Fields] =>
                                            [UserDefinedFields] =>
                                        )

                                    [EntityId] => 37
                                    [Message] =>
                                    [Fields] =>
                                    [UserDefinedFields] =>
                                )

                        )

                    [Fields] =>
                    [UserDefinedFields] =>
                )

            [Errors] => ATWS\AutotaskObjects\ArrayOfATWSError Object
                (
                    [ATWSError] =>
                    [Fields] =>
                    [UserDefinedFields] =>
                )

            [ReturnCode] => 1
        )

)

我想将所有单词'Test'替换为'Yes'

我一直在尝试使用

$replacevalue = 'Jon';
function array_replacing(&$item, $key)
{
    if($value== 'test')
        $item = $replacevalue;
}

array_walk_recursive($result, 'array_replacing');

但是没有用。我甚至尝试过str_replace。没运气。你能帮忙吗?

完整代码:(减去授权)

$authWsdl = 'https://webservices.autotask.net/atservices/1.5/atws.wsdl';
$opts = array('trace' => 1);
$client = new ATWS\Client($authWsdl, $opts);
$zoneInfo = $client->getZoneInfo($username);

//print_r($zoneInfo);

$authOpts = array(
    'login' => $username,
    'password' => $password,
    'trace' => 1,   // Allows us to debug by getting the XML requests sent
);
$wsdl = str_replace('.asmx', '.wsdl', $zoneInfo->getZoneInfoResult->URL);
$client = new ATWS\Client($wsdl, $authOpts);

$query = new ATWS\AutotaskObjects\Query('AccountAlert');

$Alerttexttoreplace = new ATWS\AutotaskObjects\QueryField('AlertText');
$lookvalue = 'test';
$Alerttexttoreplace->addExpression('contains', $lookvalue);

$query->addField($Alerttexttoreplace);

//$AccountID->$query($AccountID)
//print_r($AccountID)
//$updatequery = $query->update($query)

// If you want to debug the XML produced by the Query object
// print($query->asXml());

// Print the results of the query
//print_r($client->query($query));

//set limited aray
$result = $client->query($query);
//print_r ($result);
//die();
$final = $result->queryResult->EntityResults->Entity;
//print_r ($final);

//Replace value in array

$replacevalue = 'Jon';

foreach ($final as $key=>$val) 
    {
        if ($key='AlertText' ) 
        {
            //$final = str_replace($lookvalue,$replacevalue,$val=>'AlertText');
            echo 'old';
            print_r($val);
            echo 'new';
            print_r($newval);
        }

    }





//print_r ($final);
die(end);

1 个答案:

答案 0 :(得分:1)

您尚未定义$value,只需更改为:

function array_replacing(&$item, $key)
{
    if($item == 'test')
        $item = $replacevalue;
}