Reputation: 159
This is a tricky one to explain..
Array of categories which are in the required order
Array
(
[0] => products
[1] => installation
[2] => software setup
[3] => aftecare & warranty
[4] => other
)
and another array of questions/answers with their respective category -
Array
(
[installation] => Array
(
[0] => Array
(
[question] => Third FAQ
[answer] => Another sample FAQ question
)
)
[products] => Array
(
[0] => Array
(
[question] => Another FAQ
[answer] => This is a sample FAQ answer.
)
)
)
Then I need to sort this array so that the categories are in the same order as the first array. (ie products before installation)
I've attempted array_multisort() and usort(), multisort throws an array about array lengths being different and usort() requires the cmp function to return a integer, which stumped me somewhat.
Any help gratefully received.
Thank you
Upvotes: 2
Views: 79
Reputation: 95121
You can try
$sorted = array();
foreach ( $data as $name ) {
foreach ( $data2 as $k => $part ) {
if ($k == $name) {
$sorted[$k] = $part;
}
}
}
print_r($sorted);
Upvotes: 0
Reputation: 358
try this:
$flipped_categories = array_flip($categories);
array_merge($flipped_categories, $product_array);
that should give you one array where the first array, in the correct order, now contains the FAQs within each one.
Upvotes: 2