get element by index array php

Asked : Nov 17

Viewed : 62 times

I have an array like:

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
echo native_function($array, 0); // bar
echo native_function($array, 1); // bin
echo native_function($array, 2); // ipsum

So, this native function would return a value based on a numeric index (second arg), ignoring assoc keys, looking for the real position in an array.

Is there any native function to do that in PHP or would it be advisable for me to write it?

Thank You.

php array 

Nov 17

3 Answers

For get index from element we have to use array_search().

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;

For get value from index of array we have to use array_values().

$array = array('foo' => 'bar', 33 => 'bin', 'lorem' => 'ipsum');
$array = array_values($array);
echo $array[0]; //bar
echo $array[1]; //bin
echo $array[2]; //ipsum

answered Dec 02


Use the Array Key or Index

If you want to access an individual value from an indexed, associative, or multidimensional array you can either do it through using the array index or key.

Let's check out the following example to understand how it basically works:

Example

Try this code ยป

<?php
// Indexed array
$sports = array("Baseball", "Cricket", "Football", "Shooting");
 
// Associative array
$cities = array("France"=>"Paris", "India"=>"Mumbai", "UK"=>"London", "USA"=>"New York");
 
// Multidimensional array
$superheroes = array(
    array(
        "name" => "Peter Parker",
        "character" => "Spider-Man",
    ),
    array(
        "name" => "Tony Stark",
        "character" => "Iron-Man",
    ),
    array(
        "name" => "Clark Kent",
        "character" => "Super-Man",
    )
);
 
echo $sports[0]; // Outputs: Baseball
echo "<br>";
echo $sports[1]; // Outputs: Cricket
echo "<br>";
echo $cities["France"]; // Outputs: Paris
echo "<br>";
echo $cities["USA"]; // Outputs: New York
echo "<br>";
echo $superheroes[0]["name"]; // Outputs: Peter Parker
echo "<br>";
echo $superheroes[1]["character"]; // Outputs: Iron-Man
?>

answered Dec 30


  • Using the index of the elements
  • Using reset() function
  • Using current() function

Use the Index of Elements to Get the First Element of an Array in PHP

We know that the index of the first element in an array is 0, therefore, you can directly get the first element by accessing it through its index. The correct syntax to get the element via an index is as follows

$arrayName[0];

To get the first element, the index of the first element, 0, is enclosed in square brackets along with the array name.

<?php
$flowers = array("Rose","Lili","Jasmine","Hibiscus","Tulip","Sun Flower","Daffodil","Daisy");
$firstElement = $flowers[0];
echo "The first element of the array is $firstElement."
?>

Output:

The first element of the array is Rose.

Use the reset() Function to Get the First Element of an Array in PHP

The built-in function reset() sets the pointer of an array to its start value i.e the first element of the array.

reset($arrayName);

It has only one parameter. It returns the value of the first element if the array is not empty. It returns false if the array is empty.

<?php
$flowers = array("Rose","Lili","Jasmine","Hibiscus","Tulip","Sun Flower","Daffodil","Daisy");
$firstElement = reset($flowers);
echo "The first element of the array is $firstElement."
?>

Output:

The first element of the array is Rose.

Use the current() Function to Get the First Element of an Array in PHP

The current() the function is another built-in function used to get the value in the array that the pointer is currently pointing. It can be used to get the first element of the array if there is no internal pointer in the array. The pointer points to the first element by default. The correct syntax to use this function is as follows

current($arrayName);

It only accepts one parameter $arrayName. The variable $arrayName is an array of which we want to get the first element.

<?php
$flowers = array("Rose","Lili","Jasmine","Hibiscus","Tulip","Sun Flower","Daffodil","Daisy");
$firstElement = current($flowers);
echo "The first element of the array is $firstElement."
?>

Output:

The first element of the array is Rose.

answered Dec 30


Login and Submit Your Answer
Browse other questions tagged  php  array or ask your own question.