Home > Software engineering >  how can i write into the array using php
how can i write into the array using php

Time:09-24

basically I have to write in the $ logins array, I tried to solve with some method found on the internet but they don't work

<?php
$logins = array('Alex' => '123456','username1' => 'password1','username2' => 'password2'(i have to write there));
?>

CodePudding user response:

You can use array_push:

$array = array();
array_push($array, "user", "pwd");

But a problem that you can have later, is, that you don't save the data permanently. When you want to save it, you should use something like mysql.

See also: https://www.php.net/manual/en/function.array-push.php

CodePudding user response:

Try this custom function (write_to_logins_array)

<?php
    $logins = array('Alex' => '123456','username1' => 'password1','username2' => 'password2');
    echo '<pre>';
    echo 'Before<br>';
    print_r($logins);
    echo '</pre>';
    function write_to_logins_array($key_to_write, $written_text){
        global $logins; //pay special attention to this as you cannot access $logins variable inside this function
        $value = $logins[$key_to_write].$written_text;
        $logins[$key_to_write] = $value;
    }
    write_to_logins_array('username2', '. You wrote somthing');
    echo '<pre>';
    echo 'After<br>';
    print_r($logins);
    echo '</pre>';
?>
  •  Tags:  
  • php
  • Related