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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度