Php match values between 2 arrays - php

Guys i have a very awkward situation here , i am not sure whether am i taking a right approach or not.. i am trying to match the values between 2 arrays and then running if else statement... here's goes what i am trying to do
$array1 = array('html','php','js','css');
$array2 = array('php','python','html','java');
what i want is to make a check where the values of these 2 arrays matches to each other.
like php and html is common in both and also where it doesn't match.
Thanks

You mean like an intersection?

It's your need:
$result = array_intersect($array1, $array2);
print_r($result);
the result is:
Array
(
[0] => html
[1] => php
)

array_intersect
and
array_diff
should do what you want.

To get both the intersecting elements of the array and the differing elements use both array_diff() and array_intersect().

Related

Array filtering in PHP

So I have two arrays:
$one = array('red','green','blue','yellow','white');
$two = array('white','blue','red');
This being said, I need to now remove the elements from the first array that are existent in the second one. In short, the output after the sorting has to be (in this case): green, yellow.
I've looked at the array functions at PHP's documentation but was unable to find what I need. I'm sure it's something basic but I can't recall a function for that.
Try array_diff()
Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.
For example...
$three = array_diff($one, $two);
Demo ~ https://eval.in/167872

Array data manipulation using php

I have 2 arrays
First array -- array1(3,17,19,11,34,56,22,29);
Second array -- array2(4,6,12,19,59,21);
Now I would like to get 3 types of data
a) values which are present in both array for eg `19`
b) values which are not present in array1 but present in array 2 for eg `4,6,12,59,21`
c) values which are not present in array2 but present in array 1 for eg `3,17,11,34,56,22,29`
Can it be done using single for loop?
Please suggest.
The PHP docs are your friend
PHP has a bunch of built in functions for working with arrays
The full list is here: http://www.php.net/manual/en/ref.array.php
The ones you're after are array_intersect and array_diff
Look mum, no loop!
a) array_intersect($array1, $array2)
b) array_diff($array1, $array2)
b) array_diff($array2, $array1)
Try array_merge:
array_unique(array_merge($array1, $array2));

Can't flatten multidimensional array with lots of duplicates

I'm trying to create a script that, based on an input a?? creates an array of all the combinations and permutations of all words containing an a and two other characters from the alphabet.
Values are such as a, ab, ba, dab, bga etc - as you may see the array contains (or should contain) a weird amount of values.
The problem is that the functions I use in the script outputs even more values with many duplicates.
And for some reason I can not create a flattened array without duplicates. I tried to use array_unique() but it doesn't work here. I tried to use explode() and implode() to flatten the result array, but no success. Even if I succeed to create a string from the values, when I try to transform this string into an array, the result is again the actual multi-dimensional array.
This drives me crazy, and as you see the code, I'm a beginner in PHP.
Any help to transform the actual multidimensional array to a flattened one without duplicates is highly appreciated. An example: actually the array contains 12168 sub-arrays, and only the string a occurs 1456 times. What I need is an array that doesn't have sub-arrays and contains each results only one time.
The PHP code is available at here
and the output is here:
Have you tried something like:
$inputString = 'a??';
$array = array();
if (strpos($inputString, 'a') !== false && !in_array($inputString, $array)) {
$array[] = $inputString;
}
echo '<pre>'; print_r($array); echo '</pre>';

Convert list (array) to associative array in PHP

I want to convert PHP list (array), i.e.
array("start", "end", "coords")
into associative array with truthy values (just to be able to test the presence/absence of key quickly), i.e. to something like this:
array(
"start" => 1,
"end" => 1,
"coords" => 1
)
Is there any more elegant way to do it than this?
array_fill_keys($ar, 1)
There is probably no more elegant solution than array_fill_keys($ar, 1).
There is a function called array_flip that does this.
http://php.net/array_flip
Doing array_flip on an array and then using isset turned out to be much faster than doing in_array for me.
But note that this is only useful when you're going to be searching the array multiple times.

Comparing two arrays and showing the difference according to each one

So I know there's an easy way to see the difference between two arrays using array_diff.
Take a look at why I need something a bit more specific though:
Let's say we have these 2 arrays
$array1 $array2
1 1
2 -
- 3
4 4
- 5
6 -
The - indicates it is missing from the opposing array.
If $array1 contains a missing element from $array2, it must be dropped from $array1.
If $array2 contains a missing element from $array1, it must be added to $array1.
If I simply perform array_diff($array1, $array2), it will only return me [2, 6]. This isn't helpful in my scenario because I don't know which of the two arrays these items are missing from.
I did a bit of looking around and didn't seem to find out if there is a native php function that will distinguish the arrays the items are missing from.
What would be the best way to go about this? I was thinking of looping $array1 and checking it against $array2 and storing the results missing in a third array, and visa-versa for a fourth array.
Is that the best way? or is there an even easier, native function i can use?
$comparison1 = array_diff($array1, $array2);
$comparison2 = array_diff($array2, $array1);

Categories