user9988771
user9988771

Reputation:

how to get empty index foreach loop in php

Here i am having one array, using this array i have to get the index like which having null or empty values,i goggled it but i am not getting my expected answer,kindly see below i have posted my expected answer.

print_r($val);

    Array
(
    [id] => 4 A
    [tripID] => 
    [startFrom] => 1
    [limit] => 20
    [cabID] => 
)

Expected answer

  1. tripID
  2. cabID

Upvotes: 1

Views: 228

Answers (2)

Sougata Bose
Sougata Bose

Reputation: 31749

This should help -

$a = array
(
    'ID' => '4 A',
    'tripID' => '',
    'startFrom' => 1,
    'limit' => 20,
    'cabID' => '',
);
// Filter array if value is  blank or null but not 0
$check = array_filter($a, function($v) {
  return $v == '' || $v == null;
});
// Extract the keys
print_r(array_keys($check));

Output

Array
(
    [0] => tripID
    [1] => cabID
)

array_filter()

Upvotes: 2

Joshua
Joshua

Reputation: 1126

This function should put you in the right direction array_filter. Seems like a homeworkey question, mind.

Upvotes: 0

Related Questions