I would like to compare two array items with php, I think I should use array_intersect_key but I don't know how I can do that.
Array 1
[1] => obj Object
(
[idobj:protected] => 2
)
[2] => obj Object
(
[idobj:protected] => 1
)
Array 2
[1] => obj Object
(
[idobj:protected] => 1
)
No, you don't need to use array_intersect_key() if you need only to compare array elements.
It simple like this (for two-dimensional arrays):
if( $array1[0] == $array2[0] ) {
echo 'Array items are equal';
} else {
echo 'Array items are not equal';
}
If you have multi-dimensional array you may need add some extra indexes.
PHP manual has a very good information regarding arrays, check it out.
Are you actually looking for array_intersect()?
$objectsInArray1ThatArePresentInArray2 = array_intersect($array1, $array2);
Related
I have this type of array,
Array
(
[0] => Array
(
[id] => 0
[fams] => 5
)
[1] => Array --> I want to remove this value using its index, which is "1"
(
[id] => 2
[fams] => 5
)
)
I want to remove that array [1] entirely, using its index, so the condition is - where the ID is match, for example - [id] => 2
Is that possible, to remove a particular value with that specific condition?
and without looping (or any similar method that need to loop the array)
thanks in advance!
FYI - I did try to search around, but, to be honest, I'm not sure what "keyword" do I need to use.
I did try before, but I found, array_search, array_keys - and it seems those 2 are not.
I'm okay, if we need several steps, as long as it did not use "loop" method.
---update
I forgot to mention, that I'm using old PHP 5.3.
array_filter should work fine with PHP 5.3.
The downside of this approach is that array_filter will (internally) iterate over all your array's entries, even after finding the right one (it's not a "short-circuit" approach). But at least, it's quick to write and shouldn't make much of a difference unless you're dealing with very big arrays.
Note: you should definitely upgrade your PHP version anyway!
$array = array (
0 =>
array (
'id' => 0,
'fams' => 5
),
1 =>
array (
'id' => 2,
'fams' => 5
)
);
$indexToRemove = 2;
$resultArray = array_filter($array, function ($entry) use ($indexToRemove) {
return $entry['id'] !== $indexToRemove;
});
Demo: https://3v4l.org/6DXjl
You can use array_search to find the key of a sub-array that has a matching id value (extracted using array_column), and if found, unset that element:
if (($k = array_search(2, array_column($array, 'id'))) !== false) {
unset($array[$k]);
}
print_r($array);
Output:
Array
(
[0] => Array
(
[id] => 0
[fams] => 5
)
)
Demo on 3v4l.org
It should be noted that although there is no explicit loop in this code, array_search and array_column both loop through the array internally.
You can use array_column to make id as index of the sub-array then use unset
$a = array_column($a, null, 'id');//new array id as index
$index = 2;// id to remove
if($a[$index]) unset($a[$index]);
print_r($a);
Working example :- https://3v4l.org/ofMr7
I have two arrays and I want to compare the first with the second array. I have an IF statement and it currently does two checks:
It checks to see if there are any rows from my query
If the above passes, then I want to traverse the array values of the first array and compare it with the second array. I want to traverse through all the names first array (index 0-9) and compare it with the first name of the second array (index 0). Keep doing the same thing until we've compared all the indices 0-9 on the first array with all indices 0-9 of the second array.
Finally, if there is a match I want to exit my PHP script, else if there is no match I continue to do to stuff.
I have tried using the in_array() function and that works, but only with a single variable. Something like this in_array($firstArray[0], $secondArray) works. Something like this:
if(mysqli_num_rows($result) > 0 && in_array($firstArray[0], $secondArray)){
exit("exiting now!");
}
else {
echo "continue to do stuff...";
}
However, when I put the whole array in the inArray, it won't work. Like this:
in_array($firstArray, $secondArray)
How can I achieve this?
You can use array_intersect if you want to know if they intersect, you can use array_diff to see if the two arrays have any differences.
if(mysqli_num_rows($result) > 0 && !array_diff($firstArray, $secondArray)){
exit("exiting now!");
} else {
echo "continue to do stuff...";
}
Example:
<?php
$array1 = array (1,2,3,4);
$array2 = array (3,2,1,5);
$array3 = array_diff($array1, $array2);
$array4 = array_intersect($array1, $array2);
echo "<pre>";
echo "Array 1\n ";
print_r($array1);
echo "Array 2\n ";
print_r($array2);
echo "Arrays Difference \n ";
print_r($array3);
echo "Arrays intersect \n ";
print_r($array4);
echo "</pre>";
?>
Output:
Array 1
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Array 2
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 5
)
Arrays Difference
Array
(
[3] => 4
)
Arrays intersect
Array
(
[0] => 1
[1] => 2
[2] => 3
)
I think you are looking for array_intersect() which returns the values that are common in two (or more) arrays. http://php.net/manual/en/function.array-intersect.php
It returns an array but if it returns an empty array then empty arrays are falsy.
I am trying to compare two different arrays and get the values that do not exist in 1 of the arrays. Here are my 2 arrays:
Array ( [0] => 2fbd5868-28ec-418d-854a-0736db720c8a [1] => f4a41974-5373-4862-a5e7-9d28b8c2301f [2] => a1874f68-3da1-47c3-97ef-a68580ce2a52)
Array ( [0] => 2fbd5868-28ec-418d-854a-0736db720c8a [1] => f4a41974-5373-4862-a5e7-9d28b8c2301f [2] => a1874f68-3da1-47c3-97ef-a68580ce2a52 [3] => 583cee91-1913-4e9d-b51d-e27083420001)
As you can see the second array has an additional value. I am trying to user array_diff like this:
$result = array_diff($array1,$array2);
print_r($result);
However the out of the array_diff is:
array()
Any ideas what is going on?
As people have suggested and i have already tested switching the arrays around, this is the output:
Array ( [0] => [1] => )
array_diff gives you the values from $array1 that are not in the other arrays. All the values of your first array are in the second. Sou change the order of your arrays and you should be fine.
See also here: http://php.net/manual/de/function.array-diff.php
The order of arguments in array_diff() is important
Returns an array containing all the entries from array1 that are not
present in any of the other arrays2
Read array_diff
$result = array_diff($array2,$array1);
Try like this
i have this multidimensional array.
Array
(
[0] => Array
(
[car] => Toyota
)
[1] => Array
(
[car] => Ford
)
[2] => Array
(
[car] => Isuzu
)
[3] => Array
(
[car] => Chevrolet
)
)
i want to put them into indexed array like this..
Array = ("Toyota", "Ford", "Isuzu", "Chevrolet");
$result = array_map(function($item)
{ return $item['car']; }, $array);
Some functional approach. array_map
P.S. PHP has no indexed arrays
foreach($yourArray as $carArray)
{
$result[]=$carArray["car"];
}
And your understanding of indexed arrays is wrong. That example output you've shown contains all those values, not indexes, and since you didn't specify an index it will start from 0 and so on.
Option using array_map(), assuming your initial array is $arr
$new_arr = array_map(function($x){return $x['car'];}, $arr);
See demo
try using foreach and array_values, then save it into empty array. Hope it helps.
I'm a bit fuzzy on how to work with multidimensional arrays in Ruby. How would I recreate this PHP code in Ruby?
$objs_array = array();
foreach($objs AS $obj) {
$objs_array[$obj->group_id][] = $obj;
}
}
print_r($objs_array);
The result would be:
Array
(
[123] => Array
(
[0] => Array
(
object1
)
[1] => Array
(
object2
)
)
[456] => Array
(
[0] => Array
(
object3
)
[1] => Array
(
object4
)
)
)
Thanks.
More than a multidimensional array, hash of arrays would fit better.
In php you only have the type array, but in ruby the class Hash is very useful
objs_hash = {}
objs.each do |obj|
objs_hash[obj.id] = obj
end
Thank you rhernado for sending me down the road of looking into hashes. I ended up finding a similar question that pointed out the group_by method:
Building Hash by grouping array of objects based on a property of the items - I ended up using a hash of an array of objects by using:
groups = objs.group_by { |obj| obj.group_id }