如何解决Laravel中的数组推送问题

时间:2019-11-23 11:48:39

标签: php laravel

我有一个两个字段 1。津贴(包含此数据)

{"medical":"600","transport":"350","food":"900"}

和另一个 2。房屋租金(包含此数据)

2550.00

现在我想在第三列中得到一个结果

{"medical":"600","transport":"350","food":"900","house_rent":"2550.00"}

到目前为止,我已经尝试过

$allowances=json_decode($salary->allowances);
$house_rent = array('House Rent' => $salary->house_rent);
$allowances_logs=array_push($allowances,$house_rent);
$salary->allowances_logs = $allowances_logs;

但是它给了我以下错误"array_push() expects parameter 1 to be array, object given"。帮助我达到这个结果。任何帮助将不胜感激

3 个答案:

答案 0 :(得分:1)

首先,将true作为第二个参数添加到json_decode(),您将以数组而不是对象的形式检索结果。

第二,使用两个数组,执行以下操作:

$merged = array_merge($arr1, $arr2);

答案 1 :(得分:0)

truejson_decode()中添加第二个参数,因此您可以在下面的示例中看到

$mainArr = json_decode('{"medical":"600","transport":"350","food":"900"}',true);
$house_rent = array('House Rent' => 2550.00);
$printArr = array_merge($mainArr, $house_rent);
print_r(json_encode($printArr));

输出

{"medical":"600","transport":"350","food":"900","House Rent":2550}

答案 2 :(得分:0)

首先将您的json转换为数组。 您可以使用 json_decode php函数来做到这一点。

json_decode 函数将json转换为对象或关联数组。 此函数的第一个参数是要解码的json字符串。 当第二个参数为TRUE时,返回的对象将转换为关联数组。

{
"continue": {
    "clcontinue": "12|Articles_with_unsourced_statements_from_July_2019",
    "continue": "||"
},
"query": {
    "pages": {
        "12": {
            "pageid": 12,
            "ns": 0,
            "title": "Anarchism",
            "categories": [
                {
                    "ns": 14,
                    "title": "Category:All articles lacking reliable references"
                },
                {
                    "ns": 14,
                    "title": "Category:All articles with unsourced statements"
                },
                {
                    "ns": 14,
                    "title": "Category:Anarchism"
                },
                {
                    "ns": 14,
                    "title": "Category:Anti-capitalism"
                },
                {
                    "ns": 14,
                    "title": "Category:Anti-fascism"
                },
                {
                    "ns": 14,
                    "title": "Category:Articles containing French-language text"
                },
                {
                    "ns": 14,
                    "title": "Category:Articles containing Spanish-language text"
                },
                {
                    "ns": 14,
                    "title": "Category:Articles lacking reliable references from March 2019"
                },
                {
                    "ns": 14,
                    "title": "Category:Articles prone to spam from November 2014"
                },
                {
                    "ns": 14,
                    "title": "Category:Articles with short description"
                }
            ]
        },
        "307": {
            "pageid": 307,
            "ns": 0,
            "title": "Abraham Lincoln"
        },
        "308": {
            "pageid": 308,
            "ns": 0,
            "title": "Aristotle"
        },
        "339": {
            "pageid": 339,
            "ns": 0,
            "title": "Ayn Rand"
        },
        "627": {
            "pageid": 627,
            "ns": 0,
            "title": "Agriculture"
        },
        "666": {
            "pageid": 666,
            "ns": 0,
            "title": "Alkali metal"
        },
        "674": {
            "pageid": 674,
            "ns": 0,
            "title": "Anatomy"
        },
        "736": {
            "pageid": 736,
            "ns": 0,
            "title": "Albert Einstein"
        },
        "740": {
            "pageid": 740,
            "ns": 0,
            "title": "Allah"
        },
        "746": {
            "pageid": 746,
            "ns": 0,
            "title": "Azerbaijan"
        }
    }
}

现在您可以将新项目添加到阵列中。

$testJson = '{"medical":"600","transport":"350","food":"900"}';
$testArr = json_decode($testJson, true);

Output - Array ( [medical] => 600 [transport] => 350 [food] => 900 )

您的数组现在看起来像这样。

$testArr['house_rent'] = '2550.00';

最后,将数组转换为json。为此,您可以使用php json_encode 函数。

Array ([medical] => 600 [transport] => 350 [food] => 900 [house_rent] => 2500.00)

完整示例

$testJson = json_encode($testArr);