Search
 
SCRIPT & CODE EXAMPLE
 

PHP

get index of element in array 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;
Comment

php get item position in array

//To expand on previous comments, here are some examples of
//where using array_search within an IF statement can go
//wrong when you want to use the array key thats returned.
//Take the following two arrays you wish to search:

<?php
$fruit_array = array("apple", "pear", "orange");
$fruit_array = array("a" => "apple", "b" => "pear", "c" => "orange");

if ($i = array_search("apple", $fruit_array))
//PROBLEM: the first array returns a key of 0 and IF treats it as FALSE

if (is_numeric($i = array_search("apple", $fruit_array)))
//PROBLEM: works on numeric keys of the first array but fails on the second

if ($i = is_numeric(array_search("apple", $fruit_array)))
//PROBLEM: using the above in the wrong order causes $i to always equal 1

if ($i = array_search("apple", $fruit_array) !== FALSE)
//PROBLEM: explicit with no extra brackets causes $i to always equal 1

if (($i = array_search("apple", $fruit_array)) !== FALSE)
//YES: works on both arrays returning their keys
?>
Comment

PREVIOUS NEXT
Code Example
Php :: how to get previous date in laravel 
Php :: php knoww if array has duplicate values 
Php :: php currency formator 
Php :: how to disable opcache 
Php :: Termlaravel validation exists array rules 
Php :: pdf to image php 
Php :: json to html php table 
Php :: recursive binary search php 
Php :: cors error angular php 
Php :: php read from mariadb 
Php :: Access-Control-Allow-Origin php laravel 
Php :: How To Unset Or Delete An Element From Array By Value In PHP? 
Php :: Laravel eloquent permanent soft delete 
Php :: define constructor in trait php 
Php :: php insert array into mysql 
Php :: laravel db drop table 
Php :: update php version wamp windows 
Php :: wc create new category 
Php :: checkbox options wordpress 
Php :: laravel tinker hash password 
Php :: laravel 8 websockets 
Php :: validation laravel 
Php :: Verzeichnis einlesen php 
Php :: softDelete laravel8 
Php :: create a laravel project 
Php :: yajra laravel datatables rawcolumn 
Php :: connect rabbitMQ 
Php :: laravel check if api request 
Php :: laravel module package 
Php :: laravel imap - Get message attachments 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =