0

From the following array I can pull the last names by using array_column built in function of PHP. Now, My question is- Can I pull data if last_name is not empty?

$a = array(
  array(
    'id' => 5698,
    'first_name' => 'Peter',
    'last_name' => 'Griffin',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Ben',
    'last_name' => ''
  )
);

$last_names = array_column($a, 'last_name');
print_r($last_names);

That means My output will be Griffin.

1
  • 2
    Using array_filter()... Commented Dec 6, 2016 at 9:29

1 Answer 1

2

You can easily do it by array_diff

<?php
$a = array(
  array(
    'id' => 5698,
    'first_name' => 'Peter',
    'last_name' => 'Griffin',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Ben',
    'last_name' => ''
  )
);

$last_names = array_diff(array_column($a, 'last_name'),['']);
print_r($last_names);
?>

Live demo : https://eval.in/691720

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.