Compare values of 2 arrays, then use the keys of matched values to move filesGet first key in a (possibly) associative array?How do I determine whether an array contains a particular value in Java?Sort array of objects by string property valueDetermine whether an array contains a valueCheck if a value exists in an array in RubyHow to Sort Multi-dimensional Array by Value?PHP array delete by value (not key)Copy array by valueHow to compare arrays in JavaScript?Remove duplicate values from JS array
Are soroban (Japanese abacus) classes worth doing?
How to test soql with For Update statement
Why not make one big CPU core?
How to avoid offending original culture when making conculture inspired from original
What should I be aware of in buying second-hand sinks and toilets?
Is fission/fusion to iron the most efficient way to convert mass to energy?
Is it unethical to quit my job during company crisis?
How would Japanese people react to someone refusing to say “itadakimasu” for religious reasons?
Interview was just a one hour panel. Got an offer the next day; do I accept or is this a red flag?
The last tree in the Universe
How did Avada Kedavra get its name?
How do credit card companies know what type of business I'm paying for?
...and then she held the gun
How to address players struggling with simple controls?
Cant bend fingertip when finger is straight
Will users know a CardView is clickable
How to ask if I can mow my neighbor's lawn
Using roof rails to set up hammock
Can an open source licence be revoked if it violates employer's IP?
Do items with curse of vanishing disappear from shulker boxes?
How to know whether to write accidentals as sharps or flats?
Bullying by school - Submitted PhD thesis but not allowed to proceed to viva until change to new supervisor
Does anyone recognize these rockets, and their location?
How can this shape perfectly cover a cube?
Compare values of 2 arrays, then use the keys of matched values to move files
Get first key in a (possibly) associative array?How do I determine whether an array contains a particular value in Java?Sort array of objects by string property valueDetermine whether an array contains a valueCheck if a value exists in an array in RubyHow to Sort Multi-dimensional Array by Value?PHP array delete by value (not key)Copy array by valueHow to compare arrays in JavaScript?Remove duplicate values from JS array
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm trying to set up a script that will move files from in a particular directory to a specific subdirectory, depending on the prefix of the filename.
As it stands, I have two arrays, one set out as such:
$arr1 = array(
[12] => "prefix1",
[34] => "prefix2"
)
etc etc, where the keys are user IDs from our database, and the values are the prefix of that user's email address.
The other is like so:
$arr2 = array(
["prefix1_filename.ext"] => "prefix1",
["prefix2_filename.ext"] => "prefix2"
)
And so on.
These arrays will not be the same length.
What I want to do is compare the two arrays, and where a value in arr1 matches a value in arr2, take the corresponding keys and push those to a new array that looks like this:
$arr3 = array(
[12] => "prefix1_filename.ext",
[34] => "prefix2_filename.ext"
)
This will then be used to move the files into a subdirectory named for the id - eg /foo/bar/12/prefix1_filename.ext, /foo/bar/34/prefix2_filename.ext etc etc.
I've tried using array_combine, but as the two are not the same length, an error results. I've also tried intersecting them, to no avail.
Can anyone point me towards a solution that will achieve this? I can flip the arrays to make the keys the values if that will make this simpler.
Many thanks in advance.
php arrays
add a comment |
I'm trying to set up a script that will move files from in a particular directory to a specific subdirectory, depending on the prefix of the filename.
As it stands, I have two arrays, one set out as such:
$arr1 = array(
[12] => "prefix1",
[34] => "prefix2"
)
etc etc, where the keys are user IDs from our database, and the values are the prefix of that user's email address.
The other is like so:
$arr2 = array(
["prefix1_filename.ext"] => "prefix1",
["prefix2_filename.ext"] => "prefix2"
)
And so on.
These arrays will not be the same length.
What I want to do is compare the two arrays, and where a value in arr1 matches a value in arr2, take the corresponding keys and push those to a new array that looks like this:
$arr3 = array(
[12] => "prefix1_filename.ext",
[34] => "prefix2_filename.ext"
)
This will then be used to move the files into a subdirectory named for the id - eg /foo/bar/12/prefix1_filename.ext, /foo/bar/34/prefix2_filename.ext etc etc.
I've tried using array_combine, but as the two are not the same length, an error results. I've also tried intersecting them, to no avail.
Can anyone point me towards a solution that will achieve this? I can flip the arrays to make the keys the values if that will make this simpler.
Many thanks in advance.
php arrays
[12] => "prefix1",
<-- should this beprefix
or should["prefix_filename.ext"] => "prefix",
beprefix1
?
– Jamie - Fenrir Digital Ltd
Mar 25 at 2:27
1
Have changed the examples so it's consistently prefix1 where before it was a mix of prefix and prefix1
– Jamie B
Mar 25 at 2:30
usearray_flip()
and match keys
– Bira
Mar 25 at 2:37
Doing this unfortunately outputs the values from arr1 and arr2 as key => value pairs.
– Jamie B
Mar 25 at 2:51
add a comment |
I'm trying to set up a script that will move files from in a particular directory to a specific subdirectory, depending on the prefix of the filename.
As it stands, I have two arrays, one set out as such:
$arr1 = array(
[12] => "prefix1",
[34] => "prefix2"
)
etc etc, where the keys are user IDs from our database, and the values are the prefix of that user's email address.
The other is like so:
$arr2 = array(
["prefix1_filename.ext"] => "prefix1",
["prefix2_filename.ext"] => "prefix2"
)
And so on.
These arrays will not be the same length.
What I want to do is compare the two arrays, and where a value in arr1 matches a value in arr2, take the corresponding keys and push those to a new array that looks like this:
$arr3 = array(
[12] => "prefix1_filename.ext",
[34] => "prefix2_filename.ext"
)
This will then be used to move the files into a subdirectory named for the id - eg /foo/bar/12/prefix1_filename.ext, /foo/bar/34/prefix2_filename.ext etc etc.
I've tried using array_combine, but as the two are not the same length, an error results. I've also tried intersecting them, to no avail.
Can anyone point me towards a solution that will achieve this? I can flip the arrays to make the keys the values if that will make this simpler.
Many thanks in advance.
php arrays
I'm trying to set up a script that will move files from in a particular directory to a specific subdirectory, depending on the prefix of the filename.
As it stands, I have two arrays, one set out as such:
$arr1 = array(
[12] => "prefix1",
[34] => "prefix2"
)
etc etc, where the keys are user IDs from our database, and the values are the prefix of that user's email address.
The other is like so:
$arr2 = array(
["prefix1_filename.ext"] => "prefix1",
["prefix2_filename.ext"] => "prefix2"
)
And so on.
These arrays will not be the same length.
What I want to do is compare the two arrays, and where a value in arr1 matches a value in arr2, take the corresponding keys and push those to a new array that looks like this:
$arr3 = array(
[12] => "prefix1_filename.ext",
[34] => "prefix2_filename.ext"
)
This will then be used to move the files into a subdirectory named for the id - eg /foo/bar/12/prefix1_filename.ext, /foo/bar/34/prefix2_filename.ext etc etc.
I've tried using array_combine, but as the two are not the same length, an error results. I've also tried intersecting them, to no avail.
Can anyone point me towards a solution that will achieve this? I can flip the arrays to make the keys the values if that will make this simpler.
Many thanks in advance.
php arrays
php arrays
edited Mar 25 at 2:29
Jamie B
asked Mar 25 at 2:23
Jamie BJamie B
85
85
[12] => "prefix1",
<-- should this beprefix
or should["prefix_filename.ext"] => "prefix",
beprefix1
?
– Jamie - Fenrir Digital Ltd
Mar 25 at 2:27
1
Have changed the examples so it's consistently prefix1 where before it was a mix of prefix and prefix1
– Jamie B
Mar 25 at 2:30
usearray_flip()
and match keys
– Bira
Mar 25 at 2:37
Doing this unfortunately outputs the values from arr1 and arr2 as key => value pairs.
– Jamie B
Mar 25 at 2:51
add a comment |
[12] => "prefix1",
<-- should this beprefix
or should["prefix_filename.ext"] => "prefix",
beprefix1
?
– Jamie - Fenrir Digital Ltd
Mar 25 at 2:27
1
Have changed the examples so it's consistently prefix1 where before it was a mix of prefix and prefix1
– Jamie B
Mar 25 at 2:30
usearray_flip()
and match keys
– Bira
Mar 25 at 2:37
Doing this unfortunately outputs the values from arr1 and arr2 as key => value pairs.
– Jamie B
Mar 25 at 2:51
[12] => "prefix1",
<-- should this be prefix
or should ["prefix_filename.ext"] => "prefix",
be prefix1
?– Jamie - Fenrir Digital Ltd
Mar 25 at 2:27
[12] => "prefix1",
<-- should this be prefix
or should ["prefix_filename.ext"] => "prefix",
be prefix1
?– Jamie - Fenrir Digital Ltd
Mar 25 at 2:27
1
1
Have changed the examples so it's consistently prefix1 where before it was a mix of prefix and prefix1
– Jamie B
Mar 25 at 2:30
Have changed the examples so it's consistently prefix1 where before it was a mix of prefix and prefix1
– Jamie B
Mar 25 at 2:30
use
array_flip()
and match keys– Bira
Mar 25 at 2:37
use
array_flip()
and match keys– Bira
Mar 25 at 2:37
Doing this unfortunately outputs the values from arr1 and arr2 as key => value pairs.
– Jamie B
Mar 25 at 2:51
Doing this unfortunately outputs the values from arr1 and arr2 as key => value pairs.
– Jamie B
Mar 25 at 2:51
add a comment |
2 Answers
2
active
oldest
votes
This can be done, with a bit of trickery.
First of all, let's take the values from $arr1 that are present in $arr2:
$isect1 = array_intersect($arr1, $arr2);
Then, get the opposite
$isect2 = array_intersect($arr2, $arr1);
Now we have keys from both arrays, but we want to sort them to ensure they're in the same order. We'll need to flip them first.
$sorted1 = array_sort(array_flip($isect1));
$sorted2 = array_sort(array_flip($isect2));
Now we have that, we can just combine them back
$arr3 = array_combine(array_values($sorted1), array_values($sorted2));
Demo out of the way, to cut down on the variable copying, we can do it in one line:
$arr3 = array_combine(array_values(array_sort(array_flip(array_intersect($arr1, $arr2)))), array_values(array_sort(array_flip(array_intersect($arr2, $arr1)))));
Phew. Got there in the end!
EDIT
Here's my Psy Shell output testing this, added some extra "non matching" values in to test the filtering:
>>> $arr1 = [12 => "prefix1", 34 => "prefix2", 99 => "some_other_prefix"];
=> [
12 => "prefix1",
34 => "prefix2",
99 => "some_other_prefix",
]
>>> $arr2 = ["prefix1_filename.ext" => "prefix1", "prefix2_filename.ext" => "prefix2", "another_prefix_filename.ext" => "another_prexix"];
=> [
"prefix1_filename.ext" => "prefix1",
"prefix2_filename.ext" => "prefix2",
"another_prefix_filename.ext" => "another_prexix",
]
>>> $arr3 = array_combine(array_values(array_sort(array_flip(array_intersect($arr1, $arr2)))), array_values(array_sort(array_flip(array_intersect($arr2, $arr1)))));
=> [
12 => "prefix1_filename.ext",
34 => "prefix2_filename.ext",
]
Perfect! Does exactly what I need. Didn't need to sort the flipped arrays in the end. Thanks!
– Jamie B
Mar 25 at 3:42
@JamieB for this example you don’t, but worth doing in case you get an array in a different order... it just ensures that the results from both will be the same order
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:49
Noted. For what it's worth, the two input arrays are sorted beforehand, in case that makes a difference?
– Jamie B
Mar 25 at 3:50
That’s fine then, as long as you know the input is clean. I’ve worked with too many APIs in my time, I’ve grown to trust no one but myself haha
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:55
add a comment |
note : $arr2 can't be compared because difference of array keys
<?php
$arr1 = array(12=> "prefix1",34 => "prefix2");
$arr3 = array(12 => "prefix_filename.ext",
34 => "prefix2_filename.ext"
);
$arr2 = array(
"prefix_filename.ext" => "prefix",
"prefix2_filename.ext" => "prefix2"
);
$result=array_intersect($arr1,$arr2);
print_r($result);
?>
I've changed the inconsistencies of prefix/prefix1
– Jamie B
Mar 25 at 2:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330521%2fcompare-values-of-2-arrays-then-use-the-keys-of-matched-values-to-move-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This can be done, with a bit of trickery.
First of all, let's take the values from $arr1 that are present in $arr2:
$isect1 = array_intersect($arr1, $arr2);
Then, get the opposite
$isect2 = array_intersect($arr2, $arr1);
Now we have keys from both arrays, but we want to sort them to ensure they're in the same order. We'll need to flip them first.
$sorted1 = array_sort(array_flip($isect1));
$sorted2 = array_sort(array_flip($isect2));
Now we have that, we can just combine them back
$arr3 = array_combine(array_values($sorted1), array_values($sorted2));
Demo out of the way, to cut down on the variable copying, we can do it in one line:
$arr3 = array_combine(array_values(array_sort(array_flip(array_intersect($arr1, $arr2)))), array_values(array_sort(array_flip(array_intersect($arr2, $arr1)))));
Phew. Got there in the end!
EDIT
Here's my Psy Shell output testing this, added some extra "non matching" values in to test the filtering:
>>> $arr1 = [12 => "prefix1", 34 => "prefix2", 99 => "some_other_prefix"];
=> [
12 => "prefix1",
34 => "prefix2",
99 => "some_other_prefix",
]
>>> $arr2 = ["prefix1_filename.ext" => "prefix1", "prefix2_filename.ext" => "prefix2", "another_prefix_filename.ext" => "another_prexix"];
=> [
"prefix1_filename.ext" => "prefix1",
"prefix2_filename.ext" => "prefix2",
"another_prefix_filename.ext" => "another_prexix",
]
>>> $arr3 = array_combine(array_values(array_sort(array_flip(array_intersect($arr1, $arr2)))), array_values(array_sort(array_flip(array_intersect($arr2, $arr1)))));
=> [
12 => "prefix1_filename.ext",
34 => "prefix2_filename.ext",
]
Perfect! Does exactly what I need. Didn't need to sort the flipped arrays in the end. Thanks!
– Jamie B
Mar 25 at 3:42
@JamieB for this example you don’t, but worth doing in case you get an array in a different order... it just ensures that the results from both will be the same order
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:49
Noted. For what it's worth, the two input arrays are sorted beforehand, in case that makes a difference?
– Jamie B
Mar 25 at 3:50
That’s fine then, as long as you know the input is clean. I’ve worked with too many APIs in my time, I’ve grown to trust no one but myself haha
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:55
add a comment |
This can be done, with a bit of trickery.
First of all, let's take the values from $arr1 that are present in $arr2:
$isect1 = array_intersect($arr1, $arr2);
Then, get the opposite
$isect2 = array_intersect($arr2, $arr1);
Now we have keys from both arrays, but we want to sort them to ensure they're in the same order. We'll need to flip them first.
$sorted1 = array_sort(array_flip($isect1));
$sorted2 = array_sort(array_flip($isect2));
Now we have that, we can just combine them back
$arr3 = array_combine(array_values($sorted1), array_values($sorted2));
Demo out of the way, to cut down on the variable copying, we can do it in one line:
$arr3 = array_combine(array_values(array_sort(array_flip(array_intersect($arr1, $arr2)))), array_values(array_sort(array_flip(array_intersect($arr2, $arr1)))));
Phew. Got there in the end!
EDIT
Here's my Psy Shell output testing this, added some extra "non matching" values in to test the filtering:
>>> $arr1 = [12 => "prefix1", 34 => "prefix2", 99 => "some_other_prefix"];
=> [
12 => "prefix1",
34 => "prefix2",
99 => "some_other_prefix",
]
>>> $arr2 = ["prefix1_filename.ext" => "prefix1", "prefix2_filename.ext" => "prefix2", "another_prefix_filename.ext" => "another_prexix"];
=> [
"prefix1_filename.ext" => "prefix1",
"prefix2_filename.ext" => "prefix2",
"another_prefix_filename.ext" => "another_prexix",
]
>>> $arr3 = array_combine(array_values(array_sort(array_flip(array_intersect($arr1, $arr2)))), array_values(array_sort(array_flip(array_intersect($arr2, $arr1)))));
=> [
12 => "prefix1_filename.ext",
34 => "prefix2_filename.ext",
]
Perfect! Does exactly what I need. Didn't need to sort the flipped arrays in the end. Thanks!
– Jamie B
Mar 25 at 3:42
@JamieB for this example you don’t, but worth doing in case you get an array in a different order... it just ensures that the results from both will be the same order
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:49
Noted. For what it's worth, the two input arrays are sorted beforehand, in case that makes a difference?
– Jamie B
Mar 25 at 3:50
That’s fine then, as long as you know the input is clean. I’ve worked with too many APIs in my time, I’ve grown to trust no one but myself haha
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:55
add a comment |
This can be done, with a bit of trickery.
First of all, let's take the values from $arr1 that are present in $arr2:
$isect1 = array_intersect($arr1, $arr2);
Then, get the opposite
$isect2 = array_intersect($arr2, $arr1);
Now we have keys from both arrays, but we want to sort them to ensure they're in the same order. We'll need to flip them first.
$sorted1 = array_sort(array_flip($isect1));
$sorted2 = array_sort(array_flip($isect2));
Now we have that, we can just combine them back
$arr3 = array_combine(array_values($sorted1), array_values($sorted2));
Demo out of the way, to cut down on the variable copying, we can do it in one line:
$arr3 = array_combine(array_values(array_sort(array_flip(array_intersect($arr1, $arr2)))), array_values(array_sort(array_flip(array_intersect($arr2, $arr1)))));
Phew. Got there in the end!
EDIT
Here's my Psy Shell output testing this, added some extra "non matching" values in to test the filtering:
>>> $arr1 = [12 => "prefix1", 34 => "prefix2", 99 => "some_other_prefix"];
=> [
12 => "prefix1",
34 => "prefix2",
99 => "some_other_prefix",
]
>>> $arr2 = ["prefix1_filename.ext" => "prefix1", "prefix2_filename.ext" => "prefix2", "another_prefix_filename.ext" => "another_prexix"];
=> [
"prefix1_filename.ext" => "prefix1",
"prefix2_filename.ext" => "prefix2",
"another_prefix_filename.ext" => "another_prexix",
]
>>> $arr3 = array_combine(array_values(array_sort(array_flip(array_intersect($arr1, $arr2)))), array_values(array_sort(array_flip(array_intersect($arr2, $arr1)))));
=> [
12 => "prefix1_filename.ext",
34 => "prefix2_filename.ext",
]
This can be done, with a bit of trickery.
First of all, let's take the values from $arr1 that are present in $arr2:
$isect1 = array_intersect($arr1, $arr2);
Then, get the opposite
$isect2 = array_intersect($arr2, $arr1);
Now we have keys from both arrays, but we want to sort them to ensure they're in the same order. We'll need to flip them first.
$sorted1 = array_sort(array_flip($isect1));
$sorted2 = array_sort(array_flip($isect2));
Now we have that, we can just combine them back
$arr3 = array_combine(array_values($sorted1), array_values($sorted2));
Demo out of the way, to cut down on the variable copying, we can do it in one line:
$arr3 = array_combine(array_values(array_sort(array_flip(array_intersect($arr1, $arr2)))), array_values(array_sort(array_flip(array_intersect($arr2, $arr1)))));
Phew. Got there in the end!
EDIT
Here's my Psy Shell output testing this, added some extra "non matching" values in to test the filtering:
>>> $arr1 = [12 => "prefix1", 34 => "prefix2", 99 => "some_other_prefix"];
=> [
12 => "prefix1",
34 => "prefix2",
99 => "some_other_prefix",
]
>>> $arr2 = ["prefix1_filename.ext" => "prefix1", "prefix2_filename.ext" => "prefix2", "another_prefix_filename.ext" => "another_prexix"];
=> [
"prefix1_filename.ext" => "prefix1",
"prefix2_filename.ext" => "prefix2",
"another_prefix_filename.ext" => "another_prexix",
]
>>> $arr3 = array_combine(array_values(array_sort(array_flip(array_intersect($arr1, $arr2)))), array_values(array_sort(array_flip(array_intersect($arr2, $arr1)))));
=> [
12 => "prefix1_filename.ext",
34 => "prefix2_filename.ext",
]
edited Mar 25 at 3:18
answered Mar 25 at 3:09
Jamie - Fenrir Digital LtdJamie - Fenrir Digital Ltd
1,166720
1,166720
Perfect! Does exactly what I need. Didn't need to sort the flipped arrays in the end. Thanks!
– Jamie B
Mar 25 at 3:42
@JamieB for this example you don’t, but worth doing in case you get an array in a different order... it just ensures that the results from both will be the same order
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:49
Noted. For what it's worth, the two input arrays are sorted beforehand, in case that makes a difference?
– Jamie B
Mar 25 at 3:50
That’s fine then, as long as you know the input is clean. I’ve worked with too many APIs in my time, I’ve grown to trust no one but myself haha
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:55
add a comment |
Perfect! Does exactly what I need. Didn't need to sort the flipped arrays in the end. Thanks!
– Jamie B
Mar 25 at 3:42
@JamieB for this example you don’t, but worth doing in case you get an array in a different order... it just ensures that the results from both will be the same order
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:49
Noted. For what it's worth, the two input arrays are sorted beforehand, in case that makes a difference?
– Jamie B
Mar 25 at 3:50
That’s fine then, as long as you know the input is clean. I’ve worked with too many APIs in my time, I’ve grown to trust no one but myself haha
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:55
Perfect! Does exactly what I need. Didn't need to sort the flipped arrays in the end. Thanks!
– Jamie B
Mar 25 at 3:42
Perfect! Does exactly what I need. Didn't need to sort the flipped arrays in the end. Thanks!
– Jamie B
Mar 25 at 3:42
@JamieB for this example you don’t, but worth doing in case you get an array in a different order... it just ensures that the results from both will be the same order
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:49
@JamieB for this example you don’t, but worth doing in case you get an array in a different order... it just ensures that the results from both will be the same order
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:49
Noted. For what it's worth, the two input arrays are sorted beforehand, in case that makes a difference?
– Jamie B
Mar 25 at 3:50
Noted. For what it's worth, the two input arrays are sorted beforehand, in case that makes a difference?
– Jamie B
Mar 25 at 3:50
That’s fine then, as long as you know the input is clean. I’ve worked with too many APIs in my time, I’ve grown to trust no one but myself haha
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:55
That’s fine then, as long as you know the input is clean. I’ve worked with too many APIs in my time, I’ve grown to trust no one but myself haha
– Jamie - Fenrir Digital Ltd
Mar 25 at 3:55
add a comment |
note : $arr2 can't be compared because difference of array keys
<?php
$arr1 = array(12=> "prefix1",34 => "prefix2");
$arr3 = array(12 => "prefix_filename.ext",
34 => "prefix2_filename.ext"
);
$arr2 = array(
"prefix_filename.ext" => "prefix",
"prefix2_filename.ext" => "prefix2"
);
$result=array_intersect($arr1,$arr2);
print_r($result);
?>
I've changed the inconsistencies of prefix/prefix1
– Jamie B
Mar 25 at 2:50
add a comment |
note : $arr2 can't be compared because difference of array keys
<?php
$arr1 = array(12=> "prefix1",34 => "prefix2");
$arr3 = array(12 => "prefix_filename.ext",
34 => "prefix2_filename.ext"
);
$arr2 = array(
"prefix_filename.ext" => "prefix",
"prefix2_filename.ext" => "prefix2"
);
$result=array_intersect($arr1,$arr2);
print_r($result);
?>
I've changed the inconsistencies of prefix/prefix1
– Jamie B
Mar 25 at 2:50
add a comment |
note : $arr2 can't be compared because difference of array keys
<?php
$arr1 = array(12=> "prefix1",34 => "prefix2");
$arr3 = array(12 => "prefix_filename.ext",
34 => "prefix2_filename.ext"
);
$arr2 = array(
"prefix_filename.ext" => "prefix",
"prefix2_filename.ext" => "prefix2"
);
$result=array_intersect($arr1,$arr2);
print_r($result);
?>
note : $arr2 can't be compared because difference of array keys
<?php
$arr1 = array(12=> "prefix1",34 => "prefix2");
$arr3 = array(12 => "prefix_filename.ext",
34 => "prefix2_filename.ext"
);
$arr2 = array(
"prefix_filename.ext" => "prefix",
"prefix2_filename.ext" => "prefix2"
);
$result=array_intersect($arr1,$arr2);
print_r($result);
?>
answered Mar 25 at 2:44
HasanHasan
244
244
I've changed the inconsistencies of prefix/prefix1
– Jamie B
Mar 25 at 2:50
add a comment |
I've changed the inconsistencies of prefix/prefix1
– Jamie B
Mar 25 at 2:50
I've changed the inconsistencies of prefix/prefix1
– Jamie B
Mar 25 at 2:50
I've changed the inconsistencies of prefix/prefix1
– Jamie B
Mar 25 at 2:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330521%2fcompare-values-of-2-arrays-then-use-the-keys-of-matched-values-to-move-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
[12] => "prefix1",
<-- should this beprefix
or should["prefix_filename.ext"] => "prefix",
beprefix1
?– Jamie - Fenrir Digital Ltd
Mar 25 at 2:27
1
Have changed the examples so it's consistently prefix1 where before it was a mix of prefix and prefix1
– Jamie B
Mar 25 at 2:30
use
array_flip()
and match keys– Bira
Mar 25 at 2:37
Doing this unfortunately outputs the values from arr1 and arr2 as key => value pairs.
– Jamie B
Mar 25 at 2:51