Array_Intersect not working as expected - php

I have two multidemensional arrays and I am trying to use array_intersect to find the values from $array1 that occur in $array2. Instead the results, as you can see below, include both values from the first array $array1 even though only one of the values occurs in the second array $array2. I suppose I have some misunderstanding of how this function works, can anyone clarify what I am doing wrong here?
var_dump($array1);
array(2) {
[0]=>
array(1) {
["id"]=>
string(2) "28"
}
[7]=>
array(1) {
["id"]=>
string(2) "30"
}
}
var_dump($array2);
array(1) {
[0]=>
array(1) {
["id"]=>
string(2) "30"
}
}
var_dump(array_intersect($array1, $array2));
array(2) {
[0]=>
array(1) {
["id"]=>
string(2) "28"
}
[7]=>
array(1) {
["id"]=>
string(2) "30"
}
}

The function array_intersect compares the values as strings, see manual. Unfortunately, this gives simply "Array" for all arrays.
Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation
is the same.
Instead you can use a custom compare function:
array_uintersect($array1, $array2, function($a, $b) { return ($a<$b)?-1:($a==$b)?0:1; })
Or use !== if you want to also compare order and types - see here for the difference.

array_intersect_assoc() looks at key and value for intersection.

Related

Extract arrays with string to a new array

I know that there are some posts at SO with a similar issue, but none of those could really help me solving mine. I do have the following $test array:
array(1) {
[0]=>
array(4) {
[0]=>
array(2) {
["id"]=>
string(8) "40265656"
["text"]=>
string(29) "10' - 1st Corner - Terengganu"
}
[1]=>
array(2) {
["id"]=>
string(8) "40265715"
["text"]=>
string(25) "18' - 2nd Corner - Pahang"
}
[2]=>
array(2) {
["id"]=>
string(8) "40265770"
["text"]=>
string(29) "23' - 3rd Corner - Terengganu"
}
[3]=>
array(3) {
["id"]=>
string(8) "40265830"
["text"]=>
string(29) "26' - 4th Corner - Pahang"
}
}
}
and would like to extract only those arrays containing Pahang in the ["text"] key. First I have tried
$key = array_search('Pahang', $test);
and that gives me bool(false). What am I doing wrong here?
I think this way will be right:
$result = [];
foreach ($array[0] as $arr) {
if (strpos($arr['text'], "Pahang") !== false) {
$result[] = $arr;
}
}
The function array_search(...) searches for a value inside an array, but your array is not flat, it's multidimensional, this means you have one or more arrays inside another array.
In your case, you can use array_filter(...) that allows you to filter your array elements in a callable function.
So, initially you have to define a function that filters the elements:
function getPahang($element) {
return $element['text'] === 'Pahang'
}
This function returns true when the element text value is equal to 'Pahang', instead it returns false.
Now, you have to call array_filter, by passing your array and the callable function:
$new_array = array_filter($test[0], 'getPahang');
You can try a similar script here: http://sandbox.onlinephpfunctions.com/code/c076222dd6d1c6f2675d0241742e6c11da6eff53

How can I compare two arrays if inside the arrays some strings are similar?

I simply want to compare two strings
$result = array_diff($original, $new);
var_dump $original:
array(4) {
[0]=>
string(4) "8344"
[1]=>
string(4) "7076"
[2]=>
string(7) "6220940"
[3]=>
string(7) "6220940"
}
var_dump $new:
array(4) {
[0]=>
string(4) "8344"
[1]=>
string(4) "7076"
[2]=>
string(14) "6220940mistake"
[3]=>
string(7) "6220940"
}
var_dump $result:
array(0) {
}
But what I actually expect would be var_dump $result:
array(1) {
[2]=>
string(7) "6220940"
}
I found out that this is happening because I have two strings that are similar. So if every string is unique, there is no problem. But I do sometimes have also similar strings inside my array. Can you help me with this problem?
<?php
$a = array("8344", "7076", "6220940", "6220940");
$b = array("8344", "7076", "6220940mistake", "6220940");
var_export(array_diff_assoc($a,$b));
prints
array (
2 => '6220940',
)
see array_diff_assoc
You have empty result because all of the elements in $orginal array are present in array you are comparing against ($new) - value "6220940" is present at index 3.
You should use array_diff_assoc instead of array_diff so you will be comparing array elements with their index assignment.

How to access third level value in multidimensional mixed associative and numeric array

I am working with the array below and wish to ask how should I access/reference values in 'match_id' and 'match_comp_ID'?
I need to reference it in two ways: Question 1: firstly in a foreach statement. This has been answered below:
foreach $jason_a['matches'] as $match {
echo $match['match_id']
echo $match['match_comp_id']
}
Question 2: I want to sort the output from the above by those same two keys by using a sort function which I will call via usort:
function cmp($a, $b)
{
// sort by match_id
$retval = strnatcmp(substr($b->match_id,0,10), substr($a->match_id,0,10));
// if identical, sort by match_comp_id
if(!$retval) $retval = strnatcmp($a->match_comp_id, $b->match_comp_id);
return $retval;
}
usort($json_a, "cmp");
Using match_id or $json['match_id] format in the sort function don't work. I am at a loss to know what to search for.
Array is:
array(4) {
["APIRequestsRemaining"]=> int(920)
["matches"]=> array(3) {
[0]=> array(3) {
["match_id"]=> string(7) "1999477"
["match_static_id"]=> string(7) "1755895"
["match_comp_id"]=> string(4) "1204" }
[1]=> array(3) {
["match_id"]=> string(7) "1999478"
["match_static_id"]=> string(7) "1755891"
["match_comp_id"]=> string(4) "1204" }
[2]=> array(3) {
["match_id"]=> string(7) "1999479"
["match_static_id"]=> string(7) "1755894"
["match_comp_id"]=> string(4) "1204" }
}
["Action"]=> string(5) "today"
["Params"]=> array(4) {
["Action"]=> string(5) "today"
["APIKey"]=> string(31) "xxxx-xxxx-xxxx-xxxx"
["OutputType"]=> string(4) "JSON"
["comp_id"]=> string(4) "1204"
}
The php manual states:
Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type. I think this is my problem. But if arrays cannot be used as keys, then how do I access this key-value?
foreach ($json_a['matches'] as $match) {
// do something with $match['match_id'] and $match['match_comp_id']
}
For part 2 of your question, you really want to pass the 'matches' sub-array to your sort function:
$matches = $json_a['matches'];
usort($matches, 'cmp');
// now the $matches array should be sorted according to rules in function cmp()

PHP : Sort 2 dimension Array

I'm trying to sort a two dimensions array, and I have no idea where to start. I looked at array_multisort, but I don't really found a good solution with this sorting.
I need to sort by time, each time are associate with a race. I need to find who are the best 5 person so the best time.
My array looks like this:
[0]=>
array(2) {
[0]=>
string(15) "Beaumier Mélina"
[1]=>
string(7) "1:29.30"
}
[1]=>
array(2) {
[0]=>
string(14) "Frizzle Émilie"
[2]=>
string(7) "1:47.96"
}
[2]=>
array(3) {
[0]=>
string(18) "Morissette Camélia"
[2]=>
string(7) "1:50.26"
[1]=>
string(7) "1:50.97"
}
You can use usort. You give it a callback function and compare each index of the array. Since you make the callback function you can compare by the time for each index in the array.
http://php.net/usort
From the above documentation:
<?php
function cmp($a, $b)
{
return strcmp($a["fruit"], $b["fruit"]);
}
$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";
usort($fruits, "cmp");
?>

Sorting array numerically based on EYE_SIZE inside array's arrays

I have an array of arrays like this:
array(18) {
[0]=>
array(3) {
["ID"]=>
string(5) "23"
["EYE_SIZE"]=> "203.C"
}
[1]=>
array(2) {
["ID"]=>
string(5) "1"
["EYE_SIZE"]=> "231.2A"
}
[2]=>
array(2) {
["ID"]=>
string(5) "32"
["EYE_SIZE"]=> "231.2B"
}
[3]=>
array(3) {
["ID"]=>
string(5) "90"
["EYE_SIZE"]=> "201A"
}
... and so on
}
And I want the arrays in the array to be sorted alphanumerically based on the EYE_SIZE value. For example, if the array had an EYE_SIZE value of 201A, I would want it to be before arrays with EYE_SIZEs of 203A, 201B, or 201.2A.
Is there a function in PHP that can help me achieve this?
You could use usort and write your own comparison function.
function cmp($a, $b)
{
return strcmp($a["EYE_SIZE"], $b["EYE_SIZE"]);
}
usort($your_array, "cmp");
or with a closure
usort($your_array, function($a, $b){
return strcmp($a["EYE_SIZE"], $b["EYE_SIZE"]);
});

Categories