如何在smarty中合并具有相同值的数组?

时间:2015-08-07 08:08:12

标签: arrays smarty

我有两个数组,我想在每个数组中使用相同的值进行两次合并..

第一个数组是:

    [0] => Array
        (
            [id] => 1
            [uid] => 0090000157
            [cid] => 0090000007
            [extension] => 202
            [secret] => Myojyo42f!
            [leader] => 1
            [simultaneous] => 
            [confbridge_id] => 2
            [created_at] => 2015-07-26 12:20:20
            [updated_at] => 2015-07-26 12:20:20
        )

    [1] => Array
        (
            [id] => 2
            [uid] => 0090000159
            [cid] => 0090000007
            [extension] => 
            [secret] => Myojyo42f!
            [leader] => 
            [simultaneous] => 
            [confbridge_id] => 2
            [created_at] => 2015-07-26 14:23:41
            [updated_at] => 2015-07-26 14:23:41
        )

)

,第二个数组是:

Array
(
    [0] => Array
        (
            [_id] => 55b52f4c2bab38fc63b6272a
            [event] => ConfbridgeJoin
            [channel] => SIP/peer_voip301confbridge-0000001b
            [uniqueid] => 1437937478.63
            [conference] => 0090000156
            [calleridnum] => 0090000157
            [calleridname] => 0090000157
            [__v] => 0
            [sipSetting] => Array
                (
                    [accountcode] => 
                    [accountcode_naisen] => 202
                    [extentype] => 0
                    [extenrealname] => 
                    [name] => 0090000157
                    [secret] => Myojyo42f!
                    [username] => 0090000157
                    [context] => innercall_xdigit
                    [gid] => 101
                    [cid] => 0090000007
                )

        )

    [1] => Array
        (
            [_id] => 55b53a2e2bab38fc63b6272b
            [event] => ConfbridgeJoin
            [channel] => SIP/peer_voip301confbridge-0000001c
            [uniqueid] => 1437940260.66
            [conference] => 0090000156
            [calleridnum] => 0090000158
            [calleridname] => UID2
            [__v] => 0
            [sipSetting] => Array
                (
                    [accountcode] => 
                    [accountcode_naisen] => 203
                    [extentype] => 0
                    [extenrealname] => 
                    [name] => 0090000158
                    [secret] => Myojyo42f!
                    [username] => 0090000158
                    [context] => innercall_xdigit
                    [gid] => 101
                    [cid] => 0090000007
                )

        )

)

我想合并具有相同值的数组,例如:

第一个数组有= [uid] => 0090000157 第二个数组= [calleridnum] => 0090000157

是否可以合并它们?

enter image description here

这是我的代码

{foreach from=$participants item=participant key=p }
  {foreach from=$conference_participants item=conference_participant key=c}
            {if$participants.calleridnum == $conference_participants.uid}
              //how to get data here   ?    

            {/if}
  {/foreach}
{/foreach}

1 个答案:

答案 0 :(得分:1)

这可能是你想要的吗?

对不起所有的更改,我应该测试一下...... grr

(我把它放在PHPFiddle中:

<pre>
<?php
$participants = [
    [   'calleridnum' => 1,
        'test' => 'yay' 
    ]
];
$conferance_participants = [
    [   'uid' => 1,
        'test' => 'yay2',
        'dit' => 'deze'
    ]
];

foreach ($participants as $participant=>$p) {
    foreach ($conferance_participants as $conferance_participant=>$c) {

        if ($p['calleridnum'] == $c['uid']) {

            // ID's match do the magic here
            foreach ( $c as $key=>$val ) {
                if (!isset($p[$key])) {
                    // Value is new, copy the conferance_participant to the participant
                    $participants[$participant][$key] = $val;
                }
            } // Merging data

        } // If: Match

    } 
}

print_r( $participants );
?>