A
A
Albert Kazan2017-05-14 17:35:49
PHP
Albert Kazan, 2017-05-14 17:35:49

How to find in an array by a numeric key, if all the keys are string?

array(
"mandarin" => "frukt",
"gopota" => "ovoch"
);

How can I get, for example, the first value of an array?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolay, 2017-05-15
@Farrien

as an option - a function

$myArr = array(
  "mandarin" => "frukt",
  "gopota" => "ovoch"
);

function searchByKey($array, $num) {
  $i = 0;
  foreach($array as $item) {
    if($i === $num) return $item;
    $i++;
  }
}

echo serachByKey($myArr, 1); // => ovoch

I
iDrugov, 2017-05-14
@iDrugov

$myArr = array(
"mandarin" => "frukt",
"gopota" => "ovoch"
);

echo reset($myArr); // frukt
// или:
echo current($myArr); // frukt

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question