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がコンマ区切り文字列として出力されている必要があります。
これを取得する方法..?

答えて

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

このお試しください:私は以下のように$array$wantedIdsを定義し

$namesArr = []; 
foreach ($wantedIds as $wantedId) { 
    $namesArr[] = $array[$wantedId]['first_name']; 
} 
$namesStr = implode(',', $namesArr); 

echo $namesStr; // Returns 'Raj,Giri,Mahesh' 

を:

$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()がより小さい配列を処理するように、最初に配列をフィルタリングする必要があります。また、サブアレイにはすでにid値を表すキーが含まれているため、index_keyパラメータを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]; 

方法(Demo):

echo implode(',',array_column(array_intersect_key($array,array_flip($search_keys)),'first_name')); 

説明:

  • 最初にキー/値を反転して$search_keys
  • そして、次に文字列を作成するために、次にカンマでそれらを一緒に接着first_name
  • の新しい配列を作成するためにarray_column()を使用し、不要なサブアレイ
  • をフィルタリングするためにarray_intersect_key()を使用しています。

出力:

Raj,Giri,Mahesh 

...これは、読者から学び、実行する必要がありますので、今後のことを答えです。残念なことに、それは投票が少なく、緑のダニを着用しないので、おそらく無視されるでしょう。
:(

関連する問題