B
B
busidoway2016-11-16 15:43:53
PHP
busidoway, 2016-11-16 15:43:53

How to compare and sort two arrays in php?

Good afternoon.
Let's say there is an array:
Array (
[0] => p
[1] => s
[2] => b
[3] => a
)
and a second array (sorted alphabetically):
Array (
[0] => a
[ 1] => b
[2] => p
[3] => s
)
How can we compare both arrays and sort the second one so that we get a "fish" while preserving the keys? Example:
Array (
[2] => p
[3] => s
[1] => b
[0] => a
)
Instead of the first array, you can use a regular string ("fish").

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
busidoway, 2016-11-16
@busidoway

I solved the issue with
foreach($array1 as $k => $v){
foreach($array2 as $kk => $vv){
if($v == $vv){
$marray[$kk] = $v;
}
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question