0
foreach ($_GET['friend_name'] as $id => $key) {
    if (!empty($_GET['friend_vorname'][$id])) {
        var_dump($_GET['friend_anrede_'.$id][0]);
        $msg .= 'Anrede: ' . $_SESSION['friend_anrede'][$id][$id] .'<br>Vorname: ' 
              . $_GET['friend_vorname'][$id] . '<br>Name: '
              . $_GET['friend_name'][$id] . '<br>Strasse / Nr.: '
              . $_GET['friend_strasse_nr'][$id] . '<br>PLZ / Ort: '
              . $_GET['friend_plz_ort'][$id] . '<br><br>';
    }
}

The var_dump() returns:

string(4) "Frau" string(4) "Herr" string(4) "Frau"

I would now like to use the single stings for my variable $msg just like all the other variables.

I thought $_SESSION['friend_anrede'][$id][$id] would make it but it only returns:

F

I would be very glad if someone could get me out of this array labyrinth!

4
  • I am not sure if your question is entirely clear but what should be the output that you are looking for ? Commented Jan 11, 2016 at 16:11
  • What if you change $_SESSION['friend_anrede'][$id][$id]to $_SESSION['friend_anrede'][$id]? Not either sure I understand, though. Commented Jan 11, 2016 at 16:16
  • @Maximus2012 since the foreach will loop 3 times. The values should look like this: 1. Frau, 2: Herr, 3: Frau ... just the same as the result of var_dump but not as an array. Commented Jan 11, 2016 at 16:16
  • @MagnusEriksson $_SESSION['friend_anrede'][$id] only returns: Frau Commented Jan 11, 2016 at 16:40

1 Answer 1

3

All you need is $_SESSION['friend_anrede'][$id]. Remember that PHP allows you to treat a string as an array of characters, like this

$foo = 'Hello';
echo $foo[1]; // outputs 'e'

So your $_SESSION['friend_anrede'][$id][$id] was dumping out the 0th-character of the 0th-element in your friend_anrede array:

$_SESSION['friend_anrede'][$id][$id]
^------ 'Frau' ---------------^[0]
'Frau'[0] -> 'F'
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. My variable $_GET['friend_anrede'] looks a bit different, since there are multiple variables which look like this: $_GET['friend_anrede_0'], $_GET['friend_anrede_1'], $_GET['friend_anrede_2'] etc. The numbers at the end are getting added dynamically. I think that must be the point of my issue.
it's php. there's no need for creating dynamic "keys" like that. you can use the array-notation naming hack <input ... name="friend_anrede[0]", and then you'll get an array in $_GET['friend_anrede'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.