简体   繁体   English

我如何在不使用数组的情况下获取foreach之外的值?

[英]How can i get the value outside foreach without using array?

I want to get calleridnum without using array . 我想在不使用array的情况下获取calleridnum Is this possible? 这可能吗? or is there other way to do this ? 还是有其他方法可以做到这一点?

I have this code : 我有这个代码:

$participants = [
    [   'calleridnum' => 1,
        'test' => 'yay' 
    ],
    [   'calleridnum' => 2,
        'test' => 'yay' 
    ],
     [   'calleridnum' => 3,
        'test' => 'yay' 
    ]
];
$conferance_participants = [
    [   'uid' => 1,
        'test' => 'yay2',
        'dit' => 'deze'
    ],
    [  'uid' => 2,
        'test' => 'test',
        'dit' => 'wew'
    ]
];

foreach ($participants as $key=>$p) {
    foreach ($conferance_participants as $key=>$cp) {

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

        } 

    } 
}

print_r( $calleridnum );

My output is: 我的输出是:

Array ( [0] => 1 [1] => 2 )

but I want the output to be like this 但我希望输出像这样

1,2

Try this: Implode your array to be converted into string. 尝试以下操作: Implode要转换为字符串的数组。

  $calleridnum= [];

  foreach ($participants as $key=>$p) {
    foreach ($conferance_participants as $key=>$cp) {

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

     } 

    } 
 }

$result = implode(',', $calleridnum);
echo $result;

foreach loop外使用implode()

 echo $str = implode (",", $calleridnum);

I'll simply use array_intersect , array_column along with implode as 我会简单地使用array_intersectarray_column连同implode

echo implode(',', array_intersect(array_column($conferance_participants,'uid'), array_column($participants,'calleridnum')));

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM