Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working on arrays in PHP

Tags:

php

Assuming that you have three arrays which might contain different values as follows.:

$arr1 = array('1', '5', '10');
$arr2 = array('1', '3', '10');
$arr3 = array('1', '6', '10');

How would you strip out what's different and get it as follows?:

$arr1 = array('1', '10');
$arr2 = array('1', '10');
$arr3 = array('1', '10');

I meant I wanted to get it as follows.:

$result = array('1', '10');
like image 564
Fakedupe Avatar asked Aug 29 '10 15:08

Fakedupe


People also ask

Does += work on arrays in PHP?

The + operator in PHP when applied to arrays does the job of array UNION. $arr += array $arr1; effectively finds the union of $arr and $arr1 and assigns the result to $arr .

What is array in PHP with example?

An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

What does => mean in PHP array?

=> is the separator for associative arrays. In the context of that foreach loop, it assigns the key of the array to $user and the value to $pass .

How do I start an array in PHP?

To create an array, you use the array() construct: $myArray = array( values ); To create an indexed array, just list the array values inside the parentheses, separated by commas.


1 Answers

Use array_intersect function:

<?php
$arr1 = array('1', '5', '10');
$arr2 = array('1', '3', '10');
$arr3 = array('1', '6', '10');

$result = array_intersect($arr1, $arr2, $arr3);

print_r($result);

//now assign it back:
$arr1 = $result;
$arr2 = $result;
$arr3 = $result;
?>
like image 103
shamittomar Avatar answered Oct 12 '22 22:10

shamittomar