Get first value of an array

14,061

Solution 1

You can use array_column function

$array =  array(
    array('Pen', 'Apple' ),
    array('Oooo', 'Pineapple pen')
);

$result = array_column($array, 0);

echo '<pre>';
print_r($result);
echo '</pre>';

Output:

Array
(
    [0] => Pen
    [1] => Oooo
)

Solution 2

Use reset http://php.net/manual/es/function.reset.php

This function sets the internal pointer of the array to first element and also returns it.

$first = reset($array)

Share:
14,061
Frunky
Author by

Frunky

Hello! My name - Anuar. I'm web-developer.

Updated on June 04, 2022

Comments

  • Frunky
    Frunky almost 2 years

    I have an array:

    Array (
    [0] => Array
        (
            [0] => Pen
            [1] => Apple
        )
    
    [1] => Array
        (
            [0] => Oooo
            [1] => Pineapple pen
        )
    

    How I can get a first elements of each array?

    For example: Pen Oooo

    It's my function

    $parameters = array('wiki_1', 'wiki_2', 'wiki_3', 'wiki_4','wiki_5' ,'wiki_6', 'wiki_7', 'wiki_8', 'wiki_9', 'wiki_10', 'wiki_11', 'wiki_12');
    
    function wiki_custom_fields( $parameters, $id ) {
            foreach ($parameters as $parameter) {
               $wiki_result[] = get_post_custom_values($parameter, $id, true);
            }
    
        echo '<pre>';
        print_r($wiki_result);
        echo '</pre>';
    }
    

    If I use print_r($wiki_result[][0]); it's get 500 Error.