Array data manipulation using php - 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));

Related

Need to merge arrays but leave only one value if duplicated

I have two arrays with the same keys but different values. I need to merge it but if the values are the same leave only one of this
$array1 = array('firstname'=> $may_name, 'lastname'=>$my_last_name, 'address'=>$addres_1);
$array2 = array('firstname'=> $may_name, 'lastname'=>$my_last_name, 'address'=>$addres_2);
I need to get:
$array_result = array('firstname'=> $may_name, 'lastname'=>$my_last_name, 'address'=>$addres_1, 'address'=>$addres_2);
can anybody help to solve this?
array_merge does not work for me..
First you need to merge 2 arrays, using array_merge() function. then get the unique elements from the array using array_unique() function will get you the result
var_dump(array_unique(array_merge($array1, $array2)));
Edit
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
php doc
Thanks #Marco

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

Php match values between 2 arrays

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().

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);

add values from arrays to an array?

how do i add values from arrays to an array so that it grows by time.
eg.
all values form array1 to myArray.
all values form array2 to myArray.
so now myArray contains all values from 1 and 2.
i want to do this in a cpu efficient way
$myArray = array_merge($array1, $array2);
See the documentation, as there are a few things you will want to know about how duplicates and numerical keys are handled.
Either use the array_merge() function (also see array_merge_recursive()):
$myArray = array_merge($array1, $array2);
If the input arrays have the same
string keys, then the later value for
that key will overwrite the previous
one. If, however, the arrays contain
numeric keys, the later value will not
overwrite the original value, but will
be appended.
Or use the Union Array Operator (+):
$myArray = $array1 + $array2;
The + operator appends elements of
remaining keys from the right handed
array to the left handed, whereas
duplicated keys are NOT overwritten.

Categories