如何根据指定的给定数组值将数组值转换为逗号分隔的字符串..?

How to convert array values to comma seperated string based on specified given array values..?

我有下面指定的数组

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 作为逗号分隔字符串的输出。
如何获得这个..?

试试这个:

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

试试这个:

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

此代码适合您:-

$array1 = array_column($array, 'first_name','id');
$array2 = [8,15,19];
$names = array_intersect_key($array1, array_flip($array2));
$names = implode(',',$names);
echo $names;

这里我们使用 array_columnarray_intersect_key 来获得所需的输出。

Try this code snippet here

$result=array();
$result=  array_column($array, "first_name","id");
$result=array_intersect_key ($result,  array_flip($values));
echo implode(",",$result);
<?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);
?>

还有其他答案使用 array_intersect_key()array_column() 是明智的,但是,他们以错误的顺序使用它们。应首先过滤数组,以便 array_column() 处理较小的数组。此外,因为您的子数组中已经有代表 id 值的键,所以不需要使用 array_column()index_key 参数。这将是您可以制作的最简单、最直接的一行:

输入:

$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
  • 翻转keys/values
  • 然后用array_intersect_key()过滤掉不需要的子数组
  • 然后使用array_column()创建一个包含first_name个值的新数组
  • 然后用逗号将它们粘在一起以创建一个字符串。

输出:

Raj,Giri,Mahesh

...这是未来 SO 读者应该学习和实施的答案。不幸的是,因为它的选票较少并且没有绿色勾号,所以它很可能会被忽略。
:(