Home > Back-end >  How to find the first element with a specific value in multidimansional array?
How to find the first element with a specific value in multidimansional array?

Time:11-17

$userarray = array(
    array(
        'uid' => '100',
        'extraid' => 2,
        'name' => 'Sandra Shush',
        'pic_square' => 'urlof100'
    ),
    array(
        'uid' => '5465',
        'extraid' => 2,
        'name' => 'Stefanie Mcmohn',
        'pic_square' => 'urlof100'
    ),
    array(
        'uid' => '40489',
        'extraid' => 2,
        'name' => 'Michael',
        'pic_square' => 'urlof40489'
    ),
    array(
        'uid' => '512',
        'extraid' => 3,
        'name' => 'Hillary',
        'pic_square' => 'urlof409'
    ),
    array(
        'uid' => '792',
        'extraid' => 3,
        'name' => 'James',
        'pic_square' => 'urlof489'
    ),
);

$all_category = $this->common->getAll(TABLE_CONF_CATEGORIES, 'year', $year);
foreach($all_category as $cats) {
                    $key = array_search($cats->id, array_column($userarray , 'extraid'));echo $key;
                    if($key) {
                        $userarray[$key]->category_name = $cats->category_name;
                    }
}

In this array, I need to get every first element of extraid. i.e. if extraid = 2, here's 3 elements are there, so I need to get the first array. If extraid = 3, then there's 2 arrays are there, & I need the first array to fetched, & so on.

this all_category is another array where the corresponding extraid values are present, so looped it, & did an array search to find the value.

CodePudding user response:

<?php
$userarray = [
    [
        'uid' => '100',
        'extraid' => 2,
        'name' => 'Sandra Shush',
        'pic_square' => 'urlof100',
    ],
    [
        'uid' => '5465',
        'extraid' => 2,
        'name' => 'Stefanie Mcmohn',
        'pic_square' => 'urlof100',
    ],
    [
        'uid' => '40489',
        'extraid' => 2,
        'name' => 'Michael',
        'pic_square' => 'urlof40489',
    ],
    [
        'uid' => '512',
        'extraid' => 3,
        'name' => 'Hillary',
        'pic_square' => 'urlof409',
    ],
    [
        'uid' => '792',
        'extraid' => 3,
        'name' => 'James',
        'pic_square' => 'urlof489',
    ],
];

// final output array
$all_category = [];

// list of already set ids
$ids = [];

foreach($userarray as $user) {
    if( !isset($ids[$user['extraid']]) ){
        $ids[$user['extraid']] = true;
        $all_category[]= $user;
    }
}


print_r($all_category);
  1. Loop through your array
  2. Check if the extraid is already in your array
  3. If it is then move to the next array element
  4. If it is not, then add it to your final output array, and store the id in our list of ids we've already seen.

CodePudding user response:

Solution for the case that several fields should be unique. Unique here means that the content of all fields must match. The input array itself is reduced.

$fieldNames = ['extraid'];

$filterCols = [];
$flipFields = array_flip($fieldNames);

foreach($userarray as $key => $row){
  $fieldsFromRow = array_intersect_key($row,$flipFields);
  if(in_array($fieldsFromRow, $filterCols)) {
    unset($userarray[$key]);
  }
  else {
    $filterCols[] = $fieldsFromRow;
  }
}

$userarray = array_values($userarray);

The code comes from the TableArray class. With this class the solution looks like this:

$all_category = TableArray::create($userarray)
  ->filterUnique(['extraid'])
  ->fetchAll()
;

When using the class, the input array is retained.

  • Related