Navigate back to the homepage

How to Check if value exists in array JavaScript, php

Infinitbility
JavaScript, PHP
October 11th, 2020 · 1 min read
How to Check if value exists in array JavaScript, php

Hello Friends,

Welcome To Infinitbility!

How to Check if value exists in array using indexOf for javascript and in_array for php.

we are going to discuss today’s topic “How to find and specific item on an array without loops in javascript and PHP.”

Let’s begins with Javascript

JavaScript

Javascript provides indexOf function to find value from an array Understand with an example

1//indexOf example
2var fruits = ['Banana', 'Blackcurrant', 'Blueberry', 'Chili pepper', 'Cranberry', 'Eggplant', 'Gooseberry'];
3
4// You get their index ( Key )
5var index = fruits.indexOf('Blueberry');
6// output 2
7
8// if value not available
9var index = fruits.indexOf('Apple');
10// output -1

PHP

Similarly, Php provides in_array function to find value from an array Understand with an example

1<?php
2
3# inArray example
4$fruits = ['Banana', 'Blackcurrant', 'Blueberry', 'Chili pepper', 'Cranberry', 'Eggplant', 'Gooseberry'];
5
6# You get true or false
7$result = in_array("Blueberry", $fruits);
8# output true
9
10# if value not available
11$result = in_array("Apple", $fruits);
12# output false
13
14?>

Thanks for reading…

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to exit from initramfs busybox in Ubuntu

How to exit from initramfs busybox in Ubuntu

here you get solution of busybox initramfs issue in ubuntu

October 11th, 2020 · 1 min read
Laravel call function from another class

Laravel call function from another class

here you will learn to call a function from another controller.

October 11th, 2020 · 1 min read
© 2020–2021 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]