奇怪的数组并不能做替换

时间:2017-07-31 21:01:29

标签: php multidimensional-array

我有一个来自API的数组。我想用“是”替换“测试”一词

我尝试了各种各样的东西......你能帮忙吗?

这是阵列。

我尝试了字符串替换和其他一些。我认为它不是标准阵列。看起来价值观是错误的。请帮忙。

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
        )

)

奖金......可以用这个阵列完成吗? 好的奖金问题 如果它是什么

ATWS\AutotaskObjects\QueryResponse Object
(
    [queryResult] => ATWS\AutotaskObjects\ATWSResponse Object
        (
            [EntityResults] => ATWS\AutotaskObjects\ArrayOfEntity Object
                (
                    [Entity] => Array
                        (
                            [0] => ATWS\AutotaskObjects\Entity Object
                             (                          
                                    [id] => 35
                                    [AccountID] => 218
                                    [AlertTypeID] => 1
                                    [AlertText] => test
                                )

2 个答案:

答案 0 :(得分:1)

foreach ($entities as $entity) {
    $entity->AlertText = str_replace("test", "yes", $entity->AlertText);
}

这会遍历你的数组($ entities),找到' test'在每个实体的AlertText字段中,将其替换为是

答案 1 :(得分:-1)

为了在数组中搜索和替换,我使用它 - 因为PHP中没有内置函数

$your_array = array_replace($your_array,
    array_fill_keys(
        array_keys($your_array, $value_to_replace),
        $replacement_value
    )
);