This is array in external php file
Array
(
[0] => Array
(
[CurrencyAbbreviation] => AUD
[CurrencyRate] => 0.50600000
[DateOfCurrencyRate] => 2013-07-11
[Units] => 1
)
)
Want to call for example CurrencyRate
Tried code like $('#currency_load').html(data.CurrencyRate); (Chrome F12 get 404 error).
For comparison. If php array is like this
$arr = array ('item1'=>"I love jquery4u",'item2'=>"You love jQuery4u",'item3'=>"We love jQuery4u");
and json $('#currency_load').html(data.item1);
then all works.
Do I need somehow to modify php array?
Related
I'm working on a Laravel project that implements react-jsonschema-form and I need to convert the values saved in the database to an associative array so I can pluck certain values from it. However I am getting strange results when doing so.
Here is the code I use to grab the JSON data from the table and then convert to an array:
$form = Form::where('id', $formId)->get();
$converted = json_decode($form[0]->form_data, true);
$formArray = print_r($converted, 1);
return $formArray;
For testing purposes I am simply rendering the data in the browser.
The result from the above return is:
Array
(
[1] => Array
(
[1.1] => New
[1.2] => Ms
[1.3] => Isobel Fleming
[1.4] => Array
(
[uprn] => 52375918
[address_1] => Fake Street
[address_2] =>
[address_3] =>
[town_city] => BRISTOL
[postcode] => BS1 3KE
)
[1.5] => 0129711011
[1.6] => 0800999111
[1.7] => 0781100022
[1.8] => isobelfleming#jourrapide.com
)
)
which is great. However when I try and access anything from it like:
return $formData[1][1.1]
I get:
String offset cast occurred
If I try using a string:
return $formData[1]['1.1']
I get:
Illegal string offset '1.1'
So I am not sure what to do to access this data. The problem is, although it's not ideal to have the associate keys with decimals in them, this is the way the schema is set up and it's several thousand lines long - this is just a snippet of the form data.
Is there anything that can be done in order to get the data from this array?
I figured it out. It seems like the following line was the problem:
print_r($converted, 1);
It didn't need to be printed as an array, I should have just used the $converted variable to access the data like so:
return $converted[1]['1.1'];
I need help pulling data from the field with unknown format of data structure. I am using a wordpress quiz plugin and i want to pull data from its backend table.
Data stored in answer_data is:
a:4:{
i:0;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:17:"Kieran Trippier ";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}
i:1;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:11:"Hugo Lloris";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}
i:2;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:14:"Moussa Dembele";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}
i:3;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:14:"Jan Vertonghen";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:1;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}
}
while looking at the structure of the table, data type of answer_data is longtext but has utf8_general_ci alongside it. I dont know what this means.
From this data i want to pull, quiz answers i.e.Kieran Trippier,Hugo Lloris,Moussa Dembele and Jan Vertonghen.
Any help or hint will be very much appreciated.
EDIT 1:
Array (
[0] => WpProQuiz_Model_AnswerTypes Object ( [_answer:protected] => Kieran Trippier [_html:protected] => [_points:protected] => 1 [_correct:protected] => [_sortString:protected] => [_sortStringHtml:protected] => [_mapper:protected] => )
[1] => WpProQuiz_Model_AnswerTypes Object ( [_answer:protected] => Hugo Lloris [_html:protected] => [_points:protected] => 1 [_correct:protected] => [_sortString:protected] => [_sortStringHtml:protected] => [_mapper:protected] => )
[2] => WpProQuiz_Model_AnswerTypes Object ( [_answer:protected] => Moussa Dembele [_html:protected] => [_points:protected] => 1 [_correct:protected] => [_sortString:protected] => [_sortStringHtml:protected] => [_mapper:protected] => )
[3] => WpProQuiz_Model_AnswerTypes Object ( [_answer:protected] => Jan Vertonghen [_html:protected] => [_points:protected] => 1 [_correct:protected] => 1 [_sortString:protected] => [_sortStringHtml:protected] => [_mapper:protected] => ) )
how to get values from this array?
That is some form of serialization. It seems to be an array a of 4 elements, each of which is further structure. The plugin understands; you probably don't need to understand it.
utf8_general_ci is the "Collation" to be used for comparing the LONGTEXT strings. It implies CHARACTER SET utf8, which is the 3-byte subset of UTF-8 (aka, MySQL's utf8mb4). This allows you to include characters from most languages around the world.
One would hope that the plugin provides a way to dissect this structure, rather than leaving you guessing. Furthermore, the plugin could change the structure without notice.
Here's the hint: See PHP's serialize() and unserialize().
Using the unserialized result:
$foo seems to be an array of 'objects' of class WpProQuiz_Model_AnswerTypes. One of the 'properties' of that object seems to be $_answer. So, see if this gives you the list of answers:
foreach($foo as $obj) {
echo $obj->_answer, "\n";
}
Or, to grab the answers into an array $answers:
$answers = array();
foreach($foo as $obj) {
$answers[] = $obj->_answer;
}
Basiclly I have an url that have a parameter.
So, I need to access those parameter using GET. This is the GET that coming:
Array
(
[r] => hanwa/incoming-pipe/assign-incoming
[1] => Array
(
[min_urut] => 1
[max_urut] => 44
)
[_] => 1496645704980
)
In docs, docs,
We can use like this :
echo $request->get('min_urut');
But, I got nothing. Please Advise.
Use Concept of ArrayHelper class :-> http://www.yiiframework.com/doc-2.0/guide-helper-array.html#getting-values
ex:
$data = ArrayHelper::getValue($request->get(), 'Temp.yourvalue.yourindex');
Main use of $request->get() method mainly returns to you a value from $_GET,
so example is
$temp = $request->get('Temp'); // Here $temp variable contains $_GET['Temp']
$data = $temp['yourvaluename'][0];
Judging by the fact that you have an array with the key [r] you have an associative array, method get() you will not get it, you need to do it just like this ..
echo $request->get()[1]['min_urut'];
I am trying to understand how a Wordpress plugin works with data, when I pull it from MySQL it comes out like this:
a:1:{s:9:"home-team";a:6:{s:2:"id";s:9:"home-team";s:4:"slug";s:9:"home-team";s:4:"type";s:6:"select";s:4:"name";s:9:"Home Team";s:11:"description";s:0:"";s:4:"data";a:4:{s:7:"options";a:3:{s:60:"wpcf-fields-select-option-3892e2c3ad45e24dc7f47ff2ba880c33-2";a:2:{s:5:"title";s:13:"Chicago Bears";s:5:"value";s:1:"1";}s:60:"wpcf-fields-select-option-09fbd82bfa4142df6439c8e15d96dbfc-1";a:2:{s:5:"title";s:15:"New York Giants";s:5:"value";s:1:"2";}s:60:"wpcf-fields-select-option-7c7df972f933545b37c41ca249c686b4-1";a:2:{s:5:"title";s:15:"Oakland Raiders";s:5:"value";s:1:"3";}}s:8:"validate";a:1:{s:8:"required";a:3:{s:6:"active";s:1:"1";s:5:"value";s:4:"true";s:7:"message";s:22:"This Field is required";}}s:19:"conditional_display";a:2:{s:8:"relation";s:3:"AND";s:6:"custom";s:0:"";}s:16:"disabled_by_type";i:0;}}}
Is there a name for the way this is stored? To me it looks a little bit like JSON, but of course this is not JavaScript. Also, is there a way to clean it up easily (by using an online tool), so the first few lines would look like this:
a:1: {
s:9:"home-team";
a:6: {
s:2:"id";
s:9:"home-team";
s:4:"slug";
s:9:"home-team";
s:4:"type";
s:6:"select";
s:4:"name";
etc.. etc.. etc...
That's the PHP serialize format.
See : http://php.net/manual/en/function.serialize.php
Not sure how to get the formatted version exactly like you have it (but you could probably put that together easily), but here is another way to get an idea of what is in the serialized string:
$test_string= 'a:1:{s:9:"home-team";a:6:{s:2:"id";s:9:"home-team";s:4:"slug";s:9:"home-team";s:4:"type";s:6:"select";s:4:"name";s:9:"Home Team";s:11:"description";s:0:"";s:4:"data";a:4:{s:7:"options";a:3:{s:60:"wpcf-fields-select-option-3892e2c3ad45e24dc7f47ff2ba880c33-2";a:2:{s:5:"title";s:13:"Chicago Bears";s:5:"value";s:1:"1";}s:60:"wpcf-fields-select-option-09fbd82bfa4142df6439c8e15d96dbfc-1";a:2:{s:5:"title";s:15:"New York Giants";s:5:"value";s:1:"2";}s:60:"wpcf-fields-select-option-7c7df972f933545b37c41ca249c686b4-1";a:2:{s:5:"title";s:15:"Oakland Raiders";s:5:"value";s:1:"3";}}s:8:"validate";a:1:{s:8:"required";a:3:{s:6:"active";s:1:"1";s:5:"value";s:4:"true";s:7:"message";s:22:"This Field is required";}}s:19:"conditional_display";a:2:{s:8:"relation";s:3:"AND";s:6:"custom";s:0:"";}s:16:"disabled_by_type";i:0;}}}';
$unser = unserialize( $test_string);
print_r ( $unser );
Which will display:
Array
(
[home-team] => Array
(
[id] => home-team
[slug] => home-team
[type] => select
[name] => Home Team
[description] =>
[data] => Array
(
[options] => Array
(
[wpcf-fields-select-option-3892e2c3ad45e24dc7f47ff2ba880c33-2] => Array
(
[title] => Chicago Bears
[value] => 1
)
[wpcf-fields-select-option-09fbd82bfa4142df6439c8e15d96dbfc-1] => Array
(
[title] => New York Giants
[value] => 2
)
[wpcf-fields-select-option-7c7df972f933545b37c41ca249c686b4-1] => Array
(
[title] => Oakland Raiders
[value] => 3
)
)
[validate] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This Field is required
)
)
[conditional_display] => Array
(
[relation] => AND
[custom] =>
)
[disabled_by_type] => 0
)
)
)
That's the way WordPress stores arrays and objects in the database. From the article WordPress serializes options and meta for you
In the most basic use case, serialization is a way to store arrays and objects directly in the database, which can only store numbers, text, and dates. Serialization takes an array and turns it into a serialized string. For example:
$data = array( 'apple', 'banana', 'orange' );
echo serialize( $data );
// Result is a string we can unserialize into an array:
// a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"orange";}
WordPress has a few helper functions that we use instead of serialize() and unserialize() — maybe_serialize() and maybe_unserialize(). The first only serializes data that needs to be serialized — arrays and objects — and the second only unserializes data that is already serialized. (We have a lot of handy functions like these.)
You mention "when I pull it from MySQL it comes out like this", so you're probably not using the bundled functions to pull the data, like get_option(), get_post_meta() and get_user_meta(). Those functions take care of unserializing the data.
Worth noting that you should use a tool like WordPress (and others) Search and Replace Tool to search/replace inside the database. As it takes care of replacing strings inside serialized data.
You cannot simply change:
a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"orange";}
to
a:3:{i:0;s:5:"grapefruit";i:1;s:6:"banana";i:2;s:6:"orange";}
Because it should be s:10:"grapefruit";, being 10 the number of characters in the string.
How do you use cakephp to count, for example the number of posts, made every month in a year?
Preferably using Model->find('count') and get the data in an array.
I just did something similar, using only CakePHP (no direct queries). It works in CakePHP 2, haven't tested in 1.x.
The code for your example would be something like this:
$params = array(
'recursive' => -1,
'fields' => array('id', 'MONTH(created)')
'group' => array('YEAR(created)', 'MONTH(created)')
);
$numberOfPosts = $this->Model->find('count', $params);
This comes close
Query
$data = $this->Post->query("SELECT COUNT(id),MONTH(created) FROM posts GROUP BY YEAR(created), MONTH(created);");
Return
Array
(
[0] => Array
(
[0] => Array
(
[COUNT(id)] => 1
[MONTH(created)] => 3
)
)
[1] => Array
(
[0] => Array
(
[COUNT(id)] => 2
[MONTH(created)] => 4
)
)
)
When using cake, I prefer to stay as close to the framework as possible. This means that I try to avoid writing queries directly in the controllers because this results in the model code being everywhere. Therefore I recommend one of two solutions
1: (and what I do with more complicated stuff): Create a view for the calculation that you want to do and create a model to match.
2: Use a query as mentioned before, but put it in the model class, not the application class.