1

I have 2 arrays with numerical indices that have the same number of keys and similar but not identical values. $unordered has the unordered data, whereas array $ordered has the data in the proper order. I want to sort $unordered in the same order as $ordered, but keep the percentage values in square brackets from $unordered.

$unordered:

[0] => "Horse [1%]"
[1] => "Cat [5%]"
[2] => "Dog [94%]"

$ordered:

[0] => "Cat"
[1] => "Horse"
[2] => "Dog"

Thanks.

6
  • What's the relationship? I don't see any common denominator here Commented Aug 27, 2014 at 19:19
  • 1
    I think he wants $undernored to have the same sequence as set by the $ordered array. Commented Aug 27, 2014 at 19:21
  • 1
    What is the order? Alphabetical is not right. Letter count is not right. So how are you sorting it? Commented Aug 27, 2014 at 19:21
  • @Ohgodwhy: I think he wants to sort $unordered by the order of the values in $ordered. Commented Aug 27, 2014 at 19:22
  • 1
    $ordered is sorta ordered randomly - not alphabetical or letter count - so that's why I'm wondering if there's a way to order $unordered by $ordered rather than sorting $unordered alphabetically/etc. Commented Aug 27, 2014 at 19:35

2 Answers 2

3

usort is your go-to sorting function when it comes to complicated things like your example.

Try something like this. Basically, it parses the value from $unordered, then searches for it in the $ordered array.

usort($unordered, function($a, $b) use($ordered){
    $aVal = explode(' ', $a);
    $bVal = explode(' ', $b);

    return array_search($aVal[0], $ordered) - array_search($bVal[0], $ordered);
});

DEMO: https://eval.in/184201

Sign up to request clarification or add additional context in comments.

5 Comments

Was just in the process of writing something similar. This example worked for me. +1.
For the occasional value, there may be multiple spaces before the [%] eg. A large animal [56%]. Is there a way to modify the explode statements so they don't just look at the first set of characters? Sorry, ran into this problem as I tried to implement it.
@user3600400: A (cheesy) solution would be to do explode(' [', $a) instead. That seems to work. Otherwise, you'd have to use a regex.
I updated both $aVal and $bVal to $aVal = explode(' [', $a); and $bVal = explode(' [', $b); which seemed to work. Didn't work with changing just $aVal. Thanks!
You're welcome. Yeah, I should've mentioned to change both a and b :-)
2

Although Rocket Hazmats looks nicer and is better, but you can probably understand this

 $array = array();
 $array[] = "Horse [5%]"
 $array[] ="Cat [1%]";
 $array[] ="Dog [99%]";
 $ordered = array();
 $ordered[] = "Cat";
 $ordered[] = "Horse";
 $ordered[] = "Dog";
 $var = "";

 $a = array_fill(0, count($ordered), '');
 for($i = 0; $i < count($array); $i++){
    preg_match("/[a-zA-z]+/", $array[$i], $var);
    for($j = 0; $j < count($ordered); $j++){
      if($var[0] == $ordered[$j]) $a[$j] = $array[$i];
    }
 }

 return $a;

Comments

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.