如何合并两个数组?

发布于 2024-11-19 10:30:46 字数 4279 浏览 0 评论 0原文

我有这个查询,通过我得到的结果

$query_message1 = "select a.new,a.mes_id,a.frm_id,a.mem_id,a.subject,a.body,a.date,a.read from messages_system as a where a.frm_id='$memId' and a.type='message' order by date desc limit $lowerlimit,$limit";
             $exeMessage=execute_query($query_message1,true,"select");
$message =$exeMessage;
print_r($message);

foreach($exeMessage as $kk=>$mem){

             $query_message2 = "select b.profilenam,b.photo_thumb,b.gender,b.profile_type from members as b where b.mem_id='".$mem['mem_id']."'";
            $exeMessage1 = execute_query($query_message2,true,"select");
           //print_r($exeMessage);
            if (empty($exeMessage1)) {
            echo "0";
            $img = "images/my-profile-img3-big.gif";
              // print_r($message['mes_id']);
                    $exeMessage1['profilenam']='Annonymous';
                    $exeMessage1['photo_thumb']=$img;
                    $exeMessage1['gender']='m';
                    $exeMessage1['profile_type']='';
                    $message[$kk] = $exeMessage1;

            }

            }

很好 print_r($message); line 给了我

(
    [0] => Array
        (
            [new] => new
            [mes_id] => 1030
            [frm_id] => 329
            [mem_id] => 39
            [subject] =>  aaaa
            [body] => aaaa
            [date] => 1310285289
            [read] => read
        )

    [1] => Array
        (
            [new] => viewed
            [mes_id] => 1029
            [frm_id] => 329
            [mem_id] => 397
            [subject] =>  aaaa
            [body] => aaaa
            [date] => 1310285188
            [read] => read
        )
 )

和 line $message[$kk] = array_merge($exeMessage1,$message[$kk]); 给我条件 if (empty($exeMessage1)) { 的结果,

现在我想合并这两个数组,以便最终结果像这样

(
    [0] => Array
        (
            [new] => new
            [mes_id] => 1030
            [frm_id] => 329
            [mem_id] => 39
            [subject] =>  aaaa
            [body] => aaaa
            [date] => 1310285289
            [read] => read
            [profilenam] => Annonymous
            [photo_thumb] => images/my-profile-img3-big.gif
            [gender] => m
            [profile_type] => 



        )

    [1] => Array
        (
            [new] => viewed
            [mes_id] => 1029
            [frm_id] => 329
            [mem_id] => 397
            [subject] =>  aaaa
            [body] => aaaa
            [date] => 1310285188
            [read] => read
            [profilenam] => Purple Test
            [photo_thumb] => photos/19db7b14c0ebdb98838fdafb46b3a1ebth.jpeg
            [gender] => n
            [profile_type] => C
        )
)

PLease Help 谢谢

这里是解决方案

$query_message1 = "select a.new,a.mes_id,a.frm_id,a.mem_id,a.subject,a.body,a.date,a.read from messages_system as a where a.frm_id='$memId' and a.type='message' order by date desc limit $lowerlimit,$limit";
            $exeMessage = execute_query($query_message1, true, "select");
            $totalResult = "select a.new,a.mes_id,a.frm_id,a.mem_id,a.subject,a.body,a.date,a.read from messages_system as a where a.frm_id='$memId' and a.type='message' order by date desc";
            $exetotalResult = execute_query($totalResult, true, "select");

            foreach ($exeMessage as $kk => $mem) {

                $query_message2 = "select b.profilenam,b.photo_thumb,b.gender,b.profile_type from members as b where b.mem_id='" . $mem['mem_id'] . "'";
                $exeMessage1 = execute_query($query_message2, true, "select");

                if (empty($exeMessage1)) {
                    $img = "images/my-profile-img3-big.gif";
                    $exeMessage1[$kk]['profilenam'] = 'Annonymous';
                    $exeMessage1[$kk]['photo_thumb'] = $img;
                    $exeMessage1[$kk]['gender'] = 'm';
                    $exeMessage1[$kk]['profile_type'] = '';
                }
                $message[$kk] = array_merge((array) $mem, (array) $exeMessage1[0]);
            }

I have this query through i am getting reult

$query_message1 = "select a.new,a.mes_id,a.frm_id,a.mem_id,a.subject,a.body,a.date,a.read from messages_system as a where a.frm_id='$memId' and a.type='message' order by date desc limit $lowerlimit,$limit";
             $exeMessage=execute_query($query_message1,true,"select");
$message =$exeMessage;
print_r($message);

foreach($exeMessage as $kk=>$mem){

             $query_message2 = "select b.profilenam,b.photo_thumb,b.gender,b.profile_type from members as b where b.mem_id='".$mem['mem_id']."'";
            $exeMessage1 = execute_query($query_message2,true,"select");
           //print_r($exeMessage);
            if (empty($exeMessage1)) {
            echo "0";
            $img = "images/my-profile-img3-big.gif";
              // print_r($message['mes_id']);
                    $exeMessage1['profilenam']='Annonymous';
                    $exeMessage1['photo_thumb']=$img;
                    $exeMessage1['gender']='m';
                    $exeMessage1['profile_type']='';
                    $message[$kk] = $exeMessage1;

            }

            }

well print_r($message); line gives me

(
    [0] => Array
        (
            [new] => new
            [mes_id] => 1030
            [frm_id] => 329
            [mem_id] => 39
            [subject] =>  aaaa
            [body] => aaaa
            [date] => 1310285289
            [read] => read
        )

    [1] => Array
        (
            [new] => viewed
            [mes_id] => 1029
            [frm_id] => 329
            [mem_id] => 397
            [subject] =>  aaaa
            [body] => aaaa
            [date] => 1310285188
            [read] => read
        )
 )

and line $message[$kk] = array_merge($exeMessage1,$message[$kk]);
gives me result of condition if (empty($exeMessage1)) {

now i want to merge these two arrays so that the final result will be like this

(
    [0] => Array
        (
            [new] => new
            [mes_id] => 1030
            [frm_id] => 329
            [mem_id] => 39
            [subject] =>  aaaa
            [body] => aaaa
            [date] => 1310285289
            [read] => read
            [profilenam] => Annonymous
            [photo_thumb] => images/my-profile-img3-big.gif
            [gender] => m
            [profile_type] => 



        )

    [1] => Array
        (
            [new] => viewed
            [mes_id] => 1029
            [frm_id] => 329
            [mem_id] => 397
            [subject] =>  aaaa
            [body] => aaaa
            [date] => 1310285188
            [read] => read
            [profilenam] => Purple Test
            [photo_thumb] => photos/19db7b14c0ebdb98838fdafb46b3a1ebth.jpeg
            [gender] => n
            [profile_type] => C
        )
)

PLease Help
Thanks

here is the solution

$query_message1 = "select a.new,a.mes_id,a.frm_id,a.mem_id,a.subject,a.body,a.date,a.read from messages_system as a where a.frm_id='$memId' and a.type='message' order by date desc limit $lowerlimit,$limit";
            $exeMessage = execute_query($query_message1, true, "select");
            $totalResult = "select a.new,a.mes_id,a.frm_id,a.mem_id,a.subject,a.body,a.date,a.read from messages_system as a where a.frm_id='$memId' and a.type='message' order by date desc";
            $exetotalResult = execute_query($totalResult, true, "select");

            foreach ($exeMessage as $kk => $mem) {

                $query_message2 = "select b.profilenam,b.photo_thumb,b.gender,b.profile_type from members as b where b.mem_id='" . $mem['mem_id'] . "'";
                $exeMessage1 = execute_query($query_message2, true, "select");

                if (empty($exeMessage1)) {
                    $img = "images/my-profile-img3-big.gif";
                    $exeMessage1[$kk]['profilenam'] = 'Annonymous';
                    $exeMessage1[$kk]['photo_thumb'] = $img;
                    $exeMessage1[$kk]['gender'] = 'm';
                    $exeMessage1[$kk]['profile_type'] = '';
                }
                $message[$kk] = array_merge((array) $mem, (array) $exeMessage1[0]);
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2024-11-26 10:30:46

您可以简单地使用array_merge()函数。

you can simply use the array_merge() function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文