Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the same value from two arrays in PHP?

Tags:

php

I want to get the same value from two arrays. Example:

a[] = array(a,b,c,d,e,f,g,h);
b[] = array(c,d,o,l,p,i,u,y);

I want c[]=c,d;

like image 916
zahir hussain Avatar asked Feb 16 '10 10:02

zahir hussain


People also ask

How can I check if two arrays are equal in PHP?

The array_diff() function compares the values of two (or more) arrays, and returns the differences. This function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in array2 or array3, etc.

What is Array_keys () used for in PHP?

The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.

How do you check if an array has the same value?

In order to check whether every value of your records/array is equal to each other or not, you can use this function. allEqual() function returns true if the all records of a collection are equal and false otherwise. let's look at the syntax… const allEqual = arr => arr.

What is difference between array and [] in PHP?

Note: Only the difference in using [] or array() is with the version of PHP you are using. In PHP 5.4 you can also use the short array syntax, which replaces array() with [].


2 Answers

<?php

$arr = array_intersect(array('a', 'b', 'c', 'd'),
                       array('c', 'd', 'e', 'f'));

print_r(array_values($arr));
like image 73
Richard Knop Avatar answered Sep 28 '22 08:09

Richard Knop


Use array_intersect($a,$b) -- Ohh many guys answered before i typed

like image 45
RubyDubee Avatar answered Sep 28 '22 06:09

RubyDubee