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

如何将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 );
    

    3v4l demo


    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题