2

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.

Dave
  • 3,073
  • 7
  • 20
  • 33
Frunky
  • 385
  • 1
  • 6
  • 14

5 Answers5

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)

David Rojo
  • 2,337
  • 1
  • 22
  • 44
  • Also is more optimal than array_keys as if the array contains for example 1000 values, array_keys would generate an array of 1000 positions just to get the first element, with reset you Will not generate extra unused data that consumes memory. – David Rojo Oct 22 '16 at 13:55
  • Thanks. Almost got it, but it's looking now like this 'Array ( [0] => Pen, [1] => Apple )' And I need to get 'Array ( [0] => Array ( [0] => Pen) [1] => Array ( [0] => Oooo ))' – Frunky Oct 22 '16 at 14:00
  • $r = []; foreach($array as $k => $list){ $r[$k] = reset($list); } – David Rojo Oct 22 '16 at 14:02
  • Sorry. Array ( [0] => Pen, [1] => Apple ) And I need to get Array ( [0] => Array ( [0] => Pen ) [1] => Array ( [0] => Oooo ) ) – Frunky Oct 22 '16 at 14:02
  • Then just $r[$k][] = reset($list) – David Rojo Oct 22 '16 at 18:43
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
)
Manh Nguyen
  • 930
  • 5
  • 12
0
$param = array('first_key'=> 'First', 2, 3, 4, 5);
$keys   =   array_keys($param);
echo "Key = ".$keys[0];

example:

    $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);
        }

    $keys   =  array_keys($wiki_resul);
    echo "Key = ".$keys[0];
    echo '<pre>';
    print_r("Key = ".$keys[0]);
    echo '</pre>';
}
Yatish Agrawal
  • 452
  • 5
  • 15
0

Try:

$result = array();
foreach ($elements as $elem){
    $result[] = $elem[0];
}

$result contains 'Pen', 'Oooo'.

The sintaxis $result[] is for adding a element to the last position in the array, the same as array_push (http://php.net/manual/es/function.array-push.php)

SebCar
  • 328
  • 3
  • 12
-1

try this code

print_r($wiki_result[0]);
Khetesh kumawat
  • 681
  • 7
  • 15