0

I currently have JSON data which looks like below

[
  {
    "company": "East Lifestyle Limited",
    "phone1": "020 8877 5900",
    "website": "http://www.east.co.uk"
  },
  {
    "company": "Wandsworth Security",
    "phone1": "020 8870 9745",
    "website": ""
  }]

in my php I have

 $jsondata = $this->input->post('leads');
$data = json_decode($jsondata, true);


  $id = $data['company'];

echo "$id";

When trying to show the company, i am getting the following error :

A PHP Error was encountered

Severity: Notice

Message: Undefined index: company

Filename: controllers/leads.php

Line Number: 334

Also just out of curiosity, and sorry if this makes it a 2 part question - When doing the echo $id; will this only do echo the first company in the list or will it echo them all?

I'd like it to echo them all when it works.

Thanks

Scott O'connor
  • 133
  • 1
  • 1
  • 9
  • 1
    I'd suggest var dumping `$data`. Error is pretty clear. – ficuscr Jan 31 '18 at 21:57
  • Just did that and it gives me array(16) { ["company"]=> string(1) "d" Am i doing something right or wrong ? – Scott O'connor Jan 31 '18 at 21:59
  • As for part 2 to your question, you'll want to loop with a `foreach` or similar. First part is knowing the structure of the array you are getting back from that JSON. – ficuscr Jan 31 '18 at 22:00
  • 1
    Hint: `$data[0]['company']`... – IncredibleHat Jan 31 '18 at 22:00
  • @IncredibleHat thank you, I did upvote your comment, but accidently clicked it twice and now wont let me upvote you again. Sorry – Scott O'connor Jan 31 '18 at 22:03
  • 1
    anticipating... look at http://php.net/manual/en/function.array-column.php, but even before that play around with `foreach` on that array till you get how things work. `var_dump` is your friend. – ficuscr Jan 31 '18 at 22:03

0 Answers0