Answers for "array search"

10

array_search in php

<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?>
Posted by: Guest on March-07-2020
1

search in javascript

var str = "Please locate where 'locate' occurs!";

var ind1 = str.indexOf("locate"); // return location of first value which founded
var ind2 = str.lastIndexOf("locate"); // return location of last value which founded
var ind3 = str.indexOf("locate", 15); // start search from location 15 and then take first value which founded
var ind4 = str.search("locate");
//The search() method cannot take a second start position argument. 
//The indexOf() method cannot take powerful search values (regular expressions).

document.write("<br>" + "Length of string:", len);
document.write("<br>" + "indexOf:", ind1);
document.write("<br>" + "index of last:", ind2);
document.write("<br>" + "indexOf with start point:", ind3);
document.write("<br>" + "search:", ind4);
Posted by: Guest on June-04-2021
1

array_search

// array demo

public $gender = array(
  1 => 'Male',
  2 => 'Female',
  3 => 'Other',
);

$gen = 1;

if ($gen == array_search('Male', $this->gender)) {
  $gender = __d('member', 'male');

} elseif ($gen  == array_search('Female', $this->gender)) {
  $gender = __d('member', 'female');

}
Posted by: Guest on October-02-2020
3

function search in javascript array

// function to search array using for loop
function findInArray(ar, val) {
    for (var i = 0,len = ar.length; i < len; i++) {
        if ( ar[i] === val ) { // strict equality test
            return i;
        }
    }
    return -1;
}

// example array
var ar = ['Rudi', 'Morie', 'Halo', 'Miki', 'Mittens', 'Pumpkin'];
// test the function 
alert( findInArray(ar, 'Rudi') ); // 0 (found at first element)
alert( findInArray(ar, 'Coco') ); // -1 (not found)
Posted by: Guest on December-02-2020
1

search in javascript

str.indexOf("locate"); // return location of first find value
str.lastIndexOf("locate"); // return location of last find value
str.indexOf("locate", 15); // start search from location 15 and then take first find value
str.search("locate");
//The search() method cannot take a second start position argument. 
//The indexOf() method cannot take powerful search values (regular expressions).
Posted by: Guest on May-29-2021
0

array search

/*
  Arrays Methods [Search]
  - indexOf(Search Element, From Index [Opt])
  - lastIndexOf(Search Element, From Index [Opt])
  - includes(valueToFind, fromIndex [Opt]) [ES7]
*/

let myFriends = ["Ahmed", "Mohamed", "Sayed", "Alaa", "Ahmed"];

console.log(myFriends);

console.log(myFriends.indexOf("Ahmed"));
console.log(myFriends.indexOf("Ahmed", 2));

console.log(myFriends.lastIndexOf("Ahmed"));
console.log(myFriends.lastIndexOf("Ahmed", -2));

console.log(myFriends.includes("Ahmed"));
console.log(myFriends.includes("Ahmed", 2));

if (myFriends.lastIndexOf("Osama") === -1) {
  console.log("Not Found");
}

console.log(myFriends.indexOf("Osama"));
console.log(myFriends.lastIndexOf("Osama"));
Posted by: Guest on July-14-2021

Browse Popular Code Answers by Language