Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda