PHP对象查找和设置

时间:2018-02-14 10:50:19

标签: php json

我有一个看起来像这样的对象:

{                                                                           
    "message": {                                                               
        "attachment": {                                                        
            "payload": {                                                       
                "buttons": [                                                   
                    {                                                          
                        "title": "View",                                       
                        "type": "web_url",                                     
                        "url": "https://google.com"                            
                    }                                                          
                ],                                                             
                "template_type": "button",                                     
                "text": "You have ##likes_count## new likes in your item."  
            },                                                                 
            "type": "template"                                                 
        }                                                                      
    }                                                                          
}

我想用特定值替换“text”属性的值,比如说“5”。我尝试了str_replace('##likes_count##', '5', $message),但它似乎没有找到要替换的字符串。我可以遍历该对象并找到“text”属性并替换其值,但该属性的位置不是永久性的。有时它在“按钮”或“附件”下。

有没有办法在对象中的任何地方查找“text”属性并替换它的值?任何帮助将不胜感激:)

编辑:我知道这是一个字符串。我的意思是我有一个具有类似结构的对象。我可以将该对象转换为字符串并执行str_replace但我需要将其转换回我不想做的对象。

2 个答案:

答案 0 :(得分:0)

您必须记住,您必须分配str_replace的结果,如下所示:

$message = '"message": {
            "attachment": {
                "type": "template",
                "payload": {
                    "template_type": "button",
                    "text": "You have ##likes_count## new likes in your item.",
                    "buttons": [
                        {
                            "type": "web_url",
                            "title": "View",
                            "url": "https://google.com"
                        }
                    ]
                }
            }
        }';

$message = str_replace('##likes_count##', '5', $message);
print_r($message);

答案 1 :(得分:0)

首先,没有涉及PHP对象。只是从某个远程端点返回的JSON数据。

可以通过@Michał Szczech显示的路线 - 也就是让数据采用字符串格式并替换那里出现的所有针。

或者您可以通过将JSON字符串解码为PHP数组/对象的路径并在那里进行替换。这种方法的优点是只有在其键被称为text时才替换该值。

考虑这样的脚本:

<?php
/**
 * Replace a value stored deep within a nested array.
 *  Only in case its key is called 'text'
 *  Use pattern defined as a global constant `PATTERN`
 *
 * @param  array     $hayStack
 * @param  string    $needle
 * @param  string    $replaceString
 */
function deepReplace(&$hayStack, $needle, $replaceString)
{
    foreach ($hayStack as $key => &$value) {
        if ($key === $needle) {
            $hayStack[$key] = preg_replace(
                '/' . PATTERN . '/',
                $replaceString,
                $value
            );
        }
        if (is_array($value)) {
            deepReplace($value, $needle, $replaceString);
        }
    }
}
define('PATTERN', '##likes_count##');

// data provided from your source in JSON format
$data = '
    {
        "message": {
            "attachment": {
                "payload": {
                    "buttons": [
                        {
                            "title": "View",
                            "type": "web_url",
                            "url": "https://google.com"
                        }
                    ],
                    "template_type": "button",
                    "text": "You have ##likes_count## new likes in your item."
                },
                "type": "template"
            }
        }
    }
';

// decode JSON into a nested PHP array
$nestedArray = json_decode($data, true);

// recursively replace occurrences of '##likes_count##' within 'text' key
deepReplace($nestedArray, 'text', 5);

// replacement complete - go on processing it as you like.. 
// I am just printing the encoded string to prove it works
echo json_encode($nestedArray, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);

脚本打印一个这样的字符串:

{
    "message": {
        "attachment": {
            "payload": {
                "buttons": [
                    {
                        "title": "View",
                        "type": "web_url",
                        "url": "https://google.com"
                    }
                ],
                "template_type": "button",
                "text": "You have 5 new likes in your item."
            },
            "type": "template"
        }
    }
}