Reputation: 17
I am beginner at wordpress world. I am having difficulty to retrieve the intended value from following data.
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => MCQ Botany 101
[settings] => a:24:{s:9:"limit_one";s:2:"no";s:12:"limit_one_wp";s:2:"no";s:16:"limit_one_cookie";s:2:"no";s:11:"save_resume";s:2:"no";s:16:"question_numbers";s:3:"yes";s:5:"timer";s:4:"5000";s:9:"pass_mark";s:2:"80";s:17:"show_progress_bar";s:3:"yes";s:20:"automark_whenfreetxt";s:2:"no";s:14:"finish_display";s:11:"Quiz Review";s:6:"status";s:7:"enabled";s:9:"send_user";s:2:"no";s:7:"contact";s:2:"no";s:6:"use_wp";s:2:"no";s:16:"notificaton_type";s:7:"instant";s:14:"email_template";s:0:"";s:12:"pdf_template";s:0:"";s:7:"use_pdf";s:2:"no";s:13:"store_results";s:3:"yes";s:18:"notification_email";s:0:"";s:14:"finish_message";s:0:"";s:11:"pass_finish";s:2:"no";s:19:"pass_finish_message";s:0:"";s:11:"fail_review";s:3:"yes";}
[type] => quiz
[timestamp] => 2016-07-10 21:02:44
)
)
I want 5000 from s:4:"5000"
. Any help will be highly appreciated.
Upvotes: 1
Views: 28
Reputation: 11680
You have an array, which has an object as a first key. You want to get a 'settings' value from this object, but it's a serialized string. So you need to unserialize it first.
Assuming that $myArr
is the given array you'll do something like this:
$settingsArr = unserialize($myArr[0]->settings);
$settingsArr['timer'];
Because the timer
key holds the 5000
value.
You can recreate this in php sandbox:
<?php
$myObj = new stdClass;
$myObj->id = 1;
$myObj->name = 'MCQ Botany 101';
$myObj->settings = 'a:24:{s:9:"limit_one";s:2:"no";s:12:"limit_one_wp";s:2:"no";s:16:"limit_one_cookie";s:2:"no";s:11:"save_resume";s:2:"no";s:16:"question_numbers";s:3:"yes";s:5:"timer";s:4:"5000";s:9:"pass_mark";s:2:"80";s:17:"show_progress_bar";s:3:"yes";s:20:"automark_whenfreetxt";s:2:"no";s:14:"finish_display";s:11:"Quiz Review";s:6:"status";s:7:"enabled";s:9:"send_user";s:2:"no";s:7:"contact";s:2:"no";s:6:"use_wp";s:2:"no";s:16:"notificaton_type";s:7:"instant";s:14:"email_template";s:0:"";s:12:"pdf_template";s:0:"";s:7:"use_pdf";s:2:"no";s:13:"store_results";s:3:"yes";s:18:"notification_email";s:0:"";s:14:"finish_message";s:0:"";s:11:"pass_finish";s:2:"no";s:19:"pass_finish_message";s:0:"";s:11:"fail_review";s:3:"yes";}';
$myObj->type = 'quiz';
$myObj->timestamp = '2016-07-10 21:02:44';
$myArr = array(
'0' => $myObj
);
$settingsArr = unserialize($myArr[0]->settings);
print_r($settingsArr['timer']);
You can also see the contents of the settings array
print_r($settingsArr);
Array
(
[limit_one] => no
[limit_one_wp] => no
[limit_one_cookie] => no
[save_resume] => no
[question_numbers] => yes
[timer] => 5000
[pass_mark] => 80
[show_progress_bar] => yes
[automark_whenfreetxt] => no
[finish_display] => Quiz Review
[status] => enabled
[send_user] => no
[contact] => no
[use_wp] => no
[notificaton_type] => instant
[email_template] =>
[pdf_template] =>
[use_pdf] => no
[store_results] => yes
[notification_email] =>
[finish_message] =>
[pass_finish] => no
[pass_finish_message] =>
[fail_review] => yes
)
Upvotes: 1
Reputation: 635
I know what you mean. It is hard to find out how these serialized string called, if you never worked with them.
Please look into:
For More info visit the PHP Manual: http://php.net/manual/en/function.serialize.php
I think that should help you to work unserialize and serialize them again.
Upvotes: 0