Definition of "arrows" in PHP

Asked

Viewed 75 times

3

I don’t know if I got it very well in the official php documentation (https://www.php.net/manual/en/language.types.array.php), so I came here to ask a question. I’m giving a general review, finally my question is:

$foo = ["a" => "laranja", "b" => "maça"];

Then when I access the value of A or position 0 will equal orange?

PS: I’m a little lost in that if I understood it right...

3 answers

2


The PHP array’s key statements are optional.

$array = ["foo", "bar"];

echo $array[0];
echo $array[1];

//Saídas
"foo"
"bar"

If keys are declared, access to their values shall be made through the specified key.

$foo = ["a" => "laranja", "b" => "maça"];

echo $foo["a"];
echo $foo["b"];

//Saídas
"laranja"
"maça"

But if there is a mixture of defined keys and other omitted keys, the index will be the first value of the array that does not have a key declared explicitly.

$foo = ["a" => "laranja", "b" => "maça", "morango", "c" => "pera", "banana"];

echo $foo["a"];
echo $foo["b"];
echo $foo[0];
echo $foo["c"];
echo $foo[1];

//Saídas
"laranja"
"maça"
"morango"
"pera"
"banana"

UPDATE

If you have an array within another array (multidimensional array), access to its values will depend on the association that has been defined.

$foo = [
    "a" => "laranja",
    "pera",
    "vermelhas" => ["um" => "morango", "três" => "maçã", "framboesa"]
];

echo $foo[0];
echo $foo["vermelhas"];
echo $foo["vermelhas"]["dois"];
echo $foo["vermelhas"][0];

//Saídas
"pera"
Array to string
Undefined index: dois
"framboesa"
  • Yes! But I was in doubt now that I mentioned, if I have an array like this your $foo, and inside the key "a" I have another array, how would it look? I do not know if it is so understandable my question, but it would be basically if each key (equal to, b c) had an array inside as containing other values what would be the result and how would it access? If possible explain me in your question (if you can give an Edit)

  • 1

    @Devprogramacao, updated my question :)

1

I’d never thought of it, but I’ve come to the conclusion that nay. Associative arrays do not allow you to get the value of a position by the index it represents, but you can find out which key is correct and use it to recover the value:

<?php

$foo = ["a" => "laranja", "b" => "maça"];

$arrKey= array_keys($foo);
echo $foo[$arrKey[0]];

But about the arrows, you’re right: The structure is [chave => valor, chave => valor]

https://www.w3schools.com/php/func_array_keys.asp https://www.php.net/manual/en/function.array-keys.php

  • A hint: it makes much more sense for you to cite as a source the official documentation of a native language function than a page maintained by people with no direct connection to the language.

  • Thanks for the tip, I added the source of the official documentation.

0

These arrows are the operators that associate indexes to values in arrays. It functions as a kind of dictionary, where 'chave' => 'valor'. Using the keys, they will work as indexes, and you will not be able to get them by the position of each one.

$paises = [
    'usa' => 'Estados Unidos',
    'br'  => 'Brasil',
    'lon' => 'Londres'
];

// obtendo por índice
echo $paises[1];     // Erro - não existe uma chave '1'

// obtendo por chave
echo $paises['lon']; // Certo - Londres

Browser other questions tagged

You are not signed in. Login or sign up in order to post.