How can i search inside JSON array object PHP with search box

Related searches

I made a search box, and i want to search inside JSON data which i take it from this url: https://jsonplaceholder.typicode.com/todos/

This is the code how i recive tha data :

<?php

  $url = 'https://jsonplaceholder.typicode.com/todos/';

  $cURL = curl_init();

  curl_setopt($cURL, CURLOPT_URL, $url);
  curl_setopt($cURL, CURLOPT_HTTPGET, true);
  curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);

  curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'Accept: application/json'
  ));

  $result = curl_exec($cURL);

  curl_close($cURL);

  $arrays =  json_decode($result);

 ?>

And this is the HTML code for search box and button:

<form action="search.php" method="POST" style="search-style";>
      <input type="text" name="search" placeholder="Search" >
      <button type="submit" name="submit-search">Search</button>
</form>

And this is the PHP code for SEARCH but i dont know how to finis it :

<div>
  <?php
    if (isset($_POST['submit-search'])) {
      $search = $????? ($_POST['search']);  // i dont know how it works, i need a variable 

      foreach ($arrays as $key => $value) {
        echo $value -> $search ;
      }
    }
       ?>
</div>

At the end i need to show the result in table or something like that. But i can't get the result.. Any suggestion? Thanks in advance!

You have an array of objects so just search for part or all of the string in I assume title:

if(isset($_POST['submit-search']) && !empty($_POST['search'])) {
  foreach($arrays as $value) {
      //find partial match
      if(strpos($value->title, $_POST['search']) !== false) {
          echo $value->title . '<br />';
      }
  }
}

For a full match:

if($value->title == $_POST['search']) {

search within json with php, There is a function json_decode() which will take your json and turn it into Once you have this array, you can loop through it to find the value <?php $json = '{"foo-bar": 12345}'; $obj = json_decode($json); print $obj->{'foo-bar'}; // 12345 ?> Then you could make a loop on the object ($obj) with test case� The point is that if you have a JSON Array (or an Array of like Objects) and have to search for a key value on an Object inside that Array (more than once), it is far more efficient to create a map of the key or keys you wish to search on, than to search the Array each time. Using this technique will improve the performance of your application

you send your request to search.php actually you redirect to search.php. for use this code in one page you must put search.php code in main file. you recive data when open site. and in search form you send a key(string-title-id...anything) and in last part you filter it your final code must be like this

<?php
$url = 'https://jsonplaceholder.typicode.com/todos/';
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json'
));
$result = curl_exec($cURL);
curl_close($cURL);
$arrays =  json_decode($result);
?>
<form action="#" method="POST" style="search-style";>
    <input type="text" name="Title" placeholder="Enter Title..." >
    <button type="submit" name="submit_search">Search</button>
</form>

<div>
    <?php
    if(isset($_POST['submit_search']) && !empty($_POST['Title'])) {
        $Title = $_POST['Title'];
        foreach($arrays as $value) {
            if(strpos($value->title, $Title) !== false) {
                echo "Title : ";
                echo $value->title ."<br/>";
                echo "Id : " ;
                echo $value->id ."<br/>";
            }
        }
    }
    ?>
</div>


Live search on JSON Objects Data Using jQuery, This tutorials help to live search on json data objects using jquery and JavaScript. We will search string key in array of json object and display in HTML form which will have search text box input field and id is '#txt-search' . Decoding JSON data using PHP. You can parse or read JSON data from JSON object or any JSON file using PHP json_decode() method. Different examples of parsing JSON data using PHP are given below. Example-1: In the following example, JSON data is assigned in a variable and PHP json_decode() method is used to read the data in PHP format.

I do it like this and it worked. Thanks!

    <?php include 'header.php';

  if(isset($_POST['submit-search']) && !empty($_POST['search'])) {
//  $search = $_POST['search']; ?>
<table >
      <p style=" text-align:center; font-size:100%";>Resulti from search :</p>
      <tr>
        <th>User ID:</th>
          <th>ID:</th>
          <th>Title:</th>
          <th>Compiled:</th>
      </tr>
<?php  foreach ($arrays as $value) {
      if(strpos($value->title, $_POST['search']) !== false) {?>
        <tr>
          <td><?php echo $value ->userId ?> </td>
          <td><?php echo $value ->id ?> </td>
          <td> <?php echo $value->title ?> </td>
          <td><?php echo $value ->completed ?> </td>
        </tr>
      <?php //  echo $value->title . '</br>'; ?>

<?php   }
      }
    }
   ?>
</table>

array_search - Manual, array_search — Searches the array for a given value and returns the first a strict type comparison of the needle in the haystack , and objects must be the same� Creating php json object array inside of array. 320. April 30, 2017, at 04:21 AM.

json_decode - Manual, PHP implements a superset of JSON as specified in the original � RFC 7159. assoc. When TRUE , returned objects will be converted into associative arrays. I'm having trouble trying to figure out how would I create a Php json object that has an array inside of array. I have been working on this for hours and can't figure it out. Should I use oject inside my while loop and add array? I Would like to have my answer array inside my question array like this.

PHP JSON complete tutorial (with examples), The “Tags” variable in the previous example is a JSON array. Arrays With this option, PHP arrays are always encoded into JSON objects regardless of their type. You can find all you need to get started in my PHPMailer complete tutorial . The com.fasterxml.jackson.databind.node.ObjectNode class can be used to map the JSON object structure in Json content. We can search for a particular value inside the JSON file using the get() method of ObjectNode class, this method used for accessing the value of a specified field of an object node.

If you’re working with JSON (JavaScript Object Notation) and either need to convert a JSON string to array or object and loop through it or vice-versa, take an array or object and convert it to a JSON string to return, both can be done in PHP or JavaScript.

Comments
  • Are you getting right data according your search ??
  • i'm getting 1 array inside with 200 objects. And i want to search inside that objects. But i don't know how.
  • What have you tried so far? If there are 200 objects in the JSON array, why not loop over them and compare whatever you want to compare?