我有一个这样的数组:
[1] => Array
(
[id] => 7
[ticket_id] => 12
[client_id] => 174
[thread_name] =>
[added_by] => 2
[created_at] => 2015-07-28 23:24:07
[updated_at] => 2015-07-28 23:24:07
[notes] => Array
(
[0] => Array
(
[id] => 21
[user_id] => 2
[notes] => Not all those who wander are lost!
[notes_attachment] =>
[deleted_at] =>
[created_at] => 2015-07-28 23:34:31
[updated_at] => 2015-07-28 23:34:31
[thread_id] => 7
[users] => Array
(
[0] => Array
(
[user_id] => 1
)
[1] => Array
(
[user_id] => 2
)
)
)
)
)
我需要将 notes 数组中的用户数组操作为类似的内容。
[users] => Array
(
[0] => 1
[1] => 2
)
所以,基本上,我在users数组中需要一个id数组。
我尝试过这样做,但进入用户需要三个循环。
我怎样才能更有效地做到这一点?
注意:这只是多个阵列的结构之一。就像,我每个都有4个线程和多个音符。
此外,我需要操纵现有的数组,而不仅仅是从中获取数据。
我的尝试:
$tmp = array();
foreach($data as $value) {
foreach($value['notes'] as $notes) {
if(!empty($notes['users'])){
foreach($notes['users'] as $users) {
$tmp[] = $users['user_id'];
unset($notes['users']);
$notes['users'] = $tmp;
}
}
}
}
答案 0 :(得分:1)
使用array_column -
$new = array_column($a[notes][0]['users'], 'user_id');
var_dump($new);
<强>输出强>
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
<强>更新强>
$new = array();
foreach($a as $value) {
$new = array_merge($new, array_column($value[notes][0]['users'], 'user_id'));
}
var_dump($new);