如何在现有JSON数组中推送单个元素?

时间:2019-03-30 11:50:42

标签: php arrays json

我要在其中添加单个json元素的数组。这是数组。

"six": {
            "donnes_table_two": [
                {
                    "denomination_de_vente": "value",
                    "marques": "value"
                },
                {
                    "denomination_de_vente": "value",
                    "marques": "value"
                }
            ]
        }

我想在添加所有看起来要显示的ID之后在每个数组元素中添加ID。

"six": {
            "donnes_table_two": [
                {   "id" : "1",
                    "denomination_de_vente": "value",
                    "marques": "value"
                },
                {  
                    "id" = "1",
                    "denomination_de_vente": "value",
                    "marques": "value"
                }
            ]
        }

ID在每个元素中都相同。我可以不使用循环吗?有任何PHP函数吗?

5 个答案:

答案 0 :(得分:1)

因此,如果它只是一个字符串。那么您可以使用PHP str_replace"id":"1"之前添加"denomination_de_vente",例如下面的示例

<?php
$json = '{"six":{
            "donnes_table_two": [
                {
                    "denomination_de_vente": "value",
                    "marques": "value"
                },
                {
                    "denomination_de_vente": "value",
                    "marques": "value"
                }
            ]
        }}';

//$json = json_encode($yourObject);//if it is an php object or Array
$json = str_replace('"denomination_de_vente"','"id":"1","denomination_de_vente"',$json);
print_r(json_decode($json,true));// here 'true' to get result as array in your case
?>

Live Demo

输出

Array
(
    [six] => Array
        (
            [donnes_table_two] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [denomination_de_vente] => value
                            [marques] => value
                        )

                    [1] => Array
                        (
                            [id] => 1
                            [denomination_de_vente] => value
                            [marques] => value
                        )

                )

        )

)

答案 1 :(得分:1)

另一种方法使用array_walk

 $arr = json_decode( '{"six": {
        "donnes_table_two": [
            {
                "denomination_de_vente": "value",
                "marques": "value"
            },
            {
                "denomination_de_vente": "value",
                "marques": "value"
            }
        ]
    }}', true);
    array_walk ($arr['six']['donnes_table_two'], function (&$item)
          { $item['id'] = 1;});

     echo json_encode ($arr,JSON_PRETTY_PRINT );

演示here

答案 2 :(得分:0)

$json_value = '{
                  "six": {
                            "donnes_table_two": [
                                {
                                    "denomination_de_vente": "value",
                                    "marques": "value"
                                },
                                {
                                    "denomination_de_vente": "value",
                                    "marques": "value"
                                }
                            ]
                        }
              }';

$array_value = json_decode($json_value, true);      

 $total_inner_arrays = 1;
 for($i=0; $i<=$total_inner_arrays;$i++)
 {
    $array_value['six']['donnes_table_two'][$total_inner_arrays]['id'] = 1; 
 } 

$final_result = json_encode($array_value);

答案 3 :(得分:0)

您也可以这样操作:

<?php
$str = <<<EOD
{
    "six": {
        "donnes_table_two": [
            {
                "denomination_de_vente": "value",
                "marques": "value"
            },
            {
                "denomination_de_vente": "value",
                "marques": "value"
            }
        ]
    }
}
EOD;

print '<pre>';
$response = json_decode($str, true);
print_r('Before:');
print_r($response);
$array = array_map(function ($x) {$x['id'] = 1; return $x; }, $response['six']['donnes_table_two']);
$response['six']['donnes_table_two'] = $array;
print_r('After:');
print_r($response);

答案 4 :(得分:0)

您可以使用 array_map 函数

$json = '{
    "six": {
        "donnes_table_two": [
            {
                "denomination_de_vente": "value",
                "marques": "value"
            },
            {
                "denomination_de_vente": "value",
                "marques": "value"
            }
        ]
    },
    "seven": {
        "donnes_table_two": [
            {
                "denomination_de_vente": "value",
                "marques": "value"
            },
            {
                "denomination_de_vente": "value",
                "marques": "value"
            }
        ]
    }
}';

$jArray   = json_decode($json, true);
$newArray = array_map(function($element){

  $element['donnes_table_two'][0] = array_merge(
                                    array('id' => 1), 
                                    $element['donnes_table_two'][0]
                                );
  $element['donnes_table_two'][1] = array_merge(
                                    array('id' => 1), 
                                    $element['donnes_table_two'][0]
                                );

return $element;
}, $jArray);


echo '<pre>';
print_r($newArray);

结果:-

 Array
 (
    [six] => Array
    (
        [donnes_table_two] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [denomination_de_vente] => value
                        [marques] => value
                    )

                [1] => Array
                    (
                        [id] => 1
                        [denomination_de_vente] => value
                        [marques] => value
                    )

            )

    )

[seven] => Array
    (
        [donnes_table_two] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [denomination_de_vente] => value
                        [marques] => value
                    )

                [1] => Array
                    (
                        [id] => 1
                        [denomination_de_vente] => value
                        [marques] => value
                    )

            )

    )
  )