0

Problem is as follows:

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

I then want to convert the file into a array so that I can have a piece of code that will select a random name from the 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.

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 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 file. So I can't even get to the part where I want to use the file that's been converted into a array so I can have code to select a random name + associated rep code and display it.

Any help would be appreciated.

3
  • 2
    The first example looks like the random element part depends on $output being an array, but it's a string. Commented May 14, 2019 at 20:50
  • 2
    Use array_rand() to pick a random element of an array. Commented May 14, 2019 at 20:53
  • Thanks a lot for the feedback. Works. Commented May 15, 2019 at 6:08

1 Answer 1

2

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"];
Sign up to request clarification or add additional context in comments.

1 Comment

Your code works exceptional. Thanks a lot @AbraCadaver.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.