dtdr57046 2019-05-14 20:45 采纳率: 100%
浏览 119
已采纳

将json文件转换为php数组,从数组中选择随机名称

Problem is as follows:

I'm exporting a list of names and values(repCode) attached to each name from excel into a json file.

I then want to convert the json file into a php array so that I can have a piece of code that will select a random name from the php array and display the random name(and value(repCode) attached to the name).

I've tried many options so far but I keep running into problems that I'm struggling to find a solution for. One example would be:

<?php

$jsondata = file_get_contents("Names.json");
$json = json_decode($jsondata, true);
$output = '<ul>';
foreach($json['Reps']as $reps){
$output .='<h4>' .$reps['Client']."<h4>";
$output .= "<li>".$reps['Code']."</li>";

}

$output .= "</ul>";
$element = $output[mt_rand(0, count($output) - 1)];

echo $element;

?>

That doesn't work.

json File as follow: "Names.json"

{
"Reps": [

{"Client":"Jack",
    "repCode":"tt1790861"},
{"Client":"James",
    "repCode":"tt1790862"},
{"Client":"Sam",
    "repCode":"tt1790863"},
{"Client":"Hendry",
    "repCode":"tt1790864"},
{"Client":"Samone",
    "repCode":"tt1790865"},
{"Client":"Judy",
    "repCode":"tt179086"},
{"Client":"Jake",
    "repCode":"tt1790867"},
{"Client":"Amy",
    "repCode":"tt1790868"},
{"Client":"Brandon",
    "repCode":"tt1790869"},
{"Client":"Blake",
    "repCode":"tt17908610"},
{"Client":"Rick",
    "repCode":"tt17908611"},
{"Client":"Morty",
    "repCode":"tt17908612"}       
]
}

And then below is some php code:

<?php
  // JSON string
  $someJSON = "Names.json";

  // Convert JSON string to Array
  $someArray = json_decode($someJSON, true);
  print_r($someArray);        // Dump all data of the Array
  echo $someArray[0]["Client"]; // Access Array data

?>

I'm getting no result when I echo out the json file. So I can't even get to the part where I want to use the json file that's been converted into a php array so I can have code to select a random name + associated rep code and display it.

Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • ds3422222 2019-05-14 20:55
    关注

    In your first example you're trying to use $output as an array, it's not. Also, you're not accessing the keys of $element:

    $element = $json['Reps'][mt_rand(0, count($json['Reps']) - 1)];
    //or
    $element = $json['Reps'][array_rand($json['Reps'])];
    echo $element['Client'];
    echo $element['repCode'];
    

    For your second example, you're not actually loading the JSON file and then you forget the Reps key:

    $someJSON = file_get_contents("Names.json");
    $someArray = json_decode($someJSON, true);
    print_r($someArray);
    echo $someArray["Reps"][0]["Client"];
    //or random
    echo $someArray["Reps"][array_rand($someArray["Reps"])]["Client"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥100 某宝多次访问被拒绝,求解
  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!