T
T
Timofey Mikhailov2017-03-02 19:33:23
PHP
Timofey Mikhailov, 2017-03-02 19:33:23

Php - How does an associative array happen?

I started listening to video tutorials on PHP 7 here, and in one of the lessons they said that if you take two ordinary arrays and combine them, you get an associative array.
I was wondering how php would behave if the number of elements in the two arrays were different...
I wrote the following code:

$f1=["Jon","Stas","Fill"];
    $f2=["Anna","Jein"];
    $f=$f1+$f2;

    for ($i=0; $i < count($f); $i++) { 
      echo "$i --> $f[$i]<br>";
    }
    echo "<br>";
    echo "--> $f['Jon']";
    echo "<br>";
    foreach ($f as $k => $v) {
      echo "$k -- $v<br>";
    }

And what I got as a result:
============================
0 --> Jon
1 --> Stas
2 --> Fill
-->
0 -- Jon
1 -- Stas
2 -- Fill
============================
i.e. the code on the line didn't give me anything at all ( echo "--> $f['Jon']"; )
I'm just wondering, did I do something wrong, or did the one who explains it not correctly put it?
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Sisyukin, 2017-03-02
@JimmiJanger

It didn't show up because you have an error
echo "--> $f['key']"; echo
"--> ".$f['key'];
$f=$f1+$f2 is not nonsense, the operator has been working like this for a long time ... The same as the merge ...
P.S.
In your link, the first line is also nonsense)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question