2017-05-26 55 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讀者應該學習和貫徹。不幸的是,由於它的投票少,不穿綠色的勾號,它可能會被忽略。
:(

相關問題