我有下面指定的数组
Base*
我有另一个数组,如下所示
Array
(
[5] => Array
(
[id] => 5
[first_name] => Diyaa
[profile_pic] => profile/user5.png
)
[8] => Array
(
[id] => 8
[first_name] => Raj
[profile_pic] => profile/user8.png
)
[12] => Array
(
[id] => 12
[first_name] => Vanathi
[profile_pic] => profile/user12.png
)
[15] => Array
(
[id] => 15
[first_name] => Giri
[profile_pic] => profile/user15.png
)
[19] => Array
(
[id] => 19
[first_name] => Mahesh
[profile_pic] => profile/user19.png
)
)
我希望来自第一个数组的Array
(
[0] => 8
[1] => 15
[2] => 19
)
,基于第二个数组值=> 8,15和19 。
所以我需要 Raj,Giri,Mahesh 作为逗号分隔字符串的输出
怎么弄这个..?
答案 0 :(得分:3)
此代码适用于您: -
$array1 = array_column($array, 'first_name','id');
$array2 = [8,15,19];
$names = array_intersect_key($array1, array_flip($array2));
$names = implode(',',$names);
echo $names;
答案 1 :(得分:1)
试试这个:
$namesArr = [];
foreach ($wantedIds as $wantedId) {
$namesArr[] = $array[$wantedId]['first_name'];
}
$namesStr = implode(',', $namesArr);
echo $namesStr; // Returns 'Raj,Giri,Mahesh'
我将$array
和$wantedIds
定义如下:
$array = [
5 => [
'id' => 5,
'first_name' => 'Diyaa',
'profile_pic' => 'profile/user5.png',
],
8 => [
'id' => 8,
'first_name' => 'Raj',
'profile_pic' => 'profile/user8.png',
],
12 => [
'id' => 12,
'first_name' => 'Vanathi',
'profile_pic' => 'profile/user12.png',
],
15 => [
'id' => 15,
'first_name' => 'Giri',
'profile_pic' => 'profile/user15.png',
],
19 => [
'id' => 19,
'first_name' => 'Mahesh',
'profile_pic' => 'profile/user19.png',
],
];
$wantedIds = [8, 15, 19];
答案 2 :(得分:1)
我们正在使用array_column
和array_intersect_key
来获取所需的输出。
$result=array();
$result= array_column($array, "first_name","id");
$result=array_intersect_key ($result, array_flip($values));
echo implode(",",$result);
答案 3 :(得分:0)
试试这个:
>>> import pandas as pd
>>> import numpy as np
>>> a=pd.DataFrame([[18, 'F'],[50, 'M']],columns = ('Age','Sex'))
>>> print(a)
Age Sex
0 18 F
1 50 M
>>> b=a
>>> print(b)
Age Sex
0 18 F
1 50 M
>>> b.loc[b.index.max() + 1] = [30,'M']
>>> print(b)
Age Sex
0 18 F
1 50 M
2 30 M
>>> print(a)
Age Sex
0 18 F
1 50 M
2 30 M
>>> c=np.array([[18,'F'],[50,'M']])
>>> print(c)
[['18' 'F']
['50' 'M']]
>>> d=c
>>> print(d)
[['18' 'F']
['50' 'M']]
>>> d = np.append(d,[[30,'M']], axis=0)
>>> print(d)
[['18' 'F']
['50' 'M']
['30' 'M']]
>>> print(c)
[['18' 'F']
['50' 'M']]
答案 4 :(得分:0)
<?php
$abc = [
['id' => 5, 'firstname' => 'Diyya', 'profile_pic' => 'profile/user5.png'],
['id' => 8, 'firstname' => 'Raj', 'profile_pic' => 'profile/user8.png'],
['id' => 12, 'firstname' => 'Vanathi', 'profile_pic' => 'profile/user12.png']
];
$d = [8, 5, 12];
$output = [];
foreach ($abc as $user) {
if (in_array($user['id'], $d)) {
$output [] = $user['firstname'];
}
}
echo implode(",", $output);
?>
答案 5 :(得分:0)
使用array_intersect_key()
和array_column()
时,还有其他明智的答案,但是,他们会以错误的顺序使用它们。应首先过滤数组,以便array_column()
处理较小的数组。此外,由于您的子数组已经包含代表其中id
值的键,因此无需使用index_key
array_column()
参数。这将是您可以做出的最简单,最直接的单线:
输入:
$array = [
5 => [
'id' => 5,
'first_name' => 'Diyaa',
'profile_pic' => 'profile/user5.png',
],
8 => [
'id' => 8,
'first_name' => 'Raj',
'profile_pic' => 'profile/user8.png',
],
12 => [
'id' => 12,
'first_name' => 'Vanathi',
'profile_pic' => 'profile/user12.png',
],
15 => [
'id' => 15,
'first_name' => 'Giri',
'profile_pic' => 'profile/user15.png',
],
19 => [
'id' => 19,
'first_name' => 'Mahesh',
'profile_pic' => 'profile/user19.png',
],
];
$search_keys=[8, 15, 19];
方法(Demo):
echo implode(',',array_column(array_intersect_key($array,array_flip($search_keys)),'first_name'));
说明:
$search_keys
array_intersect_key()
过滤掉不需要的子阵列array_column()
创建first_name
值输出:
Raj,Giri,Mahesh
......这是未来SO读者应该学习和实施的答案。不幸的是,由于它的票数较少而且没有佩戴绿色标记,因此可能会被忽略。
:(