2017 © Pedro Peláez
 

library extract_property

Extracts a given property from an array of objects

image

dcelasun/extract_property

Extracts a given property from an array of objects

  • Thursday, July 11, 2013
  • by dcelasun
  • Repository
  • 1 Watchers
  • 2 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

extract_property() for PHP

Build Status, (*1)

This simple library provides an easy way to extract a given property from an array of objects., (*2)

Usage

array extract_property( array $objects, mixed $key [, mixed $index] )

Given an array of objects, extract_property() returns the values from a single property of each object, identified by $key. Optionally, you may provide an $index to index the values in the returned array by the values from the $index property of each object in the input array., (*3)

For example, using the following $users array, we tell extract_property() to return an array of just the names, indexed by their record IDs., (*4)

``` php <?php $users = array();, (*5)

$a = new stdClass; $b = new stdClass; $c = new stdClass;, (*6)

$a->id = 1; $a->name = 'John'; $a->surname = 'Doe';, (*7)

$users[] = $a;, (*8)

$b->id = 2; $b->name = 'Jane'; $b->surname = 'Doe';, (*9)

$users[] = $b;, (*10)

$c->id = 3; $c->name = 'Foo'; $c->surname = 'Bar';, (*11)

$users[] = $c;, (*12)

$names = extract_property($users, 'name', 'id');, (*13)


If we call `print_r()` on `$names`, you'll see a resulting array that looks a bit like this: ``` text Array ( [1] => John [2] => Jane [3] => Foo )

You could of course omit the $index so the resulting array looks like this:, (*14)

``` text Array ( [0] => John [1] => Jane [2] => Foo ), (*15)



## Installation The easiest way to install this library is to use Composer and add the following to your project's `composer.json` file: ``` javascript { "require": { "dcelasun/extract_property": "1.*" } }

Then, when you run composer install, everything will fall magically into place, and the extract_property() function will be available to your project, as long as you are including Composer's autoloader., (*16)

However, you do not need Composer to use this library., (*17)

This library has no dependencies and should work on older versions of PHP. Download the code and include src/extract_property.php in your project, and everything should work fine., (*18)

The Versions

11/07 2013

dev-master

9999999-dev https://github.com/dcelasun/extract_property

Extracts a given property from an array of objects

  Sources   Download

MIT

extract property extract_property

11/07 2013

1.0.1

1.0.1.0 https://github.com/dcelasun/extract_property

Extracts a given property from an array of objects

  Sources   Download

MIT

extract property extract_property

11/07 2013

1.0.0

1.0.0.0 https://github.com/dcelasun/extract_property

Extracts a given property from an array of objects

  Sources   Download

MIT

extract property extract_property