Skip to content

pfrembot/array-map-assoc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Array Map Assoc

Build Status

Add array_map functions for mapping associative arrays whilst maintaining their original array keys

Installation

composer require pfrembot/array-map-assoc

Usage

require_once 'vendor/autoload.php';

use Pfrembot;

$array = [
    'foo' => 1,
    'bar' => 2,
    'baz' => 3,
];

$newArray = Pfrembot\array_map_assoc($array, function($value, $key) {
  return $value * 2;
});

var_dump($newArray);

/*
Outputs:
    array(3) {
      'foo' =>
      int(2)
      'bar' =>
      int(4)
      'baz' =>
      int(6)
    }
*/

Functions

array_map_assoc

Applies the user defined callback to each key/value pair in the array, and maps the callback result as the new array value

array array_map_assoc ( array $array , callable $callback )

Parameters

array

The input array

callback

The callback to be applied to each key/value pair in $array

mixed callback( mixed $value , mixed $key )
  • value : Holds the value of the current iteration.

  • key : Holds the key of the current iteration.

  • Note: Many internal functions (for example strtolower()) will throw a warning if more than the expected number of argument are passed in and are not usable directly as a callback.

Returns

Returns the values of the original array mapped back to the original array keys

About

Array map functions that do not re-index the resulting array

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages