unidox Posted July 9, 2008 Share Posted July 9, 2008 I have a file users.php and what I am trying to do is lets say I have the following code: <?php // some code here; // users array $GLOBALS["users"]=array( array("admin","password",1,1,1,"2",7,1), array("steve","password",1,1,1,"2",7,1), ); ?> what I am trying to do is make a script that will add the array to the array, without deleting the code on top. How would I do that? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/ Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 Do you mean.... <?php $newuser = array("foo","password",1,1,1,"2",7,1) $GLOBALS['users'][0] = array_merge($GLOBALS['users'][0],$newuser); ?> Or something similar? Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585931 Share on other sites More sharing options...
unidox Posted July 10, 2008 Author Share Posted July 10, 2008 No, the code I have was pasted, just changed the passwords and usernames. Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585934 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 Sorry, having a bad day. <?php $newuser = array("foo","password",1,1,1,"2",7,1) $GLOBALS['users'][] = $newuser; ?> Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585944 Share on other sites More sharing options...
unidox Posted July 10, 2008 Author Share Posted July 10, 2008 you still dont get it lol, here is my file. <?php // Code here. $GLOBALS["users"]=array( array("admin","password",1,2,1,"",7,1), array("steve","password",1,2,1,"",7,1), ); ?> That code works, what I am trying to do is make a form that adds something like this: <?php // Code here. $GLOBALS["users"]=array( array("admin","password",1,2,1,"",7,1), array("steve","password",1,2,1,"",7,1), array("martin","password",1,2,1,"",7,1), ); ?> Just add another array. Get it? Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585953 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 I get it. Thats exactly what my code does. Run this example. <?php // some code here; // users array $GLOBALS["users"]=array( array("admin","password",1,1,1,"2",7,1), array("steve","password",1,1,1,"2",7,1) ); echo '<pre>'; print_r($GLOBALS['users']); echo '</pre>'; $newuser = array("foo","password",1,1,1,"2",7,1); $GLOBALS['users'][] = $newuser; echo '<pre>'; print_r($GLOBALS['users']); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585954 Share on other sites More sharing options...
unidox Posted July 10, 2008 Author Share Posted July 10, 2008 But the thing is I am adding a user from adduser.php and need to save the file users.php. Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585968 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 So you want to actually add code dynamically to a script? Why is that so hard to explain? This type of thing could get pretty messing in php. If I was going to do it I would use sed, but anyway. The easiest method would likely be to place a placeholder in your script, much as you would in a templating system. eg; $GLOBALS["users"]=array( array("admin","password",1,1,1,"2",7,1), array("steve","password",1,1,1,"2",7,1), //<newuser> ); Then you could simply do something like.... <?php $newuser = "array(\"martin\",\"password\",1,2,1,\"\",7,1),\n//<newuser>"; $file = file_get_contents('config.php'); $file = str_replace('//<newuser>',$newuser,$file); file_put_contents('config.php',$file); ?> The whole idea seems pretty messy to me though. Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585969 Share on other sites More sharing options...
ShimmyShine Posted July 10, 2008 Share Posted July 10, 2008 Thorpe that would work for 1 new registry only, then it would erase the //newuser and not write another one. Wouldn't you have to make it write another //newuser after it wrote that current array? ShimmyShine Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585976 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 Wouldn't you have to make it write another //newuser after it wrote that current array? Yes, exactly as my code does. $newuser = "array(\"martin\",\"password\",1,2,1,\"\",7,1),\n//<newuser>"; Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585978 Share on other sites More sharing options...
unidox Posted July 10, 2008 Author Share Posted July 10, 2008 Ok, is there anyway I can update one of those arrays? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585986 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 You mean like change the username or something? Once again, you could try using str_replace() to overide an existing username, updating those numbers is going to get pretty messy however. Can I ask why your not storing this stuff within a database? They are alot easier to deal with than text files. Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585993 Share on other sites More sharing options...
unidox Posted July 10, 2008 Author Share Posted July 10, 2008 Well I just want to change 1 digit from 1 array, but other arrays might have a 1 also, so is there anyway to edit a line as a whole? Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585998 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 so is there anyway to edit a line as a whole? Yes, using str_replace(). Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-585999 Share on other sites More sharing options...
ShimmyShine Posted July 10, 2008 Share Posted July 10, 2008 Sorry thorpe, didn't notice that part of the line Shimmy Quote Link to comment https://forums.phpfreaks.com/topic/114005-solved-how/#findComment-586008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.