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

Php 如何根据指定的数组值将数组值转换为逗号分隔的字符串。。?,php,multidimensional-array,filtering,implode,array-column,Php,Multidimensional Array,Filtering,Implode,Array Column,我有如下指定的数组 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 ) [1

我有如下指定的数组

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,从第一个数组中获取
第一个\u名称

所以我需要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];
试试这个:

$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
        }
    }
}
试试这个:

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

此代码适用于您:-

$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\u column
array\u intersect\u key
来获得所需的输出


这里我们使用
array\u column
array\u intersect\u key
来获得所需的输出



还有其他一些答案是明智的,可以使用
array\u intersect\u key()
array\u column()。首先应过滤数组,以便
array\u column()
处理较小的数组。此外,由于子阵列中已经有表示
id
值的键,因此不需要使用
array\u column()
index\u 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];
方法():

说明:

  • 首先从
    $search\u keys
  • 然后使用
    array\u intersect\u key()
    过滤掉不需要的子阵列
  • 然后使用
    array\u column()
  • 然后用逗号将它们粘在一起创建一个字符串
输出:

Raj,Giri,Mahesh

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

使用
array\u intersect\u key()
array\u column()
还有其他答案是明智的,但是,它们的使用顺序是错误的。应该先过滤数组,以便
array\u column()
正在处理一个较小的数组。此外,由于子数组中已经有表示
id
值的键,因此不需要使用
array\u column()
index\u 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];
方法():

说明:

  • 首先从
    $search\u keys
  • 然后使用
    array\u intersect\u key()
    过滤掉不需要的子阵列
  • 然后使用
    array\u column()
  • 然后用逗号将它们粘在一起创建一个字符串
输出:

Raj,Giri,Mahesh
…这是未来的SO读者应该学习和实施的答案。不幸的是,因为它的票数较少,而且没有绿色记号,它可能会被忽略。
:(