How to change the results of an array based off another's result

Hi, newbie here.

I’m not too sure what’s the best way to do this, I need to change the one array based off a main list.

For example here is a main list (this can change daily):

array:20 [▼
0 => “10% Offer”
1 => “6 Months”
2 => “Funding”
3 => “6% Offer”
4 => “5% Offer”
5 => “Test”
6 => “20% Offer”
7 => “Part Exchange”
8 => “Testing”
9 => “April Offer”
10 => “Key Workers”
11 => “Help to Buy 1”
12 => “Help to Buy 2”
13 => “Help to Buy 3”
14 => “Assured Sale”
15 => “Help to Sell”
16 => “Flooring Offer”
17 => “Landscaped Gardens”
18 => “June Offers”
19 => “May Offer”
]

In my second array (that I need to change the order) is currently displaying like this:

array:8 [▼
0 => “Key Workers”
1 => “June Offers”
2 => “Test”
3 => “6 Months”
4 => “Part Exchange”
5 => “10% Offer”
6 => “Funding”
7 => “Flooring Offer”
]

This is how the results should be displaying (the key values can stay the same as long as the results are in the right order):

array:7 [▼
0 => “10% Offer”
1 => “6 Months”
2 => “Funding”
3 => “Test”
4 => “Part Exchange”
5 => “Key Workers”
6 => “Flooring Offer”
7 => “June Offers”
]

I was trying a array_multisort but I was getting a ‘Array sizes are inconsistent’ error. I really don’t know what’s the best way to do this. Any advice?

Update - after testing multiple different ways, and feeling like an idiot, I landed up doing this:

$sorted = array_intersect($main_list, $need_to_sort_list);

I’m leaving this up just incase anyone has any better suggestions :slight_smile:

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service