Obtain specific result within an array

1

I want to get the id found within this array :

Array ( 
[count] => 386 
[items] => Array ( 
    [0] => Array ( [id] => 293259195 [first_name] => Ana [last_name] => Nidegger ) 
    [1] => Array ( [id] => 320180374 [first_name] => David [last_name] => Gomariz )
    [2] => Array ( [id] => 200727184 [first_name] => Piet [last_name] => Mondrian ) 
    [3] => Array ( [id] => 112780770 [first_name] => Victor [last_name] => Gini )
    [4] => Array ( [id] => 170239828 [first_name] => Angelo [last_name] => Santana )
    [5] => Array ( [id] => 316175174 [first_name] => Mhelyssa [last_name] => Vazquez ) 
    [6] => Array ( [id] => 185581600 [first_name] => Diego [last_name] => Pascal ) 
    [7] => Array ( [id] => 167767182 [first_name] => Mario [last_name] => Sith ) 
    [8] => Array ( [id] => 170574351 [first_name] => Micromago [last_name] => Micromago )
    [9] => Array ( [id] => 216820978 [first_name] => Benicio [last_name] => Della-Cabra ) 
) ) 

What I would like to get are all id id = 200727184 and store them in a used e variable:

$contenido = "";
foreach($arrayLineas as $id){
$contenido .= $id."\n";
}

But I do not achieve any result I hope you can give me some idea.

    
asked by BotXtrem Solutions 16.10.2018 в 11:40
source

2 answers

2

If $arrayLineas is the complete object

You should do something like this:

$contenido="";
foreach($arrayLineas["items"] as $row){ 
    $contenido .= $row["id"].PHP_EOL; 
}

If $arrayLineas is the array that is in the key items

$contenido="";
foreach($arrayLineas as $row){ 
    $contenido .= $row["id"].PHP_EOL; 
}

The question is that in each row you should look for your key id , which you are not doing.

Note that I preferred PHP_EOL to \n . It is compatible on all platforms.

    
answered by 16.10.2018 / 12:33
source
1

You can use array_column() to get all the data of a column in this case id , basically we pass the Array of the items and indicate the column you want to obtain, this returns an Array with all the elements of the indicated column.

Example

$array = Array ( 
'count' => 386,
'items' => Array ( 
    0 => Array ( 'id' => 293259195, 'first_name' => 'Ana', 'last_name' => 'Nidegger' ),
    1=> Array ( 'id'=> 320180374, 'first_name' => 'David', 'last_name' => 'Gomariz' )
));

$ids = array_column($array ['items'], 'id');
print_r($ids);

Result

  Array ( [0] => 293259195 [1] => 320180374 )

To show the list one after another you can simply make a implode()

echo implode($ids, ', ');
// 293259195, 320180374

Documentation: link

Given the interest of A.Cedano in this answer and the speed between one or the other, I will leave the example used to measure the execution time between his example and mine.

It has been worked with an array of 300 items like the following:

<?php
$array = Array ( 
    'count' => 386,
    'items' => Array()
);
// esta línea se repetira 300 veces para ambos ejemplos
// solo lo pongo una vez para no ensuciar la respuesta
$array['items'][] = Array ( 'id'=> 320180374, 'first_name' => 'David', 'last_name' => 'Gomariz' );

For the foreach example we have used the following script 10 times

$time_start = microtime(true);
foreach($array["items"] as $row){ 
    echo $row["id"].', '; 
}
$time_end = microtime(true);
$t = $time_end - $time_start;
echo '<br>';
echo 'Tiempo: '.number_format($t, 10);

For example of array_column, this example has been executed another 10 times

$time_start = microtime(true);
echo implode(array_column($array['items'], 'id'), ', ');
$time_end = microtime(true);
$t = $time_end - $time_start;
echo '<br>';
echo 'Tiempo: '.number_format($t, 10);

Results:

Resultados foreach
0.0000901222
0.0001010895
0.0001621246
0.0000991821
0.0000610352
0.0001089573
0.0000920296
0.0000650883
0.0001289845
0.0000991821

máximo: 0.0001621246
mínimo: 0.0000610352

Resultados array_column
0.0000419617
0.0000500679
0.0000529289
0.0000610352
0.0000579357
0.0000460148
0.0000600815
0.0000398159
0.0000641346
0.0000619888

máximo: 0.0000641346
mínimo: 0.0000398159
    
answered by 16.10.2018 в 12:32