I currently have an associative array of values that I need sorted based on values in another array.
Example:
If I have an array of values,
AssocArray = ‘122’=>’12’, ‘123’=>’32’, ‘232’=>’54’, ‘343’=>’12’
I need to check it against another array of values,
orderArray = ‘232’, ‘123’
if it exists then push the value to the top of the AssocArray. so the final array looks like this.
AssocArray = ‘232’=>’54’, ‘123’=>’32’, ‘122’=>’12’, ‘343’=>’12’
Sorry I do not have any working code, I am still learning PHP.
Any help would be really grateful 🙂 Thank you.
Loop through your
$orderArray, and check if an element with that key is present in$AssocArray. If it is, add that element to the$resultarray, and remove it from the original$AssocArray. Then, you just have to merge the remaining elements in$AssocArraywith whatever was pushed to the top of the$resultsarray:You can see that this prints: