0

I have two arrays like this:-

Array $a
(
    [0] => Array
        (
            [video_id] => 1
            [video_title] => A1
            [video_type] => 2
            [user_id] => 7
            [video_link] => /challenge_video_1490082018-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490082017-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

    [1] => Array
        (
            [video_id] => 2
            [video_title] => A2
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490082526-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490082526-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

    [2] => Array
        (
            [video_id] => 3
            [video_title] => A3
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490083750-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490083750-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

    [3] => Array
        (
            [video_id] => 4
            [video_title] => A4
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490084089-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490084089-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

    [4] => Array
        (
            [video_id] => 5
            [video_title] => A5
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490084344-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490084344-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

    [5] => Array
        (
            [video_id] => 6
            [video_title] => A6
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490086316-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490086316-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

    [6] => Array
        (
            [video_id] => 7
            [video_title] => A7
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490087497-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490087497-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

)

Array $b
(
    [0] => Array
        (
            [video_id] => 1
            [video_title] => A1
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490082018-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490082017-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

    [1] => Array
        (
            [video_id] => 2
            [video_title] => A2
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490082526-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490082526-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

    [2] => Array
        (
            [video_id] => 3
            [video_title] => A3
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490083750-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490083750-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

    [3] => Array
        (
            [video_id] => 4
            [video_title] => A4
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490084089-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490084089-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

    [4] => Array
        (
            [video_id] => 5
            [video_title] => A5
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490084344-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490084344-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

    [5] => Array
        (
            [video_id] => 6
            [video_title] => A6
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490086316-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490086316-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

    [6] => Array
        (
            [video_id] => 7
            [video_title] => A7
            [video_type] => 2
            [user_id] => 7
            [video_link] => challenge/challenge_video_1490087497-user_uservideo.mov
            [video_image] => challenge/challenge_video_1490087497-Image.png
            [video_like] => 0
            [user_username] => sujan
            [user_image] => 
            [likeCount] => 0
            [fanCount] => 0
            [followCount] => 0
        )

)

I want to create a third array $c in such a way that $a[0]['video_id'] will never be $b[0][video_id'];

For this, I am writing this code:-

for($i = 0; $i < count($a); $i++)
{
     if($a[i]['video_id'] != $b[i]['video_id'])
     {
         $c[] = $b[i];
     }
     else
     {
         // fetch some other row from the rest of the rows//
     }
}

How can I do this? Also, if $b can be shuffled in the desired manner, then I may not need a third array $c. How can I achieve this?

1 Answer 1

1

Use shuffle to shuffle array.

while (compareArrays($a, $b) == true) {
    shuffle($b);
}

$c = $b;

/**
 * return bool - True if equal, false if not.
 */
function compareArrays($a, $b) {
    for($i = 0; $i < count($a); $i++) {
        if($a[i]['video_id'] != $b[i]['video_id']) {
            return false;
        }
    }

    return true;
}

What to do if arrays contains 1 element or is empty? You can't shuffle single-element array this way.

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

1 Comment

actually, the two arrays would be identical. $a will have video in Descending order based on likes. $b will be random. The only condtion is that no video_id in both the list should be at same place.

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.