I have the following array structure with master data with thousands of records.
Array
(
[0] => 0,1
[1] => 0,0
[2] => 0,2
[3] => 0,3
[4] => 10,2
)
I have a second array with a smaller subset. Such as
Array
(
[0] => 0,1
[1] => 0,0
)
I would like to find second array in first array in the extact same order of elements present in second array. But rather than doing an intersect I would like to find the key (or keys) from first array as well. I have been wrecking my brain on this...
UPDATED:
The keys are unique . So far example in above array I would like to see output of:
Array2 found in array1 (starting at key 0).
Second Example:
Array
(
[a] => 0,1
[b] => 0,0
[c] => 0,2
[d] => 0,3
[e] => 10,2
)
second array
Array
(
[1] => 0,3
[2] => 10,2
)
expected output:
second array match in array A, starting at key d of array A..
hope that clears it.
The best solution to get matching keys is
$result_array = array_intersect_assoc($array1, $array2);
$result_array_keys = array_keys(array_intersect_assoc($array1, $array2));
print_r($result_array);
print_r($result_array_keys); // this gives matching keys array
I don't understand what is wrong with array_intersect.
As you describe your expected output it seems as array_intersect is perfect.
$a = Array
(
15 => "0,1",
16 => "0,0",
2 => "0,2",
3 => "0,3",
4 => "10,2"
);
$b = Array
(
0 => "0,1",
1 => "0,0"
);
Var_dump(array_intersect($a, $b));
Output:
array(2) {
[15]=> string(3) "0,1"
[16]=> string(3) "0,0"
}
https://3v4l.org/KG1v6
Or if MonkeyZeus is correct maybe this can work for you?
I match array_intersect, then make sure the keys is the same.
$intersect = array_intersect($a, $b);
$keys = array_keys($intersect);
If($keys == array_keys($b)){
Echo "they match";
}else{
Echo "don't match";
}
https://3v4l.org/iiNmd
After OPs edit it seems it is a simple array_intersect that is needed.
https://3v4l.org/gIb10
$intersect = array_intersect($a, $b);
Var_dump($intersect);
Echo "matching keys is: " . Implode(", ", array_keys($intersect));
None of the solutions posted above work. What helped me is to re-structure my array and answer from here: Find array in array, in sequence
Related
I want to need multiple array combaine in one array
I have array
array(
0=> test 1
)
array(
0=> test 2
)
array(
0=> test 3
)
I need expected output
`array(
0=>Test1
1=>Test2
2=>test3
)`
You can use the array_merge() for this. The array_merge() function merges one or more arrays into one array.If two or more array elements have the same key, the last one overrides the others.
Syntax:
array_merge(array ...$arrays): array
Example:
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
Result:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
You can check more here.
$a = array('test_1');
$b = array('test_2');
$c = array('test_3');
print_r(array_merge($a,$b,$c));
O/P - Array ( [0] => test_1 [1] => test_2 [2] => test_3 )
Hope you are doing well and good.
So, as per your requirement i found solution to get result.
$array1 = [0 => "Test 1"]; $array2 = [0 => "Test 2"]; $array3 = [0 => "Test 3"];
print_r(array_merge($array1,$array2,$array3));
In the above example you have to merge the n number of array with single array, so for that you need to use array function which is array_merge(array ...$array).
What is array_merge()?
The array_merge() function merges one or more arrays into one array.
Tip: You can assign one array to the function, or as many as you like.
Note: If two or more array elements have the same key, the last one overrides the others.
I have lot arrays and i want to only print number of 2 and first arrays
i use array_slice but there is still a problem
Arrays:
Array
(
[0] => 441
[1] => Awesome
)
Array
(
[0] => 570
[1] => Noons
)
Array
(
[0] => 571
[1] => Roods
)
I need to like this:
Array
(
[0] => 441
[1] => Awesome
)
Array
(
[0] => 570
[1] => Noons
)
Basically you seem to only need:
array_slice(array_unique(array_column($Myarrays, 'nidtitle')), 0, 2);
This should be done instead of the entire code you use to generate the arrays.
Short explanation:
array_column will get the element nidtitle from each "row" (array entry) in $Myarrays
After that we run that column through a unique function
Then we get the first 2 elements with an array_slice
This should do the job:
$finalarray = array_slice($Myarray, 0, 2);
print_r($finalarray);
You can use array_slice to get the items you want.
$arr = array(Array(441,"Awesome"),
Array(570,"Noons"),
Array(571,"Roods"));
$two = array_slice($arr, 0,2);
Var_dump($two);
Array_slice second parameter is where the slice should start.
Third parameter is count of values to slice.
https://3v4l.org/NPcJT
How can I sort an array by value, but instead of changing the position of the values, change the position of the keys?
Array
(
[0] => 16
[1] => 12
[2] => 30
)
When I sort this array, I want to get the output like this:
Array
(
[1] => 16
[0] => 12
[2] => 30
)
Your starting array:
$a = [16, 12, 30];
First make a copy:
$b = $a;
Then use asort on one of them to sort it while maintaining the key association:
asort($a);
Then use array_combine with array_keys to create your result array using the the keys from the sorted array, and the values from the unsorted array.
$result = array_combine(array_keys($a), $b);
I want to get the array_diff of these two array and echo by key
$s1=1;
$s2=7;
$s3=8;
$r=array("$s1","$s2","$s3");
$rr=array("1","2","3","4","5","6","7","8","9","10");
$rrr=array_diff($r,$rr);
echo $rrr[0];
I was hoping to get a result like 2 but i got an error. Someone help out here.
The documentation for array_diff says that it
Compares array1 against one or more other arrays and returns the
values in array1 that are not present in any of the other arrays.
Your array1 is $r, and your other array is $rr.
$r is essentially equivalent to
$r = array("1","7","8");
We can clearly see that there are no values in $r that are not present in $rr. ("1", "7", "8" are all in $rr.)
Thus, you will receive a E_NOTICE when you try to access $rrr[0], because $rrr is empty.
Perhaps you meant to reverse the order of the two arguments.
$rrr = array_diff($rr, $r);
As Loop Me pointed out, array_diff does not reorder your indices.
What this means is that your array is now like this.
Array
(
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[8] => 9
[9] => 10
)
You can reorder them with array_values.
$rrr = array_values($rrr);
Demo
I am trying to compare a sub-array with the indices with a main array. I tried using array_diff, array_intersect_key, etc, but unable to find the way to proceed, other than using a stupid for loop and doing some old school method.
Okay, let me clarify my question. Say, I have two arrays:
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] => W
[6] => o
[7] => r
[8] => l
[9] => d
[10] => !
)
Array
(
[1] => e
[5] => W
[7] => r
[10] => !
)
The first one is the main array of full string: HelloWorld! and the second one is the sub-array, which has some selected indices and it's values, given by the users. This can also be wrong, for eg:
Array
(
[1] => F
[5] => a
[7] => 6
[10] => t
)
Now, when I pass the first array, it should obviously return true and the next one should return false. Is this possible with the built-in functions using PHP? Thanks in advance.
You should use array_intersect_assoc() if you want to check also if the keys match
if (array_intersect_assoc ($a,$b)==$b) {} else {}
given that $a is the full array and $b the subset to test against.
I think array_intersect_uassoc() is the function you're looking for. It compares based on both keys and values. Here's a quick example:
$array1 = array(0 => 'h', 1 => 'e', 2 => 'l', 3 => 'o');
$array2 = array(0 => 'h', 1 => 'e');
$array3 = array(0 => 'h', 1 => 'z');
var_dump(array_intersect_uassoc($array1, $array2, 'strcasecmp'));
var_dump(array_intersect_uassoc($array1, $array3, 'strcasecmp'));
Output:
array(2) {
[0]=>
string(1) "h"
[1]=>
string(1) "e"
}
array(1) {
[0]=>
string(1) "h"
}
You then compare the size of the second array with the size of the returned array - if they are equal, all matches are right. If the returned array is smaller - there's a mistake in the second array.
Replace strcasecmp() with a callback to your liking and profit :)
Cheers
$array3 = array_diff($array2,$array1); //array2 is checking either present or not in array1
and check array3 is empty. if array3 is empty then matched and if not empty than does not matched.
if (array_diff_assoc($array2, $array1)) {
echo 'array 2 is not an exact subset of array 1';
}
array_diff_assoc returns all elements from $array2 which are not in $array1. So if array 2 is a subset of array 1, the return is an empty array, which evaluates to false, and otherwise a non-empty array, which evaluates to true.