Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
How to find the first element with a specific value in multidimansional array?
$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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

<?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.
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!