Multiple explode and foreach, while searching a `input` value in txt file html/php/ajax

Active3 hr before
Viewed126 times

7 Answers


Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Thanks for contributing an answer to Stack Overflow!, Why are we to leave a front-loader clothes washer open, but not the dishwasher? ,Now firstly let me describe the whole scenario with appropriate code.

Sample code:

< ? php

$data = array();
$return_value = array();

   $needle = strtolower(urlencode($_GET['sv']));
   $file_contents = file('ul.txt');
   $range = strlen($needle);

   foreach($file_contents as $key => $haystack) {
      $smaller_haystack = explode('|', $haystack);
      // search for name or email
      $remove_dot_com = str_replace('.com', '', $smaller_haystack[1]);
      // if(stripos($smaller_haystack[0], $needle) !== false || stripos($remove_dot_com, $needle) !== false) {
      if ((substr(strtolower($smaller_haystack[0]), 0, $range)) == $needle || (substr($remove_dot_com, 0, $range)) == $needle) {
         $return_value[$key]['name'] = $smaller_haystack[0];
         $return_value[$key]['email'] = $smaller_haystack[1];
         $return_value[$key]['status'] = $smaller_haystack[2];

   echo json_encode($return_value);

load more v

Pass 1: Copies $array[0] (i.e., “1”) into $value (which is a reference to $array[2]), so $array[2] now equals 1. So $array now contains [1, 2, 1].,As a result, when we go through the second foreach loop, “weird stuff” appears to happen. Specifically, since $value is now being accessed by value (i.e., by copy), foreach copies each sequential $array element into $value in each step of the loop. As a result, here’s what happens during each step of the second foreach loop:,Pass 2: Copies $array[1] (i.e., “2”) into $value (which is a reference to $array[2]), so $array[2] now equals 2. So $array now contains [1, 2, 2].,OK, now let’s see what happens when we attempt to access the test property of each of these classes:

Not sure how to use foreach loops in PHP? Using references in foreach loops can be useful if you want to operate on each element in the array that you are iterating over. For example:

$arr = array(1, 2, 3, 4);
foreach($arr as & $value) {
   $value = $value * 2;
// $arr is now array(2, 4, 6, 8)
load more v

stripslashes() can be used if you aren't inserting this data into a place (such as a database) that requires escaping. For example, if you're simply outputting data straight from an HTML form. ,Example #2 Using stripslashes() on an array,Example #1 A stripslashes() example,The above example will output:

      [0] => f 'oo [1] => b 'ar [2] => Array(
         [0] => fo 'o [1] => b 'ar


Create two <input type='text'> elements.,First is used for search value and another to display selected item id from the suggestion list.,Initialize jQuery UI autocomplete on <input type='text' id='searchpost'>.,The select option trigger when an option selected from the suggestion list. Assign label in $('#searchpost') and value in $('#selectedpost_id').

I am using posts table in the example and added some records.

   varchar(200) NOT NULL,
   text NOT NULL,
   varchar(255) NOT NULL
load more v

Search Answer Titles

$var = 'a/asdas/fgdfg/zfdvs/sdfh';
$array = explode('/', $var);
foreach($array as $values) {

We have already seen how to convert JSON to JavaScript Object while creating the example on JSON handling with PHP via AJAX without jQuery.,As the input JSON file contains multi-level hierarchical structural data, we have to recursively iterate the JSON object using PHP RecursiveArrayIterator.,In this section, we are going to see about JSON handling with PHP and AJAX. I have used JavaScript to send the AJAX call to the PHP via an XML HTTP request.,In this article, under the sub-heading “Read JSON via AJAX and Process”, there is a code snippet and it uses plain JavaScript solution without using jQuery. Is this what you are looking for?

JSON object format consists of a collection of key-value pairs of data. Below structure shows the example of a valid JSON object structure.

   "FIFA_World_Cup_Winners": {
      "2018": "France",
      "2014": "Germany",
      "2010": "Spain",
      "2006": "Italy",
      "2002": "Brazil"
load more v

This site uses functional cookies and external scripts to improve your experience. Which cookies and scripts are used and how they impact your visit is specified on the left. You may change your settings at any time. Your choices will not impact your visit.,This site uses functional cookies and external scripts to improve your experience.,See my other PHP code tutorials here : All posts tagged with “PHP”,NOTE: These settings will only apply to the browser and device you are currently using.

Non – Ajax version :

load more v

Other "foreach-multiple" queries related to "Multiple explode and foreach, while searching a `input` value in txt file html/php/ajax"