0

I have made researches and havent fount any solutions for this yet. So final thought is come to Stackoverflow and ask the question.

I have 2 array like below:

BigArray

Array
(
    [0] => Array
        (
            [id] => 1
            [category_name] => Accountancy
            [category_name_vi] => Kế toán
            [category_id] => 1
        )

    [1] => Array
        (
            [id] => 2
            [category_name] => Armed forces
            [category_name_vi] => Quân đội
            [category_id] => 2
        )

    [2] => Array
        (
            [id] => 3
            [category_name] => Admin & Secretarial
            [category_name_vi] => Thư ký & Hành chính
            [category_id] => 3
        )

    [3] => Array
        (
            [id] => 4
            [category_name] => Banking & Finance
            [category_name_vi] => Tài chính & Ngân hàng
            [category_id] => 4
        )
)

and SmallArray:

Array
(
    [0] => Array
        (
            [id] => 7
            [category_id] => 2
            [jobseeker_id] => 1
        )

    [1] => Array
        (
            [id] => 8
            [category_id] => 3
            [jobseeker_id] => 1
        )
)

Ok, now I wanted to match each category_id from SmallArray link with respectively category_name from BigArrayand the output I only need matched values between SmallArray and BigArraywhere category_id of SmallArray is key and category_name of BigArray is value like below:

Matched array:

Array
    (
        [0] => Array
            (
                [2] => Armed forces                    
            )

        [1] => Array
            (
                [3] => Admin & Secretarial
            )
    )

So far, I have tried array_intersect, 2 foreach loops but no luck. Any advise would be very appreciated :(

Thanks

1 Answer 1

1

This should do that:

foreach ($smallArray as $smallKey => $smallElement) {
    foreach ($bigArray as $bigKey => $bigElement) {
        if ($bigElement['id'] == $smallElement['category_id']) {
            $smallArray[$smallKey] = array(
                $bigElement['id'] => $bigElement['category_name'],
            );
            break; // for performance and no extra looping
        }
    }
}

After these loops, you have what you want in $smallArray.

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

2 Comments

btw, these 2 arrays are get directly from 2 tables in database (BigArray and SmallArray table). Do you think I should add extra column in smallArray table name "category_name" and insert category name to speed up performance instead do foreach loops like above ? :)
No, not a good idea :) . They are completely separate concepts. There are other ways to improve performance. If you are reading from database then the best way is join tables (on product.category_id=category.id) when you are querying.

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.