我意识到之前已经问过这个问题,但我似乎无法工作。
我有这个多维数组
foreach ( $friendsid as $row )
{
$sql1= "SELECT friend_user_id FROM wp_bp_friends WHERE initiator_user_id='".$row->friend_user_id."' AND is_confirmed = 1";
$ffid = $wpdb->get_results($sql1);
// $recur_multi_dimen_arr_obj = new RecursiveArrayIterator($ffid);
// $recur_flat_arr_obj = new RecursiveIteratorIterator($recur_multi_dimen_arr_obj);
// $flat_arr = iterator_to_array($recur_flat_arr_obj, false);
// print_r($flat_arr);
print_r($ffid);
}
输出以下内容
Array(
[0] => stdClassObject([friend_user_id] => 62) [1] => stdClassObject([friend_user_id] => 51) [2] => stdClassObject([friend_user_id] => 60) [3] => stdClassObject([friend_user_id] => 65) [4] => stdClassObject([friend_user_id] => 56)
) Array(
[0] => stdClassObject([friend_user_id] => 43) [1] => stdClassObject([friend_user_id] => 50) [2] => stdClassObject([friend_user_id] => 64) [3] => stdClassObject([friend_user_id] => 45) [4] => stdClassObject([friend_user_id] => 44)
) Array(
[0] => stdClassObject([friend_user_id] => 57)
)
使用
$resultarray =array();
foreach($ffid as $oneitem){
$resultarray[]=$oneitem->friend_user_id;
}
$excluded_user =implode(", ",$resultarray);
我得到了
$excluded_user = 57;
我最终需要的是
$excluded_user = 62,51,60,....,44,57;
在使用foreach循环之前,如何在$ ffid中组合数组?
请注意,$ ffid中的数组数量可能会有所不同
提前感谢您提供任何帮助
答案 0 :(得分:1)
您可以使用array_merge函数创建包含所有数据的数组。
这样的事情:
$arrFfid = array();
foreach ( $friendsid as $row )
{
$sql1= "SELECT friend_user_id FROM wp_bp_friends WHERE initiator_user_id='".$row->friend_user_id."' AND is_confirmed = 1";
$ffid = $wpdb->get_results($sql1);
$arrFfid = array_merge($arrFfid, $ffid);
}
答案 1 :(得分:1)
使用此代码: 像这样的事情
$resultarray =array();
foreach ( $friendsid as $row )
{
$sql1= "SELECT friend_user_id FROM wp_bp_friends WHERE initiator_user_id='".$row->friend_user_id."' AND is_confirmed = 1";
$ffid = $wpdb->get_results($sql1);
foreach($ffid as $oneitem){
$resultarray[] =$oneitem->friend_user_id;
}
}
$excluded_user =implode(", ",$resultarray);
答案 2 :(得分:1)
函数array_map
和array_walk_recursive
可以解决问题。下面是一个独立的代码示例:
function flatten ( $item, $key, &$trg ) {
array_push($trg, $item);
}
function hoist ( $item ) {
return $item->friend_user_id;
}
$basearray = array(
array(
(Object)array("friend_user_id" => 62)
, (Object)array("friend_user_id" => 9)
, (Object)array("friend_user_id" => 42)
)
, array(
(Object)array("friend_user_id" => 1)
, (Object)array("friend_user_id" => 2)
, (Object)array("friend_user_id" => 3)
)
, array(
(Object)array("friend_user_id" => 99)
, (Object)array("friend_user_id" => 9)
)
);
$resultarray = array();
array_walk_recursive ( $basearray, 'flatten', &$resultarray );
print_r($resultarray);
$resultarray = array_map ( 'hoist', $resultarray );
print_r($resultarray);
$excluded_user = implode(", ",$resultarray);
print_r("excluded_user = " . $excluded_user . "\n");
答案 3 :(得分:1)
array_map和array_merge似乎是这个
的不错选择$result = array();
foreach ( $friendsid as $row ) {
...
...
$r = array_map(function($item) {
return $item->friend_user_id;
}, $ffid);
$result = array_merge($result,$r);
}
$excluded_user = implode(", ", array_unique($result));