2017-05-26 106 views
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 
) 
) 

作为指定的I具有另一阵列下面

Array 
(
    [0] => 8 
    [1] => 15 
    [2] => 19 
) 

我想从第一阵列first_name给出,基于第二阵列值=>8,15和19
所以我需要Raj,Giri,Mahesh作为逗号分隔的字符串输出。
如何得到这.. ..?

回答

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

试试这个:

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

这里我们使用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); 
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); 
?> 
0

还有其他的答案是明智的使用array_intersect_key()array_column()然而,他们用他们在错误的顺序。数组应该先被过滤,以便array_column()正在处理更小的数组。此外,由于您的子阵列已经有代表其内部值的密钥,因此不需要使用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
  • 然后使用array_intersect_key()过滤掉不想要的子阵列
  • 然后使用array_column()创建first_name
  • 的新数组然后用逗号一起胶水他们创建一个字符串。

输出:

Raj,Giri,Mahesh 

...这就是答案,未来的SO读者应该学习和贯彻。不幸的是,由于它的投票少,不穿绿色的勾号,它可能会被忽略。
:(

相关问题