I have question like in the title. I tried to figure it out by myself and searching in the internet but with no result ;/
I have 4 arrays:
$sortingArr = array(
0 => 'EURUSD',
1 => 'USDGBP'
);
$arr1 = array('name' => 'EURUSD');
$arr2 = array('name' => 'USDPLN');
$arr3 = array('name' => 'USDGBP');
and i merged those named arr1, arr2, arr3 as seen below:
$data = array_merge_recursive($arr1, $arr2, $arr3);
How to sort values from $data like 'EURUSD'... by values from $sortingArr.
I hope I explained this clearly and pls be understanding coz its my first post here:)
Below is the function to sort an array by values of another array
<?php
//sorting array
$sortingArr = array(
0 => 'EURUSD',
1 => 'USDGBP',
);
$arr1 = array('name' => 'EURUSD');
$arr2 = array('name' => 'USDPLN');
$arr3 = array('name' => 'USDGBP');
// to be sorted array
$data = array_merge_recursive($arr1, $arr2, $arr3);
// calling a custom function
$new_data = sortArrayByArrayValue($data['name'],$sortingArr);
print_r($new_data); // print results
function sortArrayByArrayValue($array, $orderArray) {
$ordered = array();
// loop the array
foreach ($orderArray as $key=>$value) {
// check the value is in the $array
if (($k = array_search($value, $array)) !== false) {
// assign thet value to new array
$ordered[$k] = $array[$k];
// unset the value
unset($array[$k]);
}
}
return $ordered+ $array;
}
Output:
Array
(
[0] => EURUSD
[2] => USDGBP
[1] => USDPLN
)