duanmoen784988 2017-11-16 14:10
浏览 66
已采纳

仅从数组中提取值,以使用PHP在JSON中对它们进行编码

This has probably been asked many times but I can't find a solution for my case.

This is my array :

$request=Array ( 
        [0] => Array ( [staName] => Auditorium Stravinsky 2m2c ) 
        [1] => Array ( [staName] => Geneva Arena ) 
        [2] => Array ( [staName] => Les Docks ) 
        [3] => Array ( [staName] => Kheops ) 
    )

And i need an output as follows as JSON:

"Auditorium Stravinsky 2m2c ","Geneva Arena","Les Docks","Kheops"

My current code is as follows:

    foreach($request as $value)
    {
        $names[]=$value;
    }

    $jsonValue = json_encode(array_values($names));

    print_r($jsonValue);

And my current output is as follows in JSON format:

[{"staName":"Auditorium Stravinsky 2m2c "},{"staName":"Geneva Arena"},{"staName":"Les Docks"},{"staName":"Kheops"}]

How can i stop "staName " from being outputed?

Many thanks in advance and please be considerate of my post as this is only the second one I make on this site.

  • 写回答

3条回答 默认 最新

  • douhan8009 2017-11-16 14:15
    关注
    <?php
    
    $request=Array (
            0 => Array ( 'staName' => 'Auditorium Stravinsky 2m2c' ) ,
            1 => Array ( 'staName' => 'Geneva Arena' ) ,
            2 => Array ( 'staName' => 'Les Docks' ) ,
            3 => Array ( 'staName' => 'Kheops' )
        );
    
    
    $newArray=array();
    for($i=0;$i<count($request);$i++){
        $newArray[$i]=$request[$i]['staName'];
    }
    
    $newArray=json_encode($newArray,true);
    print_r($newArray);
    

    And the output is a merged json:

    ["Auditorium Stravinsky 2m2c","Geneva Arena","Les Docks","Kheops"]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解