0

I have the array as specified below

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 have another array as given below

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

I want the first_name from the first array, based on second array values => 8, 15 and 19.
So I need Raj,Giri,Mahesh as output as comma separated string.
How to get this..?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
ArunValaven
  • 1,753
  • 2
  • 25
  • 44

6 Answers6

3

This code will work for you :-

$array1 = array_column($array, 'first_name','id');
$array2 = [8,15,19];
$names = array_intersect_key($array1, array_flip($array2));
$names = implode(',',$names);
echo $names;
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
1

Try this:

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

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

I defined $array and $wantedIds as below:

$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];
Ethan
  • 4,295
  • 4
  • 25
  • 44
1

Here we are using array_column and array_intersect_key to obtain desired output.

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);
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
0

Try this out:

$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
        }
    }
}
DragonZero
  • 810
  • 5
  • 8
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);
?>
Omeiza
  • 111
  • 1
  • 5
0

There are other answers that are wise to use array_intersect_key() and array_column() however, they use them in the wrong order. The array should be filtered first, so that array_column() is processing a smaller array. Also, because your subarrays already have keys that represent the id value inside of them, there is no need to use the index_key parameter of array_column(). This is going to be the simplest, most direct one-liner you can make:

Inputs:

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

Method (Demo):

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

Explanation:

  • First flip the keys/values from $search_keys
  • Then use array_intersect_key() to filter out the unwanted subarrays
  • Then use array_column() to create a new array of first_name values
  • Then glue them together with commas to create a string.

Output:

Raj,Giri,Mahesh

...This is the answer that future SO readers should be learning from and implementing. Unfortunately, because it has less votes and doesn't wear the green tick, it will likely get ignored.
:(

mickmackusa
  • 43,625
  • 12
  • 83
  • 136