dpthuyh1678 2016-03-10 02:32
浏览 50
已采纳

如何将JSON字符串更改为单个数组

I have a json string, now i want it to become another string like an array and the $key is the same with $value

original string:

$input='
{
        "label_values":[
        "OK1","OK2","OK3"
        ]
}'

tried json_decode

$obj = json_decode($input);
$results = $obj->{'label_values'};
$result = implode(",", $results);

and $result export OK1,OK2,OK3

$key is the same with $value,i want it change to below example

Array(
        "OK1"=>"OK1",
        "OK2"=>"OK2",
        "OK3"=>"OK3"
)

Any help is greatly appreciated

  • 写回答

1条回答 默认 最新

  • doufu8588 2016-03-10 02:38
    关注

    You can simply use array_combine().

    array_combine() returns an array using first argument as keys and second argument as values:

    $result = array_combine( $results, $results );
    

    <kbd>3v4l demo</kbd>


    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败