Retrieving All Keys from an Array in PHP

Q

How To Get All the Keys Out of an Array in PHP?

✍: FYIcenter.com

A

Function array_keys() returns a new array that contains all the keys of a given array. Here is a PHP script on how to use array_keys():

<?php 
$mixed = array();
$mixed["Zero"] = "PHP";
$mixed[1] = "Perl";
$mixed["Two"] = "Java";
$mixed["3"] = "C+";
$mixed[""] = "Basic";
$mixed[] = "Pascal";
$mixed[] = "FORTRAN";
$keys = array_keys($mixed);
print("Keys of the input array:\n");
print_r($keys);
?>

This script will print:

Keys of the input array:
Array
(
    [0] => Zero
    [1] => 1
    [2] => Two
    [3] => 3
    [4] =>
    [5] => 4
    [6] => 5
)

 

PHP Built-in Functions for Arrays

⇒⇒PHP Tutorials

2017-01-21, 358👍, 0💬