×

Welcome to Knowledge Base!

KB at your finger tips

This is one stop global knowledge base where you can learn about all the products, solutions and support features.

Categories
All
Web-PHP
PHP / APCUIterator::valid — DevDocs

APCUIterator::valid

(PECL apcu >= 5.0.0)

APCUIterator::valid Checks if current position is valid

Description

public APCUIterator::valid(): bool

Checks if the current iterator position is valid.

Parameters

This function has no parameters.

Return Values

Returns true if the current iterator position is valid, otherwise false .

See Also

  • APCUIterator::current() - Get current item
  • Iterator::valid() - Checks if current position is valid
PHP / array — DevDocs

array

(PHP 4, PHP 5, PHP 7, PHP 8)

array Create an array

Description

array(mixed ...$values): array

Creates an array. Read the section on the array type for more information on what an array is.

Parameters

values

Syntax "index => values", separated by commas, define index and values. index may be of type string or integer. When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1. Note that when two identical index are defined, the last overwrite the first.

Having a trailing comma after the last defined array entry, while unusual, is a valid syntax.

Return Values

Returns an array of the parameters. The parameters can be given an index with the => operator. Read the section on the array type for more information on what an array is.

Examples

The following example demonstrates how to create a two-dimensional array, how to specify keys for associative arrays, and how to skip-and-continue numeric indices in normal arrays.

Example #1 array() example

<?php
$fruits = array (
    "fruits"  => array("a" => "orange", "b" => "banana", "c" => "apple"),
    "numbers" => array(1, 2, 3, 4, 5, 6),
    "holes"   => array("first", 5 => "second", "third")
);
?>

Example #2 Automatic index with array()

<?php
$array = array(1, 1, 1, 1,  1, 8 => 1,  4 => 1, 19, 3 => 13);
print_r($array);
?>

The above example will output:

Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 13
    [4] => 1
    [8] => 1
    [9] => 19
)

Note that index '3' is defined twice, and keep its final value of 13. Index 4 is defined after index 8, and next generated index (value 19) is 9, since biggest index was 8.

This example creates a 1-based array.

Example #3 1-based index with array()

<?php
$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
?>

The above example will output:

Array
(
    [1] => January
    [2] => February
    [3] => March
)

As in Perl, you can access a value from the array inside double quotes. However, with PHP you'll need to enclose your array between curly braces.

Example #4 Accessing an array inside double quotes

<?php

$foo = array('bar' => 'baz');
echo "Hello {$foo['bar']}!"; // Hello baz!

?>

Notes

Note :

array() is a language construct used to represent literal arrays, and not a regular function.

See Also

  • array_pad() - Pad array to the specified length with a value
  • list() - Assign variables as if they were an array
  • count() - Counts all elements in an array or in a Countable object
  • range() - Create an array containing a range of elements
  • foreach
  • The array type
Read article
PHP / array_change_key_case — DevDocs

array_change_key_case

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

array_change_key_case Changes the case of all keys in an array

Description

array_change_key_case(array $array, int $case = CASE_LOWER): array

Returns an array with all keys from array lowercased or uppercased. Numbered indices are left as is.

Parameters

array

The array to work on

case

Either CASE_UPPER or CASE_LOWER (default)

Return Values

Returns an array with its keys lower or uppercased, or null if array is not an array.

Examples

Example #1 array_change_key_case() example

<?php
$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
?>

The above example will output:

Array
(
    [FIRST] => 1
    [SECOND] => 4
)

Notes

Note :

If an array has indices that will be the same once run through this function (e.g. " keY " and " kEY "), the value that is later in the array will override other indices.

Read article
PHP / array_chunk — DevDocs

array_chunk

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

array_chunk Split an array into chunks

Description

array_chunk(array $array, int $length, bool $preserve_keys = false): array

Chunks an array into arrays with length elements. The last chunk may contain less than length elements.

Parameters

array

The array to work on

length

The size of each chunk

preserve_keys

When set to true keys will be preserved. Default is false which will reindex the chunk numerically

Return Values

Returns a multidimensional numerically indexed array, starting with zero, with each dimension containing length elements.

Errors/Exceptions

If length is less than 1 , a ValueError will be thrown.

Changelog

Version Description
8.0.0 If length is less than 1 , a ValueError will be thrown now; previously, an error of level E_WARNING has been raised instead, and the function returned null .

Examples

Example #1 array_chunk() example

<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>

The above example will output:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
        )

)
Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [2] => c
            [3] => d
        )

    [2] => Array
        (
            [4] => e
        )

)

See Also

  • array_slice() - Extract a slice of the array
Read article
PHP / array_column — DevDocs

array_column

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

array_column Return the values from a single column in the input array

Description

array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array

array_column() returns the values from a single column of the array , identified by the column_key . Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.

Parameters

array

A multi-dimensional array or an array of objects from which to pull a column of values from. If an array of objects is provided, then public properties can be directly pulled. In order for protected or private properties to be pulled, the class must implement both the __get() and __isset() magic methods.

column_key

The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be null to return complete arrays or objects (this is useful together with index_key to reindex the array).

index_key

The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name. The value is cast as usual for array keys (however, prior to PHP 8.0.0, objects supporting conversion to string were also allowed).

Return Values

Returns an array of values representing a single column from the input array.

Changelog

Version Description
8.0.0 Objects in columns indicated by index_key parameter will no longer be cast to string and will now throw a TypeError instead.

Examples

Example #1 Get the column of first names from a recordset

<?php
// Array representing a possible record set returned from a database
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);
 
$first_names = array_column($records, 'first_name');
print_r($first_names);
?>

The above example will output:

Array
(
    [0] => John
    [1] => Sally
    [2] => Jane
    [3] => Peter
)

Example #2 Get the column of last names from a recordset, indexed by the "id" column

<?php
// Using the $records array from Example #1
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
?>

The above example will output:

Array
(
    [2135] => Doe
    [3245] => Smith
    [5342] => Jones
    [5623] => Doe
)

Example #3 Get the column of usernames from the public "username" property of an object

<?php

class User
{
    public $username;

    public function __construct(string $username)
    {
        $this->username = $username;
    }
}

$users = [
    new User('user 1'),
    new User('user 2'),
    new User('user 3'),
];

print_r(array_column($users, 'username'));
?>

The above example will output:

Array
(
    [0] => user 1
    [1] => user 2
    [2] => user 3
)

Example #4 Get the column of names from the private "name" property of an object using the magic __get() method.

<?php

class Person
{
    private $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function __get($prop)
    {
        return $this->$prop;
    }

    public function __isset($prop) : bool
    {
        return isset($this->$prop);
    }
}

$people = [
    new Person('Fred'),
    new Person('Jane'),
    new Person('John'),
];

print_r(array_column($people, 'name'));
?>

The above example will output:

Array
(
    [0] => Fred
    [1] => Jane
    [2] => John
)
If __isset() is not provided, then an empty array will be returned.
Read article
PHP / array_combine — DevDocs

array_combine

(PHP 5, PHP 7, PHP 8)

array_combine Creates an array by using one array for keys and another for its values

Description

array_combine(array $keys, array $values): array

Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.

Parameters

keys

Array of keys to be used. Illegal values for key will be converted to string .

values

Array of values to be used

Return Values

Returns the combined array .

Errors/Exceptions

As of PHP 8.0.0, a ValueError is thrown if the number of elements in keys and values does not match. Prior to PHP 8.0.0, a E_WARNING was emitted instead.

Changelog

Version Description
8.0.0 array_combine() will now throw a ValueError if the number of elements for each array is not equal; previously this function returned false instead.

Examples

Example #1 A simple array_combine() example

<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>

The above example will output:

Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)

See Also

  • array_merge() - Merge one or more arrays
  • array_walk() - Apply a user supplied function to every member of an array
  • array_values() - Return all the values of an array
Read article

Still Thinking?
Give us a try!

We embrace agility in everything we do.
Our onboarding process is both simple and meaningful.
We can't wait to welcome you on AiDOOS!