2

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try

$sorted = array();
foreach ( $data as $name ) {
    foreach ( $data2 as $k => $part ) {
        if ($k == $name) {
            $sorted[$k] = $part;
        }
    }
}

print_r($sorted);

See Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.