arrayutils
  • Home
  • 📖How to use?
    • âš¡Installation
  • 📖Methods
    • âš¡Static method
      • static from()
      • static of()
      • static mapToArray()
    • âš¡Generic method
      • join()
      • every()
      • some()
      • reduce()
        • reduceRight()
      • sum()
      • pop()
      • shift()
      • includes()
      • keyExists()
      • indexOf()
      • find()
        • findIndex()
      • first()
        • keyFirst()
      • last()
        • keyLast()
      • random()
        • keyRandom()
      • splice()
    • âš¡Chain method
      • chunk()
      • column()
      • combine()
      • concat()
        • concatSoft()
      • countValues()
      • diff()
        • diffAssoc()
        • diffKey()
      • fill()
        • fillKeys()
      • filter()
      • flat()
        • flatMap()
      • flip()
      • forEach()
      • intersect()
        • intersectAssoc()
        • intersectKey()
      • keys()
      • map()
        • mapAssoc()
        • mapKey()
      • pad()
      • push()
      • replace()
      • reverse()
      • slice()
      • sort()
        • sortKey()
      • unique()
      • unshift()
      • values()
  • 📖Suffixes
    • âš¡Suffix - From
    • âš¡Suffix - As
  • links
    • 📌Github repo
    • 📌Packagist project
    • 📌Poggit project
Powered by GitBook
On this page
  • Syntax
  • Parameter
  • Return value
  • Prefixing
  • References
  1. 📖Methods
  2. ⚡Chain method

column()

The column() method returns the values from a single column in the input array

Example.php
<?php
use kim\present\utils\arrays\ArrayUtils;

$arrayUtils = ArrayUtils::from([
    ["any" => "first",  1, 2, 3],
    ["any" => "second", 4, 5, 6],
    ["any" => "third",  7, 8, 9]
]);

// Use "any" value in internal array as value of array
$arrayUtils->column("any");
// expected output: ["first", "second", "third"]


// Use 2nd value in internal array as value,
// Use "any" in internal array as key of array
$arrayUtils->column(1, "any");
// expected output: ["first" => 2, "second" => 5, "third" => 8]


// Use value in internal array as value,
// Use "any" in internal array as key of array
$arrayUtils->column(null, "any");
// expected output: [
//   "first"  => ["any" => "first",  1, 2, 3],
//   "second" => ["any" => "second", 4, 5, 6],
//   "third"  => ["any" => "third",  7, 8, 9]
//]

Syntax

$arrayUtils->column(mixed $valueKey, mixed $indexKey = null) : ArrayUtils;

Parameter

  • $valueKey

    The key value of the element to be used as the value.

    If is null, Use element to value.

  • The key value of the element to be used as the key. Default is NULL. If is null, Re-index from 0.

Return value

  • A array of values representing a single column from the input array.

Prefixing

$arrayUtils->columnAs(mixed $valueKey, mixed $indexKey = null) : array;
ArrayUtils::columnFrom(iterable $from, mixed $valueKey, mixed $indexKey = null) : ArrayUtils;
ArrayUtils::(iterable $from, mixed $valueKey, mixed $indexKey = null) : array;

References

Previouschunk()Nextcombine()

Last updated 4 years ago

$indexKey

https://www.php.net/manual/en/function.array-columnwww.php.net