Home > Net >  return first level key from a second level array value without looping through the array
return first level key from a second level array value without looping through the array

Time:01-13

I have the following array:

$entrate=Array
(
    0 => Array(
            'id' => 1,
            'nome' => 'Stipendio',
            'datamov' => '2023-04-15',
            'mese' => 4,
            'anno' => 2023,
            'total_mov' => 3000.00
        ),

    1 => Array(
            'id' => 1,
            'nome' => 'Stipendio',
            'datamov' => '2023-03-15',
            'mese' => 3,
            'anno' => 2023,
            'total_mov' => 3000.00
        ),

    2 => Array(
            'id' => 1,
            'nome' => 'Stipendio',
            'datamov' => '2023-02-15',
            'mese' => 2,
            'anno' => 2023,
            'total_mov' => 3000.00
        ),

    3 => Array(
            'id' => 1,
            'nome' => 'Stipendio',
            'datamov' => '2023-01-15',
            'mese' => 1,
            'anno' => 2023,
            'total_mov' => 3000.00
        )
);

and I want to search by datamov value returning the key of the first level array. So looking for '2023-03-15' I want to have back 1 as result.

I tried to do this way:

$anno_start='2023';
$mese_start='01';
$end='2023-12-31';
$datemovs=array_column($entrata, 'datamov');
while (strtotime($anno_start . '-' . $mese_start . '-01') < strtotime($end)) {
    $mese_start_num = ltrim($mese_start, '0');
    $searched_date=$anno_start.'-'.$mese_start.'-15';
    echo 'looking for '.$searched_date;
    if($found_key = array_search($searched_date, $datemovs)!==NULL){
    echo ' found this key '.$found_key;
        $datatrovata = $entrata[$found_key]['datamov'];
        echo " that matches $datatrovata \n";
    }else{
        echo " and I found nothing";

    }
    $mese_start_num = $mese_start_num   1;
    if ($mese_start_num > 12) {
        $anno_start = $anno_start   1;
        $mese_start_num = 1;
        $mese_start='01';
    }
    if($mese_start_num<10){$mese_start='0'.$mese_start_num;}else{$mese_start=$mese_start_num;}
}

but it gives me:

looking for 2023-01-15 found this key 1 that matches 2023-03-15

looking for 2023-02-15 found this key 1 that matches 2023-03-15

looking for 2023-03-15 found this key 1 that matches 2023-03-15

looking for 2023-04-15 found this key 1 that matches 2023-03-15

looking for 2023-05-15 found this key 1 that matches 2023-03-15

looking for 2023-06-15 found this key 1 that matches 2023-03-15

looking for 2023-07-15 found this key 1 that matches 2023-03-15

looking for 2023-08-15 found this key 1 that matches 2023-03-15

looking for 2023-09-15 found this key 1 that matches 2023-03-15

looking for 2023-10-15 found this key 1 that matches 2023-03-15

looking for 2023-11-15 found this key 1 that matches 2023-03-15

looking for 2023-12-15 found this key 1 that matches 2023-03-15

that is obviously wrong. What am I missing?

My goal is to end up with

looking for 2023-01-15 found this key 3 that matches 2023-01-15 and total_mov is 3000

looking for 2023-02-15 found this key 2 that matches 2023-02-15 and total_mov is 3000

looking for 2023-03-15 found this key 1 that matches 2023-03-15 and total_mov is 3000

looking for 2023-04-15 found this key 0 that matches 2023-04-15 and total_mov is 3000

looking for 2023-05-15 and found nothing

looking for 2023-06-15 and found nothing

looking for 2023-07-15 and found nothing

looking for 2023-08-15 and found nothing

looking for 2023-09-15 and found nothing

looking for 2023-10-15 and found nothing

looking for 2023-11-15 and found nothing

looking for 2023-12-15 and found nothing

CodePudding user response:

I think that you should fix this part:

if($found_key = array_search($searched_date, $datemovs)!==NULL){

to this:

$found_key = array_search($searched_date, $datemovs);
if($found_key !== false){

Here is the full part:

while (strtotime($anno_start . '-' . $mese_start . '-01') < strtotime($end)) {
    $mese_start_num = ltrim($mese_start, '0');
    $searched_date=$anno_start.'-'.$mese_start.'-15';
    $found_key = array_search($searched_date, $datemovs);
    if($found_key !== false){ // <-- THIS ROW
        echo ' found this key '.$found_key;
        $datatrovata = $entrata[$found_key]['datamov'];
        echo " that matches $datatrovata \n";
    }else{
        echo " and I found nothing";
    }
    $mese_start_num = $mese_start_num   1;
    if ($mese_start_num > 12) {
        $anno_start = $anno_start   1;
        $mese_start_num = 1;
        $mese_start='01';
    }
    if($mese_start_num<10){$mese_start='0'.$mese_start_num;}else{$mese_start=$mese_start_num;}
}

Because the array_search function returns the key of the first element that matches the searched value, it will always return a value, even if the value is not found in the array.

  • Related