1

I am trying to create an associative array with the keys being email addresses and the values being passwords. It is reading from an XML database to get the information. Here is my code:

$data = simplexml_load_file("Treasury.xml");
//Add in all passwords
for ($i = 0; $i < count($data->Member); $i++) {
    $key = $data->Member[$i]->Email + '';
    $USERS[$key] = $data->Member[$i]->Pin;
}

The problem comes in the for loop. It gets a correct count of the members (I had that print out) but the key is always being labeled as the number 0, resulting in only the last pin being stored in an array on length 1. Is there something syntactically that I am doing wrong?

Thanks in advance.

EDIT: I did a var_dump of the first user in the XML document. Here it is (Sorry for how long it is):

object(SimpleXMLElement)#4 (5) { ["Name"]=> string(19) "Mackenzie Daugherty" ["PC"]=> object(SimpleXMLElement)#2 (0) { } ["Email"]=> string(16) "[email protected]" ["Pin"]=> string(4) "0000" ["Payments"]=> object(SimpleXMLElement)#3 (1) { ["Payment"]=> array(2) { [0]=> object(SimpleXMLElement)#5 (7) { ["Type"]=> string(4) "Dues" ["Description"]=> string(18) "Dues for Fall 2013" ["DateIssued"]=> string(7) "8/26/13" ["DateEnd"]=> string(6) "9/9/13" ["Owed"]=> string(2) "55" ["Paid"]=> string(2) "55" ["Plan"]=> object(SimpleXMLElement)#7 (5) { ["InPlan"]=> string(1) "0" ["PlanDescription"]=> object(SimpleXMLElement)#8 (0) { } ["Intervals"]=> string(1) "0" ["Completed"]=> string(1) "0" ["PerInterval"]=> string(1) "0" } } [1]=> object(SimpleXMLElement)#6 (7) { ["Type"]=> string(19) "Tiger Tunes Tickets" ["Description"]=> string(18) "Two Saturday Night" ["DateIssued"]=> string(7) "8/26/13" ["DateEnd"]=> string(7) "8/26/13" ["Owed"]=> string(2) "30" ["Paid"]=> string(2) "30" ["Plan"]=> object(SimpleXMLElement)#7 (5) { ["InPlan"]=> string(1) "0" ["PlanDescription"]=> object(SimpleXMLElement)#8 (0) { } ["Intervals"]=> string(1) "0" ["Completed"]=> string(1) "0" ["PerInterval"]=> string(1) "0" } } } } }
5
  • Can you try a var_dump($data->Member[$i]); exit; in the loop for me and edit your question with the value? Commented Jan 4, 2014 at 1:38
  • Added the var_dump for the first user. Commented Jan 4, 2014 at 1:44
  • Since $data->Member[$i]->Email and ->Pin appear to be simple strings, you shouldn't need any of the additional type casting used. Try simply $USERS[$data->Member[$i]->Email] = $data->Member[$i]->Pin;, if that works I can post as an answer with some more info. Commented Jan 4, 2014 at 1:47
  • No luck, it still only stores the last user in the file's password in a key of 0. Commented Jan 4, 2014 at 1:51
  • Did you get rid of the unnecessary concatenation as well? + '' Commented Jan 4, 2014 at 1:52

1 Answer 1

3

As clearly stated in the documentation that I'm sure you've been studying carefully, the PHP concatenation operator is ., not +.

Your code takes two operands, and attempts to perform arithmetic addition on them. Since they are not [meaningful] numbers, you end up with 0.

(I couldn't give a more detailed assessment without knowing the precise values of your operands, which you did not provide.)

Your code should read:

$key = $data->Member[$i]->Email . '';
//                              ^
//  (is the concatenation necessary at all?
//   isn't Email already a string?)

Make the same correction elsewhere.

Sign up to request clarification or add additional context in comments.

2 Comments

Well that's embarrassing! I'd switched to Java for a while and just went back to PHP. It's always the little errors that make me go in search of hard answers.
@Jake: And here you find them in the form of multiple live demos and code samples! Wahey! We aim to please.

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.