arfa Posted November 20, 2009 Share Posted November 20, 2009 Adding a value to an array. Why won't this work? $notify_list=file($filename); $notify_list = array_push($notify_list,"$user_em"); foreach($notify_list as $key => $val) { echo "$val<BR>"; } It prints the number of records. the file() is clearly an array and foreach prints as expected. I wonder if it is something to do with the \n at the end of each line? I could easily loop and manually build the new array but.... It seems so simple. Link to comment https://forums.phpfreaks.com/topic/182312-solved-add-to-array-hassle/ Share on other sites More sharing options...
mikesta707 Posted November 20, 2009 Share Posted November 20, 2009 http://php.net/manual/en/function.array-push.php array_push returns an int. you can just call it like so and it will work array_push($notify_list,"$user_em"); but since you are just pusing one element into the array, this would be slightly faster $notify_list[] = $user_em; since you don't have the overhead of calling a function Link to comment https://forums.phpfreaks.com/topic/182312-solved-add-to-array-hassle/#findComment-962040 Share on other sites More sharing options...
arfa Posted November 21, 2009 Author Share Posted November 21, 2009 And on reading the manual (again) more closely I do indeed see: array_push "Returns the new number of elements in the array." Thanks Link to comment https://forums.phpfreaks.com/topic/182312-solved-add-to-array-hassle/#findComment-962456 Share on other sites More sharing options...
jamesxg1 Posted November 21, 2009 Share Posted November 21, 2009 Try this, $notify_list = array(); $notify_list[file($filename)] = $user_em; foreach($notify_list as $key => $val) { echo "$val<BR>"; } James. Link to comment https://forums.phpfreaks.com/topic/182312-solved-add-to-array-hassle/#findComment-962458 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.