Serialized data list to foreach
How can I list the serialized data retrieved from the database. My code is failing.
PHP 5.6
My data
a:3:{i:0;a:4:{s:2:"no";i:2;s:4:"name";s:23:"file-417-1546062916.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:1;a:4:{s:2:"no";i:1;s:4:"name";s:23:"file-417-1546063067.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:2;a:4:{s:2:"no";i:3;s:4:"name";s:23:"file-417-1546063154.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}}
My code
$fileStr = $getMeta('post_files', $pID);
$list = ($fileStr) ? unserialize($fileStr) : '';
foreach ($list as $k => $v) {
echo $k.' - '.$v;
}
Notice: Array to string conversion in ..........
0 - Array
php serialization multidimensional-array foreach
add a comment |
How can I list the serialized data retrieved from the database. My code is failing.
PHP 5.6
My data
a:3:{i:0;a:4:{s:2:"no";i:2;s:4:"name";s:23:"file-417-1546062916.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:1;a:4:{s:2:"no";i:1;s:4:"name";s:23:"file-417-1546063067.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:2;a:4:{s:2:"no";i:3;s:4:"name";s:23:"file-417-1546063154.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}}
My code
$fileStr = $getMeta('post_files', $pID);
$list = ($fileStr) ? unserialize($fileStr) : '';
foreach ($list as $k => $v) {
echo $k.' - '.$v;
}
Notice: Array to string conversion in ..........
0 - Array
php serialization multidimensional-array foreach
Did you look at what the array looks like? Did you try a var_dump/print_r?
– Andreas
Dec 29 '18 at 6:28
Feel free to accept my answer if it helped you.
– Andreas
Dec 29 '18 at 7:10
add a comment |
How can I list the serialized data retrieved from the database. My code is failing.
PHP 5.6
My data
a:3:{i:0;a:4:{s:2:"no";i:2;s:4:"name";s:23:"file-417-1546062916.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:1;a:4:{s:2:"no";i:1;s:4:"name";s:23:"file-417-1546063067.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:2;a:4:{s:2:"no";i:3;s:4:"name";s:23:"file-417-1546063154.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}}
My code
$fileStr = $getMeta('post_files', $pID);
$list = ($fileStr) ? unserialize($fileStr) : '';
foreach ($list as $k => $v) {
echo $k.' - '.$v;
}
Notice: Array to string conversion in ..........
0 - Array
php serialization multidimensional-array foreach
How can I list the serialized data retrieved from the database. My code is failing.
PHP 5.6
My data
a:3:{i:0;a:4:{s:2:"no";i:2;s:4:"name";s:23:"file-417-1546062916.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:1;a:4:{s:2:"no";i:1;s:4:"name";s:23:"file-417-1546063067.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:2;a:4:{s:2:"no";i:3;s:4:"name";s:23:"file-417-1546063154.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}}
My code
$fileStr = $getMeta('post_files', $pID);
$list = ($fileStr) ? unserialize($fileStr) : '';
foreach ($list as $k => $v) {
echo $k.' - '.$v;
}
Notice: Array to string conversion in ..........
0 - Array
php serialization multidimensional-array foreach
php serialization multidimensional-array foreach
edited Dec 29 '18 at 6:48
Andreas
15.4k31542
15.4k31542
asked Dec 29 '18 at 6:24
MuratMurat
31
31
Did you look at what the array looks like? Did you try a var_dump/print_r?
– Andreas
Dec 29 '18 at 6:28
Feel free to accept my answer if it helped you.
– Andreas
Dec 29 '18 at 7:10
add a comment |
Did you look at what the array looks like? Did you try a var_dump/print_r?
– Andreas
Dec 29 '18 at 6:28
Feel free to accept my answer if it helped you.
– Andreas
Dec 29 '18 at 7:10
Did you look at what the array looks like? Did you try a var_dump/print_r?
– Andreas
Dec 29 '18 at 6:28
Did you look at what the array looks like? Did you try a var_dump/print_r?
– Andreas
Dec 29 '18 at 6:28
Feel free to accept my answer if it helped you.
– Andreas
Dec 29 '18 at 7:10
Feel free to accept my answer if it helped you.
– Andreas
Dec 29 '18 at 7:10
add a comment |
1 Answer
1
active
oldest
votes
You have a multidimensional array which means you need two foreach nested.
$str = 'a:3:{i:0;a:4:{s:2:"no";i:2;s:4:"name";s:23:"file-417-1546062916.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:1;a:4:{s:2:"no";i:1;s:4:"name";s:23:"file-417-1546063067.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:2;a:4:{s:2:"no";i:3;s:4:"name";s:23:"file-417-1546063154.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}}';
$list = unserialize($str);
if(!empty($list)){ // check if the array/string is empty
foreach ($list as $k => $v) {
echo $k . "n";
foreach($v as $k2 => $val){
echo $k2.' - '.$val . "n";
}
echo "nn";
}
}
Output:
0
no - 2
name - file-417-1546062916.zip
type - application/x-zip-compressed
size - 31835501
1
no - 1
name - file-417-1546063067.zip
type - application/x-zip-compressed
size - 31835501
2
no - 3
name - file-417-1546063154.zip
type - application/x-zip-compressed
size - 31835501
https://3v4l.org/itGpB
To see what the array looks like you can use print_r/var_dump or var_export.
They will show how many levels deep your array is, if it's a unknown number of levels then you can use array_walk_recursive to walk the array.
its works, thanks :)
– Murat
Dec 29 '18 at 6:37
@Murat :-) . I see that you use a ternary to see if you want to unserialize or not. Unserialize will fail silently and not create any problems. Foreach however will not fail silently. I would remove the ternary and add a if empty as this: 3v4l.org/p2hUn that way if the string is empty no problems.
– Andreas
Dec 29 '18 at 6:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967237%2fserialized-data-list-to-foreach%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have a multidimensional array which means you need two foreach nested.
$str = 'a:3:{i:0;a:4:{s:2:"no";i:2;s:4:"name";s:23:"file-417-1546062916.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:1;a:4:{s:2:"no";i:1;s:4:"name";s:23:"file-417-1546063067.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:2;a:4:{s:2:"no";i:3;s:4:"name";s:23:"file-417-1546063154.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}}';
$list = unserialize($str);
if(!empty($list)){ // check if the array/string is empty
foreach ($list as $k => $v) {
echo $k . "n";
foreach($v as $k2 => $val){
echo $k2.' - '.$val . "n";
}
echo "nn";
}
}
Output:
0
no - 2
name - file-417-1546062916.zip
type - application/x-zip-compressed
size - 31835501
1
no - 1
name - file-417-1546063067.zip
type - application/x-zip-compressed
size - 31835501
2
no - 3
name - file-417-1546063154.zip
type - application/x-zip-compressed
size - 31835501
https://3v4l.org/itGpB
To see what the array looks like you can use print_r/var_dump or var_export.
They will show how many levels deep your array is, if it's a unknown number of levels then you can use array_walk_recursive to walk the array.
its works, thanks :)
– Murat
Dec 29 '18 at 6:37
@Murat :-) . I see that you use a ternary to see if you want to unserialize or not. Unserialize will fail silently and not create any problems. Foreach however will not fail silently. I would remove the ternary and add a if empty as this: 3v4l.org/p2hUn that way if the string is empty no problems.
– Andreas
Dec 29 '18 at 6:40
add a comment |
You have a multidimensional array which means you need two foreach nested.
$str = 'a:3:{i:0;a:4:{s:2:"no";i:2;s:4:"name";s:23:"file-417-1546062916.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:1;a:4:{s:2:"no";i:1;s:4:"name";s:23:"file-417-1546063067.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:2;a:4:{s:2:"no";i:3;s:4:"name";s:23:"file-417-1546063154.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}}';
$list = unserialize($str);
if(!empty($list)){ // check if the array/string is empty
foreach ($list as $k => $v) {
echo $k . "n";
foreach($v as $k2 => $val){
echo $k2.' - '.$val . "n";
}
echo "nn";
}
}
Output:
0
no - 2
name - file-417-1546062916.zip
type - application/x-zip-compressed
size - 31835501
1
no - 1
name - file-417-1546063067.zip
type - application/x-zip-compressed
size - 31835501
2
no - 3
name - file-417-1546063154.zip
type - application/x-zip-compressed
size - 31835501
https://3v4l.org/itGpB
To see what the array looks like you can use print_r/var_dump or var_export.
They will show how many levels deep your array is, if it's a unknown number of levels then you can use array_walk_recursive to walk the array.
its works, thanks :)
– Murat
Dec 29 '18 at 6:37
@Murat :-) . I see that you use a ternary to see if you want to unserialize or not. Unserialize will fail silently and not create any problems. Foreach however will not fail silently. I would remove the ternary and add a if empty as this: 3v4l.org/p2hUn that way if the string is empty no problems.
– Andreas
Dec 29 '18 at 6:40
add a comment |
You have a multidimensional array which means you need two foreach nested.
$str = 'a:3:{i:0;a:4:{s:2:"no";i:2;s:4:"name";s:23:"file-417-1546062916.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:1;a:4:{s:2:"no";i:1;s:4:"name";s:23:"file-417-1546063067.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:2;a:4:{s:2:"no";i:3;s:4:"name";s:23:"file-417-1546063154.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}}';
$list = unserialize($str);
if(!empty($list)){ // check if the array/string is empty
foreach ($list as $k => $v) {
echo $k . "n";
foreach($v as $k2 => $val){
echo $k2.' - '.$val . "n";
}
echo "nn";
}
}
Output:
0
no - 2
name - file-417-1546062916.zip
type - application/x-zip-compressed
size - 31835501
1
no - 1
name - file-417-1546063067.zip
type - application/x-zip-compressed
size - 31835501
2
no - 3
name - file-417-1546063154.zip
type - application/x-zip-compressed
size - 31835501
https://3v4l.org/itGpB
To see what the array looks like you can use print_r/var_dump or var_export.
They will show how many levels deep your array is, if it's a unknown number of levels then you can use array_walk_recursive to walk the array.
You have a multidimensional array which means you need two foreach nested.
$str = 'a:3:{i:0;a:4:{s:2:"no";i:2;s:4:"name";s:23:"file-417-1546062916.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:1;a:4:{s:2:"no";i:1;s:4:"name";s:23:"file-417-1546063067.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}i:2;a:4:{s:2:"no";i:3;s:4:"name";s:23:"file-417-1546063154.zip";s:4:"type";s:28:"application/x-zip-compressed";s:4:"size";i:31835501;}}';
$list = unserialize($str);
if(!empty($list)){ // check if the array/string is empty
foreach ($list as $k => $v) {
echo $k . "n";
foreach($v as $k2 => $val){
echo $k2.' - '.$val . "n";
}
echo "nn";
}
}
Output:
0
no - 2
name - file-417-1546062916.zip
type - application/x-zip-compressed
size - 31835501
1
no - 1
name - file-417-1546063067.zip
type - application/x-zip-compressed
size - 31835501
2
no - 3
name - file-417-1546063154.zip
type - application/x-zip-compressed
size - 31835501
https://3v4l.org/itGpB
To see what the array looks like you can use print_r/var_dump or var_export.
They will show how many levels deep your array is, if it's a unknown number of levels then you can use array_walk_recursive to walk the array.
edited Dec 29 '18 at 6:43
answered Dec 29 '18 at 6:32
AndreasAndreas
15.4k31542
15.4k31542
its works, thanks :)
– Murat
Dec 29 '18 at 6:37
@Murat :-) . I see that you use a ternary to see if you want to unserialize or not. Unserialize will fail silently and not create any problems. Foreach however will not fail silently. I would remove the ternary and add a if empty as this: 3v4l.org/p2hUn that way if the string is empty no problems.
– Andreas
Dec 29 '18 at 6:40
add a comment |
its works, thanks :)
– Murat
Dec 29 '18 at 6:37
@Murat :-) . I see that you use a ternary to see if you want to unserialize or not. Unserialize will fail silently and not create any problems. Foreach however will not fail silently. I would remove the ternary and add a if empty as this: 3v4l.org/p2hUn that way if the string is empty no problems.
– Andreas
Dec 29 '18 at 6:40
its works, thanks :)
– Murat
Dec 29 '18 at 6:37
its works, thanks :)
– Murat
Dec 29 '18 at 6:37
@Murat :-) . I see that you use a ternary to see if you want to unserialize or not. Unserialize will fail silently and not create any problems. Foreach however will not fail silently. I would remove the ternary and add a if empty as this: 3v4l.org/p2hUn that way if the string is empty no problems.
– Andreas
Dec 29 '18 at 6:40
@Murat :-) . I see that you use a ternary to see if you want to unserialize or not. Unserialize will fail silently and not create any problems. Foreach however will not fail silently. I would remove the ternary and add a if empty as this: 3v4l.org/p2hUn that way if the string is empty no problems.
– Andreas
Dec 29 '18 at 6:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967237%2fserialized-data-list-to-foreach%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Did you look at what the array looks like? Did you try a var_dump/print_r?
– Andreas
Dec 29 '18 at 6:28
Feel free to accept my answer if it helped you.
– Andreas
Dec 29 '18 at 7:10