Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

231
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda