0

我有如下指定的数组

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
)

我想要first_name来自第一个数组,基于第二个数组值 => 8、15 和 19
所以我需要Raj,Giri,Mahesh作为逗号分隔字符串的输出。
这个怎么得到..?

4

6 回答 6

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;
于 2017-05-26T11:37:31.707 回答
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];
于 2017-05-26T11:36:05.347 回答
1

在这里,我们使用array_columnandarray_intersect_key来获得所需的输出。

在此处尝试此代码段

$result=array();
$result=  array_column($array, "first_name","id");
$result=array_intersect_key ($result,  array_flip($values));
echo implode(",",$result);
于 2017-05-26T11:38:16.383 回答
0

试试这个:

$names = [];
foreach ($MainArray as $aIndex => $aPayload) // loop over the key and values of the first array
{
    foreach ($searchArray as $b) // loop over your search array (we don't care about the indicies)
    {
        if ($aIndex == $b) // match up the index of the first array with the values of the second array
        {
            $names[] = $b['first_name'];// append the name to the return array
            break; // since we've found the index break out of the inner loop
        }
    }
}
于 2017-05-26T11:36:29.433 回答
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);
?>
于 2017-05-26T12:00:28.570 回答
0

还有其他明智的答案,array_intersect_key()但是array_column(),他们以错误的顺序使用它们。应首先过滤数组,以便array_column()处理较小的数组。此外,因为您的子数组已经有代表id它们内部值的键,所以不需要使用index_key参数 of 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];

方法(演示):

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 读者应该学习和实施的答案。不幸的是,因为它的选票较少并且没有绿色勾号,它很可能会被忽略。
:(

于 2017-05-31T08:12:22.340 回答