1

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
3
  • Do you select data from database?Give us the code and query and loops. I'm afraid I can't help you with just a printed array because the printed array is just the result of problem not the problem itself. Commented Jul 18, 2018 at 5:20
  • I'm not sure the sample array is valid. You are missing the comma at the end of each line. Commented Jul 18, 2018 at 5:24
  • Possible duplicate of Remove empty array elements Commented Jul 18, 2018 at 5:33

2 Answers 2

2

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()

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.