-2

I've seen a few questions with similar topics, but most of them used arrays such as [1, 2, 3, 4, 5] for sorting, as opposed to what I need to use, which is [1, 1, 2, 1, 4], with the largest numbers coming first, and the zeroes removed.

Example: [4, 3, 8, 0, 0] as the array to sort by, and ["hello", "my", "name", "is", "indigo"] as the array to be sorted. This should be turned into: ["name", "hello", "my"] because the zeroes are removed.

My main issue is that most methods I've tried end up assigning the values in one array to the numbers in another. Example:

[4, 1, 1, 4, 8] and ["hello", "my", "name", "is", "indigo"]

should return ["indigo", "is", "hello", "my", "name"], but returns ["indigo", "hello", "hello", "my", "my"], or something to that effect.

9
  • use php array sort() function Commented Oct 28, 2015 at 13:52
  • rsort() and array_filter() ? Commented Oct 28, 2015 at 13:54
  • try something on your own and let us know. Commented Oct 28, 2015 at 13:54
  • Possible duplicate of array_filter based on keys from another array Commented Oct 28, 2015 at 14:01
  • The question editing is getting a bit out of hand so the answers look off topic now. Commented Oct 28, 2015 at 14:01

3 Answers 3

1

I think you are looking for something like this?

$words = array("hello", "my", "name", "is", "indigo");
$order = array(0, 1, 3, 2, 4);
$result = array_map(function($o) use ($words){
    return $words[$o];
}, $order);

Which produces this:

array(5) {
    [0] = string(5) "hello"
    [1] = string(2) "my"
    [2] = string(2) "is"
    [3] = string(4) "name"
    [4] = string(6) "indigo"
}

Update

I have reread your question but I am still unsure what you seek? I have updated my original code, but now 0's are removed and it is ordered largest numbers to smallest. Although if the 0's are removed you cannot access the first element of the array?

If this is not correct, perhaps you could explain what it is doing wrong, and what it needs to be doing differently. I suspect the issue is just a language breakdown.

$words = array("hello", "my", "name", "is", "indigo");
$order = array(0, 0, 4, 4, 3, 1);

$order  = array_filter($order); // remove 0's
rsort($order);                  // Largest First
$result = array_map(function($o) use ($words){
    return $words[$o];
}, $order);

Which produces this:

array(4) {
    [0] = string(6) "indigo"
    [1] = string(6) "indigo"
    [2] = string(2) "is"
    [3] = string(2) "my"
}
Sign up to request clarification or add additional context in comments.

4 Comments

This actually almost works, but it assigns things like [0, 0, 4, 4] + ["text1", "text2", "text3", "text4"] as ["text1", "text1", "text3", "text3"]
@Hate9 which is exactly what I thought you wanted? You will need to elaborate more if not.
My initial question was too vague, but I've edited it to make more sense since.
This is really difficult to explain. So, what I meant with the zeroes is that if you had, say, $words = array("hello", "my", "name", "is", "indigo"); $order = array(0, 0, 4, 4, 3); then removing the zeroes would remove "hello" and "my" from $words. I think I can do that, though. My main issue now is getting the words sorted by order, without simply assigning words' values to order's locations on a new array. Essentially, I would need the example I gave in this comment to return ["indigo", "name", "is"] or ["indigo, "is", "name"].
0

you want to use rsort()

solution

$arrayVariable = array(1, 1, 2, 1, 4);

rsort($arrayVariable);

here is the manual rsort()

1 Comment

You misunderstood my question. I've edited it to make it more understandable, but you'll probably want to remove this question.
0

Update

I finally figured out what was wrong with what I was doing, and fixed it. For anyone who was curious as to what I was trying to do (since I was apparently shit at it), or how I eventually did it, here's my code:

$words=array("hello","my","name","is","indigo");
$sort=array(4,3,8,0,0);
$count=0;
foreach($sort as $item){
    if ($item==0) {
        $count++;
    }
}
$cut=count($sort)-$count;
for($i=count($sort); $i>=$cut; $i--) {
    unset($sort[$i]);
    unset($words[$i]);
}
array_multisort($sort,$words);
$words=array_reverse($words);
foreach($words as $word){
    echo $word."<br>";
}

Which outputs:

name
hello
my

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.