0

Here is a api response. What I am having problem is that there are several crew member here. I can just get anyone by their basic index. What do I do to get just the writers name or Should I say how can I access the array with Writer as parameter and get his/her name from this array.

  "crew": [
        {
          "credit_id": "53f52157c3a36833fa002225",
          "department": "Writing",
          "gender": 2,
          "id": 1930,
          "job": "Writer",
          "name": "Truman Capote",
          "profile_path": "/kmhddndt7hI38aN6NzbhPcnjKSh.jpg"
        },
        {
          "credit_id": "52fe4a6ec3a368484e152e99",
          "department": "Directing",
          "gender": 1,
          "id": 96199,
          "job": "Director",
          "name": "Liz Garbus",
          "profile_path": "/1Z5a6djeDX57yyLe2ixX3VzIaLJ.jpg"
        },
        {
          "credit_id": "53f5216dc3a36833fd0021bd",
          "department": "Writing",
          "gender": 1,
          "id": 96199,
          "job": "Writer",
          "name": "Liz Garbus",
          "profile_path": "/1Z5a6djeDX57yyLe2ixX3VzIaLJ.jpg"
        },
        {
          "credit_id": "53f52180c3a36834000023b2",
          "department": "Writing",
          "gender": 0,
          "id": 1355119,
          "job": "Writer",
          "name": "Ralph Greenson",
          "profile_path": null
        }
      ]

Thanks in Advance for any help.

3
  • where you want to filter this data from server side or from user side? Commented Jun 6, 2020 at 8:32
  • from the server Commented Jun 6, 2020 at 8:34
  • where you want to process this data? in server-side or in client-side? Commented Jun 6, 2020 at 9:06

4 Answers 4

1

With PHP you can use FilterIterator classes like in the following example ...

class CrewFilter extends FilterIterator {
    protected $department;

    public function __construct(Iterator $iterator, $department) {
        $this->department = $department;
        parent::__construct($iterator);
    }

    public function accept(): bool
    {
        $current = $this->getInnerIterator()->current();
        return $current->department === $department;
    }
}

$data = json_decode($response);
$iterator = new ArrayIterator($data['crew']);

$filter = new CrewFilter($iterator, 'Writing');
foreach ($filter as $crew) {
    // output the name
    var_dump($crew->name);
}

What does the code above. The FilterIterator instance takes a $department parameter, that sets the department for the crew memebers you want to see. The first parameter is an instance of an Iterator. In this case this will be the json decoded data from the api. It 's elementary to decode the received json data.

The foreach loop will just give back the crew members with the department Writing.

Sign up to request clarification or add additional context in comments.

Comments

1

if you want to search from browser side assume ur api response json is in obj here is the javascript code

var results = [];
var searchField = "job";
var searchVal = "Writer";
for (var i=0 ; i < obj.crew.length ; i++)
{
    if (obj.list[i][searchField] == searchVal) {
        results.push(obj.list[i]);
    }
}

then the result variable will have all the writers data hope this is the solution you are searching for

4 Comments

No it's going to be done from the back end. The result is going to be shown to the user automatically.
okey then pass the arguments to the api and filter by the mysql query
The webpage is completely dynamic html & php and don't have a database connected to it. Though in future I might connect mysql but for the time being is there a way that get the data using php.
then you need to convert the json data to php array/object using json_decode() then filter and after filtering json_encode the data and print
1

If you want to process JSON object in php then try following code

<?php
  // JSON string
  $someJSON = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]';

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

  // Convert JSON string to Object
  $someObject = json_decode($someJSON);
  print_r($someObject);      // Dump all data of the Object
  echo $someObject[0]->name; // Access Object data
?>

Or if you want to process the data in Javscript (client side)

obj = [{
          "credit_id": "53f52157c3a36833fa002225",
          "department": "Writing",
          "gender": 2,
          "id": 1930,
          "job": "Writer",
          "name": "Truman Capote",
          "profile_path": "/kmhddndt7hI38aN6NzbhPcnjKSh.jpg"
        },
                {
          "credit_id": "53f52157c3a36833fa002225",
          "department": "Writing",
          "gender": 2,
          "id": 1930,
          "job": "Writer",
          "name": "Truman Capote 2",
          "profile_path": "/kmhddndt7hI38aN6NzbhPcnjKSh.jpg"
        },
]

obj.forEach(function(v,i){console.log(v.name)})

2 Comments

I could do that with ease but the problem is I don't want everything in a serial. I want data of writers as you may notice there are other types of people(Direction/Production) who are using this array. I just want to get the peoples name who are on writing department and a writer.
So you want to filter out the data depend on upon department ??
0

So based on everyone's suggestion I tried tweaking somethings though it's not fast but much effective.(PHP Code)

 $data = json_decode($response,true);

   $n = sizeof($data['crew']);

    for ($i=0; $i < $n; $i++) { 

        if($data['crew'][$i]['job']=='Writer')
        {
            echo $data['crew'][$i]['name'];
        }
    }

Comments

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.