1

I'm struggling with a stupid issue i can't solve, probably because my head has just gone nuts. But really, i can't figure it out. It probably is simple than i think, but I wasn't able to find a solution on the web either. I have this array, sorted like i want:

array (size=3)
  9 => float 124.58194
  2 => float 52.8428
  8 => float 25.041806

Then, another array has the same keys, but different order

array (size=3)
  8 => string '3' (length=1)
  9 => string '3' (length=1)
  2 => string '2' (length=1)

Can i sort the second one by keys, using the order of the first one? If so, how?

Thanks in advance

1 Answer 1

1

Try this, I tested it, but it will kill any keys in disciple that aren't available in master.

$master = array(
        9 => 999
    ,   2 => 222
    ,   8 => 888
);

$disciple = array(
        8 => 8989
    ,   9 => 9090
    ,   2 => 2020
);

$disciplined = discipline($master, $disciple);
var_dump($disciplined);

function discipline($master, $disciple) {
    $like_keys = array_intersect_key($master, $disciple);
    foreach($like_keys as $key => &$value) {
        $value = $disciple[$key];
    }
    return $like_keys;
}
Sign up to request clarification or add additional context in comments.

2 Comments

A new meaning of zen coding?
@WaleedKhan haha yeah, was trying to think of a good name for the function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.