1

I need to do a search and replace in a multi layer array. I just have not been able to get it to work. I know it is something simple. Should I be looking at a walk or string replace. Nothing seems to work.

I this array:

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
        )

)

I want to replace all the words 'Test' with 'Yes'

I have been trying to use

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

array_walk_recursive($result, 'array_replacing');

But is not working. I even tried str_replace. No luck. Can you help?

Full Code: (minus authorization)

$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);
2
  • I think $value should be $item Commented Jul 26, 2017 at 18:38
  • No... still no change. I think the issue how deep it is in the array Commented Jul 26, 2017 at 20:58

1 Answer 1

1

You haven't defined $value, simply change to this:

function array_replacing(&$item, $key)
{
    if($item == 'test')
        $item = $replacevalue;
}
Sign up to request clarification or add additional context in comments.

4 Comments

that did not change anything. sorry
I was thinking something like
foreach ($final as $key=>$val) { if ($key='AlertText' ) { $newval = str_replace($lookvalue,$replacevalue,$val=>'AlertText'); echo 'old'; print_r($val); echo 'new'; print_r($newval); } }
Are you getting errors? Your input is an object not an array, that would explain it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.