tmartin314
tmartin314

Reputation: 4171

Create a flat associative array from two columns of a 2d array and filter out blacklisted values

I am using the following foreach loop to populate a new array with filtered data.

foreach ($aMbs as $aMemb) {
    $ignoreArray = array(1, 3);
    if (!in_array($aMemb['ID'], $ignoreArray)) { 
        $aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);
    }
}

The problem is that it is producing a 2-dimensional array, but I want to have a flat, associative array.

If $aMbs had the following data:

[
    ['ID' => 1, 'Name' => 'Standard'],
    ['ID' => 2, 'Name' => 'Silver'],
    ['ID' => 3, 'Name' => 'Gold'],
    ['ID' => 4, 'Name' => 'Platinum'],
    ['ID' => 5, 'Name' => 'Unobtainium'],
]

The desired output would be:

[
    2 => 'Silver',
    4 => 'Platinum',
    5 => 'Unobtainium',
]

How can I achieve this?

Upvotes: 28

Views: 109300

Answers (6)

mickmackusa
mickmackusa

Reputation: 47894

Instead of performing a filtering process on each iteration of your loop, you can create a new associative array with array_column(), then filter out the blacklisted keys with array_diff_key() after flipping the blacklist. The advantage in doing so is that the process will always only take 3 function calls instead of 1 + n function calls.

Code: (Demo)

$aMbs = [
    ['ID' => 1, 'Name' => 'Standard'],
    ['ID' => 2, 'Name' => 'Silver'],
    ['ID' => 3, 'Name' => 'Gold'],
    ['ID' => 4, 'Name' => 'Platinum'],
    ['ID' => 5, 'Name' => 'Unobtainium'],
];

$ignoreArray = [1, 3];

var_export(
    array_diff_key(
        array_column($aMbs, 'Name', 'ID'),
        array_flip($ignoreArray)
    )
);

Output:

array (
  2 => 'Silver',
  4 => 'Platinum',
  5 => 'Unobtainium',
)

If you'd rather keep the foreach() loop, then the earlier answers are perfectly fine. Here's a foreach() style using array destructuring. The advantage of this syntax (if you have additional irrelevant columns), is that you don't need to access all data in each row and you set up convenient temporary variables. Demo

$ignoreArray = [1, 3];
$aMemberships = [];
foreach ($aMbs as ['ID' => $id, 'Name' => $name]) {
    if (!in_array($id, $ignoreArray)) {
        $aMemberships[$id] = $name;
    }
}
var_export($aMemberships);
// same output as the previous snippet

Upvotes: 0

Gufran Hasan
Gufran Hasan

Reputation: 9373

You get key and value of an associative array in foreach loop and create an associative with key and value pairs.

$aMemberships=array();//define array
foreach($aMbs as $key=>$value){
    $ignoreArray = array(1,3);
    if (!in_array($key,$ignoreArray)){ 
        $aMemberships[$key] = $value;
    }
}

It will give you an expected output:

array('1' => 'Standard', '2' => 'Silver');

Upvotes: 0

GWW
GWW

Reputation: 44093

You need to change your $aMemberships assignment

$aMemberships[] = $aMemb['Name']; 

If you want an array

$aMemberships[$aMemb['ID']] = $aMemb['Name'];

if you want a map.

What you are doing is appending an array to an array.

Upvotes: 50

ShivarajRH
ShivarajRH

Reputation: 940

Associative array in foreach statement:

foreach($nodeids as $field => $value) {

  $field_data[$field]=$value;

}

Output:

Array(
$field => $value,
$field => $value
...
);

insertion in CodeIgniter:

$res=$this->db->insert($bundle_table,$field_data);

Upvotes: 23

codaddict
codaddict

Reputation: 455020

Your existing code uses incremental key and uses the array as corresponding value. To make make $aMemberships an associative array with key as $aMemb['ID'] and value being $aMemb['Name'] you need to change

    $aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);

in the foreach loop to:

    $aMemberships[$aMemb['ID']] = $aMemb['Name']);

Upvotes: 4

Tim Gautier
Tim Gautier

Reputation: 30112

Instead of

$aMemberships[] = array($aMemb['ID'] => $aMemb['Name']);

Try

$aMemberships[$aMemb['ID']] = $aMemb['Name'];

Upvotes: 10

Related Questions