我有两个这样的数组: -
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
)
)
我想以这样的方式创建第三个数组$ c $ a [0] [' video_id']永远不会是$ b [0] [video_id'];
为此,我正在编写此代码: -
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//
}
}
我该怎么做?另外,如果$ b可以以所需的方式进行洗牌,那么我可能不需要第三个数组$ c。我怎样才能做到这一点?
答案 0 :(得分:1)
使用shuffle
来重新排列数组。
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;
}
如果数组包含1个元素或为空,该怎么办?你不能以这种方式改变单元素阵列。