0
 {
    "id": 833,
    "name": "mumbai",
    "state": "",
    "country": "IN",
    "coord": {
      "lon": 47.159401,
      "lat": 34.330502
    }
  },
  {
    "id": 2960,
    "name": "‘kolkata",
    "state": "",
    "country": "IN",
    "coord": {
      "lon": 36.321911,
      "lat": 34.940079
    }
  },

I want if i define $name = kolkata ; then it should show its id as 2960

using php guys please help

$name = kolkata;
$data = file_get_contents('city.json'); // put the contents of the file into a variable
$citys = json_decode($data);
norbitrial
  • 14,716
  • 7
  • 32
  • 59
ganesh rao
  • 52
  • 1
  • 8
  • 1
    https://stackoverflow.com/questions/4742903/php-find-entry-by-object-property-from-an-array-of-objects might help you. (P.S. the fact that the data used to be JSON is irrelevant. Once you've decoded it, it's just a list of objects. If you've been searching for solutions in relation to JSON, stop doing that and start searching for solutions relating to arrays and objects in PHP) – ADyson Mar 23 '20 at 16:00
  • @ADyson its not helping – ganesh rao Mar 23 '20 at 16:13
  • Show what you tried from that, please, and what went wrong. I can't guess what specific problem you might have had. – ADyson Mar 23 '20 at 16:34
  • i want to get the id from the above json when i search the name – ganesh rao Mar 23 '20 at 16:45
  • @ADyson let me explain you as sql databse – ganesh rao Mar 23 '20 at 16:46
  • @ADyson $sql = "SELECT id FROM table_name where name = 'kolkata' "; just like this i want in json – ganesh rao Mar 23 '20 at 16:48
  • 1
    Read my first comment again. After you write `json_decode()` then your `$citys` data is **NOT** JSON anymore. Stop thinking about JSON. It's now a PHP array. The link I gave you shows how to search through an array of PHP objects and find an item based on one of the properties of the objects, which is exactly what you're asking for. Try it. If you fail somehow, please add the code you tried. was there something there which you still don't understand? – ADyson Mar 23 '20 at 16:48
  • @ADyson got it bro thanks – ganesh rao Mar 23 '20 at 16:50
  • http://sandbox.onlinephpfunctions.com/code/fc91d57ac0b9c40ac391afc7aa0a878e174a8aac – ADyson Mar 23 '20 at 16:53
  • @ADyson you are awesome pal thank you sooooo much for your help :D – ganesh rao Mar 23 '20 at 17:05

0 Answers0