Get same value in array php
I want to get the same value from two arrays. Example: Show
I want
asked Feb 16, 2010 at 10:15
zahir hussain zahir hussain 3,63110 gold badges28 silver badges36 bronze badges 1
answered Feb 16, 2010 at 10:22
Richard KnopRichard Knop 78.1k145 gold badges387 silver badges546 bronze badges see http://docs.php.net/array_intersect: array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.
prints
answered Feb 16, 2010 at 10:19
1 Try answered Feb 16, 2010 at 10:20
chelmertzchelmertz 20k4 gold badges41 silver badges46 bronze badges Use
S.M_Emamian 16.4k31 gold badges120 silver badges232 bronze badges answered Feb 16, 2010 at 10:21
RubyDubeeRubyDubee 2,4261 gold badge23 silver badges33 bronze badges in_array() might be what you're looking for Use in_array() to see if the value you're looking for is in both arrays a and b and then put it in array c (following your example) http://www.php.net/manual/en/function.in-array.php answered Feb 16, 2010 at 10:19
Ganesh ShankarGanesh Shankar 4,8168 gold badges42 silver badges56 bronze badges You can use array_intersect() function.
emmanuel 9,53710 gold badges24 silver badges37 bronze badges answered Sep 4, 2014 at 8:53
1 (PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8) array_intersect — Computes the intersection of arrays Descriptionarray_intersect(array Parametersarray The array with master values to check. arrays Arrays to compare values against. Return Values Returns an array containing all of the values in Changelog
ExamplesExample #1 array_intersect() example
The above example will output: Array ( [a] => green [0] => red ) Notes
See Also
stuart at horuskol dot co dot uk ¶ 14 years ago
= array(2, 4, 6, 8, 10, 12); Niels ¶ 16 years ago
Shawn Pyle ¶ 13 years ago
sapenov at gmail dot com ¶ 17 years ago
blu at dotgeek dot org ¶ 17 years ago
[0] = array('John Doe', 'PHP Book'); yuval at visualdomains dot com ¶ 7 years ago
= range(1,1000000); dml at nm dot ru ¶ 11 years ago
matang dot dave at gmail dot com ¶ 7 years ago
info at iridsystem dot it ¶ 7 years ago
Esfandiar -- e.bandari at gmail dot com ¶ 14 years ago
theking at king dot ma ¶ 4 months ago
Anonymous ¶ 1 year ago
Ehsan.Chavoshi.com ¶ 3 years ago
Yohann ¶ 12 years ago
Oto Brglez ¶ 13 years ago
= array(); Malte ¶ 14 years ago
nthitz at gmail dot com ¶ 16 years ago
t dot wiltzius at insightbb dot com ¶ 18 years ago
anbolb at boltblue dot com ¶ 18 years ago
caffinated ¶ 9 years ago
terry(-at-)shuttleworths(-dot-)net ¶ 16 years ago
tom p ¶ 16 years ago
ben at kazez dot com ¶ 18 years ago
Mike Block ¶ 9 years ago
david at audiogalaxy dot com ¶ 21 years ago
gary ¶ 13 years ago
$numbers;
meihao at 126 dot com ¶ 8 years ago
zoolyka at gmail dot com ¶ 3 years ago
= range(1, 250000); ram ¶ 4 years ago
karl at libsyn dot com ¶ 13 years ago
findMatchingArray( $needle, $haystack ) {
189780 at gmail dot com ¶ 12 years ago
jake ¶ 5 years ago
How can I get two data from the same array in PHP?The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.
How can I get unique values from two arrays in PHP?The array_diff() (manual) function can be used to find the difference between two arrays: $array1 = array(10, 20, 40, 80); $array2 = array(10, 20, 100, 200); $diff = array_diff($array1, $array2); // $diff = array(40, 80, 100, 200);
What is use of Array_unique in PHP?The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed. Note: The returned array will keep the first array item's key type.
How do you find the duplicate number on a given integer array in PHP?This is the only answer that returns only the duplicate values. – Redzarf. ... . Extended to only give list of duplicate values (what I needed): return array_values(array_unique(array_diff_key($a, array_unique($a)))); – Autumn Leonard. ... . Stop scrolling. Although it doesn't seem, this is the answer you are looking for.. |