如何在php中为数组中的每个对象添加新的键值对?

bqf10yzr  于 2023-04-10  发布在  PHP
关注(0)|答案(3)|浏览(146)

我的代码:场景1

$newarray = array();

foreach($rows as $k => $v) 
    {
           $newarray[$k] = $v;
           $newarray['newitem'] = 'Add this to existing object';   
    }
echo json_encode($newarray);

我也试过这个:方案2

$newarray = array();

foreach($rows as $k => $v) 
    {
           $newarray[$k] = $v;
           $newarray[$k]['newitem'] = 'Add this to existing object';   
    }
echo json_encode($newarray);

在场景1中,键值对只在第一个对象之后添加。它不会循环。在场景2中,我得到HTTP ERROR 500
我做错了什么?
更新:
现有输出:

[{"pid":"123","date":"2016-08-23 08:08:40","post_title":"AHHH"},{"pid":"223","date":"2016-08-23 08:08:40","post_title":"wwwwAHHH"},{"pid":"223","date":"2016-08-23 08:08:40","post_title":"xxxAHHH"}]

所需输出:

[{"pid":"123","date":"2016-08-23 08:08:40","post_title":"AHHH","newkey":"new value1"},{"pid":"223","date":"2016-08-23 08:08:40","post_title":"wwwwAHHH","newkey":"new value2"},{"pid":"223","date":"2016-08-23 08:08:40","post_title":"xxxAHHH","newkey":"new value3"}]
57hvy0tb

57hvy0tb1#

你可以制作一个包含你想要的内容的临时数组然后把它添加到newray中,所以创建一个临时数组,把新数据和现有数据相加,然后把它添加到你的newray中

$newarray = array();
foreach($rows as $k => $v) 
{
    $t = $v;
    $t['newitem'] = 'Add this to existing object';
    $newarray[$k] = $t;
}
echo json_encode($newarray);

编辑:
现在我知道$rows中的是一个JSON字符串,这是你想要的方法。你不能foreach过一个JSON字符串,你必须首先将它转换为PHP数据结构,然后foreach过它添加新数据,然后将它转换回JSON字符串

$rows = '[{"pid":"123","date":"2016-08-23 08:08:40","post_title":"AHHH"},{"pid":"223","date":"2016-08-‌​23 08:08:40","post_title":"wwwwAHHH"},{"pid":"223","date":"2016‌​-08-23 08:08:40","post_title":"xxxAHHH"}]';

$objArray = json_decode($rows);

$newarray = array();
foreach($objArray as $v) 
{

    $v->newitem = 'Add this to existing object';
    $newarray[] = $v;
}
print_r($newarray);
echo json_encode($newarray);

$newArray现在

Array
(
    [0] => stdClass Object
        (
            [pid] => 123
            [date] => 2016-08-23 08:08:40
            [post_title] => AHHH
            [newitem] => Add this to existing object
        )

    [1] => stdClass Object
        (
            [pid] => 223
            [date] => 2016-08-‌​23 08:08:40
            [post_title] => wwwwAHHH
            [newitem] => Add this to existing object
        )

    [2] => stdClass Object
        (
            [pid] => 223
            [date] => 2016‌​-08-23 08:08:40
            [post_title] => xxxAHHH
            [newitem] => Add this to existing object
        )

)

新的JSON字符串将是

[  {"pid":"123",
    "date":"2016-08-23 08:08:40",
    "post_title":"AHHH",
    "newitem":"Add this to existing object"
   },
   {"pid":"223",
    "date":"2016-08-\u200c\u200b23 08:08:40",
    "post_title":"wwwwAHHH",
    "newitem":"Add this to existing object"
   },
   {"pid":"223",
    "date":"2016\u200c\u200b-08-23 08:08:40",
    "post_title":"xxxAHHH",
    "newitem":"Add this to existing object"
   }
]
e4yzc0pl

e4yzc0pl2#

在场景1中,每次都将不同的值$v分配给同一个数组键,因此它只是被覆盖(最终将成为传递给foreach的最后一行的值):

$newarray['newitem'] = 'Add this to existing object';

场景2是正确的,但需要稍微修改一下。在场景2中,您添加了一个包含$v的数组,但随后直接覆盖了它:

$newarray[$k] = $v;
$newarray[$k]['newitem'] = 'Add this to existing object';

你需要给予这些项单独的键,所以一个解决方案可能是使用一个'olditem'键来分配初始值:

$newarray[$k]['olditem'] = $v;
$newarray[$k]['newitem'] = 'Add this to existing object';

根据您希望如何构造数组,您可以执行许多解决方案,但关键原则是确保值具有单独的键,以便它们不会相互覆盖。

s3fp2yjn

s3fp2yjn3#

您可以在同一个$row变量中添加新的键值。

$row = json_decode($row,true);
$row= array_map(function($a) {
          $a += ['newitem' => 'Add this to existing object'];
          return $a;
       }, $row);
    
echo json_encode($row);

相关问题