Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7956115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:53:55+00:00 2026-06-04T03:53:55+00:00

I’m sure this question has been asked before, my apologies for not finding it

  • 0

I’m sure this question has been asked before, my apologies for not finding it first.

The original array:

[0] => Array
    (
        [categoryId] => 1
        [eventId] => 2
        [eventName] => 3
        [vendorName] => 4
    )

[1] => Array
    (
        [categoryId] => 5
        [eventId] => 6
        [eventName] => 7
        [vendorName] => 8
    )

[2] => Array
    (
        [categoryId] => 9
        [eventId] => 10
        [eventName] => 11
        [vendorName] => 12
    )

My hoped for result out of: print_r(get_values_from_a_key_in_arrays(‘categoryId’, $array));

[0] => 1
[1] => 5
[2] => 9

I’m just looking for something cleaner than writing my own foreach based function. If foreach is the answer, I already have that in place.

Edit: I don’t want to use a hard-coded key, I was just showing an example call to the solution. Thanks! ^_^

Quick Grab Solution for PHP 5.3:

private function pluck($key, $data) {
    return array_reduce($data, function($result, $array) use($key) {
        isset($array[$key]) && $result[] = $array[$key];
        return $result;
    }, array());
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:53:59+00:00Added an answer on June 4, 2026 at 3:53 am

    So, the cool thing about higher-order collection/iterator functions such as pluck, filter, each, map, and friends is that they can be mixed and matched to compose a more complex set of operations.

    Most languages provide these types of functions (look for packages like collection, iterator, or enumeration/enumerable)…some provide more functions than others and you will commonly see that the functions are named differently across languages (i.e. collect == map, reduce == fold). If a function doesn’t exist in your language, you can create it from the ones that do exist.

    As for your test case…we can use array_reduce to implement pluck. The first version I posted relied on array_map; however, I agree with @salathe that array_reduce is more succinct for this task; array_map is an OK option, but you end up having to do more work in the end. array_reduce can look a bit odd at first, but if the callback is neatly organized, all is well.

    A less naive pluck would also check to see if it can “call” (a function/method) on the iterated value. In the naive implementation below, we assume the structure to be a hash (associative array).

    This will setup the test-case data (Fixtures):

    <?php
    
    $data[] = array('categoryId' => 1,    'eventId' => 2,  'eventName' => 3,  'vendorName' => 4);
    $data[] = array('categoryId' => 5,    'eventId' => 6,  'eventName' => 7,  'vendorName' => 8);
    $data[] = array('categoryId' => 9,    'eventId' => 10, 'eventName' => 11, 'vendorName' => 12);
    $data[] = array(/* no categoryId */   'eventId' => 10, 'eventName' => 11, 'vendorName' => 12);
    $data[] = array('categoryId' => false,'eventId' => 10, 'eventName' => 11, 'vendorName' => 12);
    $data[] = array('categoryId' => 0.0,  'eventId' => 10, 'eventName' => 11, 'vendorName' => 12);
    

    Choose the version of pluck you’d prefer

    $preferredPluck = 'pluck_array_reduce'; // or pluck_array_map
    

    “pluck” for PHP 5.3+: array_reduce provides a terse implementation though not as easy to reason about as the array_map version:

    function pluck_array_reduce($key, $data) {
      return array_reduce($data, function($result, $array) use($key){
        isset($array[$key]) &&
          $result[] = $array[$key];
    
        return $result;
      }, array());
    }
    

    “pluck” for PHP 5.3+: array_map isn’t perfect for this so we have to do more checking (and it still doesn’t account for many potential cases):

    function pluck_array_map($key, $data) {
      $map = array_map(function($array) use($key){
        return isset($array[$key]) ? $array[$key] : null;
      }, $data);
    
      // is_scalar isn't perfect; to make this right for you, you may have to adjust
      return array_filter($map, 'is_scalar');
    }
    

    “pluck” for legacy PHP <5.3

    We could have used the legacy create_function; however, it is bad form, not recommended, and also not at all elegant, thus, I’ve decided not to show it.

    function pluck_compat($key, $data) {
      $map = array();
      foreach ($data as $array) {
        if (array_key_exists($key, $array)) {
          $map[] = $array[$key];
        }
      }
      unset($array);
    
      return $map;
    }
    

    Here we choose a version of “pluck” to call based on the version of PHP we are running. If you run the entire script, you should get the correct answer no matter what version you are on.

    $actual   = version_compare(PHP_VERSION, '5.3.0', '>=')
              ? $preferredPluck('categoryId', $data)
              : pluck_compat('categoryId', $data);
    $expected = array(1, 5, 9, false, 0.0);
    $variance = count(array_diff($expected, $actual));
    
    var_dump($expected, $actual);
    echo PHP_EOL;
    echo 'variance: ', $variance, PHP_EOL;
    
    print @assert($variance)
        ? 'Assertion Failed'
        : 'Assertion Passed';
    

    Notice there is no ending ‘?>’. That is because it isn’t needed. More good can come of leaving it off than from keeping it around.

    FWIW, it looks like this is being added to PHP 5.5 as array_column.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.