Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
PHP - Get Array value by Index instead of Key


I've got a multidimensional array returned by an API call. The values into this array are stored with a key like this :

Array(["BTC_XRP"] => 1)

I can get the values of the Array by getting them using the keys (like BTC_XRP), but what if I need to get the value using the Index?
For example, how do I get each value of the Array using a for loop if I can't get the values by the Index?
Since the Array is composed by something like 100 values, I need to echo every one using a for loop, but this gives me this error :

Notice: Undefined offset: 0

Is it possible to get to a value using the Index instead of the Key?
Furthermore, I'd like to get the Key by the Index. If I want to get the Key of the first value, I'd like to get it using the 0 Index.
Thanks!

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Sounds like you are in search of array_keys (to get key by index) and array_values (to get value by index) functions:

$array = array("BTC_XRP" => 1, "EUR_XRP" => 234, "USD_XRP" => 567);
$keys   = array_keys( $array );
$values = array_values( $array );

var_dump( $keys[1] ); // string(7) "EUR_XRP"
var_dump( $values[1] ); // int(234)

Or use a foreach as Joshua suggests.

about 4 years ago · Santiago Trujillo Relatório

0

Try:

foreach ($array as $key => $value) {
  echo "Key: $key";
}

Where $array is the array you want to loop through.

It will print out all the keys

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda