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

时间:2022-07-24 11:11:07

I want to get calleridnum without using array . Is this possible? or is there other way to do this ?

我想不使用数组就得到calleridnum。这是可能的吗?或者还有别的方法吗?

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

3 个解决方案

#1


0  

Use implode() outside of foreach loop

在foreach循环外部使用内爆()

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

#2


0  

Try this: Implode your array to be converted into string.

尝试以下操作:内爆数组以转换为字符串。

  $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;

#3


0  

I'll simply use array_intersect,array_column along with implode as

我将使用array_intersect、array_column以及内爆as

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

Demo

演示

#1


0  

Use implode() outside of foreach loop

在foreach循环外部使用内爆()

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

#2


0  

Try this: Implode your array to be converted into string.

尝试以下操作:内爆数组以转换为字符串。

  $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;

#3


0  

I'll simply use array_intersect,array_column along with implode as

我将使用array_intersect、array_column以及内爆as

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

Demo

演示