I have a registration form which saves user data to a text file via the serializable method.
a:1:{i:0;s:44:"{"name":"Mario","pw":"3214","email":"mo@mo"}";}
I can deserialize the data however I am having some trouble extracting the value "Mario" from the key - "name". Code can be seen below:
$array = file_get_contents('user.txt');
$artikel = unserialize($array);
foreach ($artikel as $item ){
echo $item['name'];
}
The error I recieve is Illegal string offset 'name'. Any help would be greatly appreciated.
You have a serialized array of JSON strings.
So try like this
//$array = file_get_contents('user.txt');
$artikel = unserialize('a:1:{i:0;s:44:"{"name":"Mario","pw":"3214","email":"mo@mo"}";}');
//print_r($artikel);
foreach ($artikel as $item ){
$json = json_decode($item);
echo $json->name;
}
PS: Storing a password in plain text is very bad security, storing that in a simple file is even worse security, so I hope system this will never actually be used. PHP provides
password_hash()
andpassword_verify()
please use them.
You need to json_decode
after foreach
with true
as a second parameter then you will get array as you want..
<?php
$arr = 'a:1:{i:0;s:44:"{"name":"Mario","pw":"3214","email":"mo@mo"}";}';
$artikel = unserialize($arr);
foreach ($artikel as $item ){
$response = json_decode($item, true);
echo $response['name'];
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.