I am using oxford dictionary API to develop a dictionary function on my website. I have successfully implemented the function as I can search the word and pull the results from the API. However, I have a problem with saving these data into MySQL database.
This is what I get when I print_r($dic_array):
Array
(
[metadata] => Array
(
[provider] => Oxford University Press
)
[results] => Array
(
[0] => Array
(
[id] => love
[language] => en
[lexicalEntries] => Array
(
[0] => Array
(
[entries] => Array
(
[0] => Array
(
[etymologies] => Array
(
[0] => Old English lufu, of Germanic origin; from an Indo-European root shared by Sanskrit lubhyati ‘desires’, Latin libet ‘it is pleasing’, libido ‘desire’, also by leave and lief
)
[grammaticalFeatures] => Array
(
[0] => Array
(
[text] => Mass
[type] => Countability
)
)
[homographNumber] => 000
[senses] => Array
(
[0] => Array
(
[definitions] => Array
(
[0] => a strong feeling of affection
)
[examples] => Array
(
[0] => Array
(
[text] => their love for their country
)
[1] => Array
(
[text] => babies fill parents with intense feelings of love
)
)
[id] => m_en_gbus0596690.007
[subsenses] => Array
(
[0] => Array
(
[definitions] => Array
(
[0] => a strong feeling of affection and sexual attraction for someone
)
[examples] => Array
(
[0] => Array
(
[text] => they were both in love with her
)
[1] => Array
(
[text] => we were slowly falling in love
)
)
[id] => m_en_gbus0596690.009
)
[1] => Array
(
[definitions] => Array
(
[0] => affectionate greetings conveyed to someone on one's behalf
)
[examples] => Array
(
[0] => Array
(
[text] => give her my love
)
)
[id] => m_en_gbus0596690.010
)
[2] => Array
(
[definitions] => Array
(
[0] => a formula for ending an affectionate letter
)
[examples] => Array
(
[0] => Array
(
[text] => take care, lots of love, Judy
)
)
[id] => m_en_gbus0596690.011
)
[3] => Array
(
[definitions] => Array
(
[0] => a personified figure of love, often represented as Cupid.
)
[domains] => Array
(
[0] => Roman History
)
[id] => m_en_gbus0596690.012
[variantForms] => Array
(
[0] => Array
(
[text] => Love
)
)
)
)
)
[1] => Array
(
[definitions] => Array
(
[0] => a great interest and pleasure in something
)
[examples] => Array
(
[0] => Array
(
[text] => his love for football
)
[1] => Array
(
[text] => we share a love of music
)
)
[id] => m_en_gbus0596690.016
)
[2] => Array
(
[definitions] => Array
(
[0] => a person or thing that one loves
)
[examples] => Array
(
[0] => Array
(
[text] => she was the love of his life
)
[1] => Array
(
[text] => their two great loves are tobacco and whisky
)
)
[id] => m_en_gbus0596690.018
[notes] => Array
(
[0] => Array
(
[text] => count noun
[type] => grammaticalNote
)
)
[subsenses] => Array
(
[0] => Array
(
[definitions] => Array
(
[0] => a friendly form of address
)
[examples] => Array
(
[0] => Array
(
[text] => it's all right, love
)
)
[id] => m_en_gbus0596690.021
[regions] => Array
(
[0] => British
)
[registers] => Array
(
[0] => informal
)
)
[1] => Array
(
[definitions] => Array
(
[0] => used in affectionate requests
)
[examples] => Array
(
[0] => Array
(
[text] => don't fret, there's a love
)
)
[id] => m_en_gbus0596690.022
[notes] => Array
(
[0] => Array
(
[text] => "a love"
[type] => wordFormNote
)
)
[registers] => Array
(
[0] => informal
)
)
)
)
[3] => Array
(
[definitions] => Array
(
[0] => (in tennis, squash, and some other sports) a score of zero; nil
)
[domains] => Array
(
[0] => Tennis
)
[examples] => Array
(
[0] => Array
(
[text] => love fifteen
)
)
[id] => m_en_gbus0596690.024
)
)
)
)
[language] => en
[lexicalCategory] => Noun
[pronunciations] => Array
(
[0] => Array
(
[audioFile] => http://audio.oxforddictionaries.com/en/mp3/love_gb_1.mp3
[dialects] => Array
(
[0] => British English
)
[phoneticNotation] => IPA
[phoneticSpelling] => lʌv
)
)
[text] => love
)
[1] => Array
(
[entries] => Array
(
[0] => Array
(
[grammaticalFeatures] => Array
(
[0] => Array
(
[text] => Transitive
[type] => Subcategorization
)
)
[homographNumber] => 001
[senses] => Array
(
[0] => Array
(
[definitions] => Array
(
[0] => feel deep affection or sexual love for (someone)
)
[examples] => Array
(
[0] => Array
(
[text] => do you love me?
)
)
[id] => m_en_gbus0596690.026
[subsenses] => Array
(
[0] => Array
(
[definitions] => Array
(
[0] => like or enjoy very much
)
[examples] => Array
(
[0] => Array
(
[text] => I just love dancing
)
[1] => Array
(
[text] => I'd love a cup of tea
)
)
[id] => m_en_gbus0596690.032
)
)
)
)
)
)
[language] => en
[lexicalCategory] => Verb
[pronunciations] => Array
(
[0] => Array
(
[audioFile] => http://audio.oxforddictionaries.com/en/mp3/love_gb_1.mp3
[dialects] => Array
(
[0] => British English
)
[phoneticNotation] => IPA
[phoneticSpelling] => lʌv
)
)
[text] => love
)
)
[type] => headword
[word] => love
)
)
)
To echo out the result on my website:
foreach($dic_array['results'][0]['lexicalEntries'] as $word) {
echo '<hr><div class="partOfSpeech"><p><b>'.$word['lexicalCategory'].'</b></p></div>';
foreach($word['entries'][0]['senses'] as $definition) {
echo '<p class="definition">'.$definition['definitions'][0].'</p>';
if (!empty($definition['subsenses'])) {
foreach($definition['subsenses'] as $subsenses) {
echo '<p class="subDefinition" style="padding-left: 10px; "> -'.$subsenses['definitions'][0].'</p>';
}
}
}
}
This is not an answer. I am the author who asks this question. Because I cannot write over 30000 characters, I use some extra content here.
This is what I tried:
foreach($dic_array['results'][0]['lexicalEntries'] as $word) {
$lexicalCategory = serialize($word['lexicalCategory']);
foreach($word['entries'][0]['senses'] as $definition) {
$wordDefinition = serialize($definition['definitions'][0]);
if (!empty($definition['subsenses'])) {
foreach($definition['subsenses'] as $subsenses) {
$wordSubdefinition = serialize($subsenses['definitions'][0]);
if(!empty($_POST['save'])) {
$sth = $this->db->prepare('INSERT INTO dictionary
(`word`, `partOfSpeech`, `definition`, `subDefinition`)
VALUES (:word, :partOfSpeech, :definition, :subDefinition)
');
$sth->execute(array(
':word' => $data['word'],
':partOfSpeech' => $lexicalCategory,
':definition' => $wordDefinition,
':subDefinition' => $wordSubdefinition
));
}
}
}
}
}
I know it is too nested but once I can get this done, I will separate this into several functions.
Related
Array
(
[3M] => Array
(
[0] => Array
(
[name] => 3M
[price] => 158.15
)
[440] => Array
(
[name] => 3M
[price] => 156.69
)
)
[AO Smith] => Array
(
[1] => Array
(
[name] => AO Smith
[price] => 47.29
)
[441] => Array
(
[name] => AO Smith
[price] => 47.19
)
)
So I have an Array that is above^^^. I would like to get it into a condensed array format. I need a function that loops through the above and outputs it in the format below.
Array
(
[3M] => Array
(
[price1] => 158.15
[price2] => 156.69
)
[AO Smith] => Array
(
[price1] => 47.29
[price2] => 47.19
)
)
Above is how I would like the data oriented.
Thanks for the help.
What you'll find is the format you want is not good and not as usable or flexible. This however will give you a better format. name and price are descriptive, price1 and price2 are no different than 0 and 1:
foreach($array as $key => $values) {
$result[$key] = array_column($values, 'price');
}
Yields:
Array
(
[3M] => Array
(
[0] => 158.15
[1] => 156.69
)
[AO Smith] => Array
(
[0] => 47.29
[1] => 47.19
)
)
I am trying to get list of branches of the shipping company from their php API.The output from API I get is array which contains objects and I would like to convert these objects to arrays and save whole array to database. Example of the output is below. As I say I would like convert these objects into array and save it to database. Can you help me please? I tried some tips from stackoverflow but I have't fount anything yet. Thank you
Array
(
[email] => info#ulozenka.cz
[phone] => +420777208204
[hours] => UlozenkaLib\APIv3\Model\Branch\OpeningHours\OpeningHours Object
(
[regular:protected] => UlozenkaLib\APIv3\Model\Branch\OpeningHours\RegularOpeningHours Object
(
[monday:protected] => Array
(
[0] => UlozenkaLib\APIv3\Model\Branch\OpeningHours\Hours Object
(
[open:protected] => 10:00
[close:protected] => 16:00
)
)
)
[exceptions:protected] => Array
(
)
)
[links] => Array
(
[0] => UlozenkaLib\APIv3\Model\Link Object
(
[resourceName:protected] => website
[url:protected] => https://www.ulozenka.cz/pobocky/50020/jablonec-nad-nisou-machova-877-4-czc-cz
)
[1] => UlozenkaLib\APIv3\Model\Link Object
(
[resourceName:protected] => picture
[url:protected] => https://www.ulozenka.cz/cdn/images/branches/destination/50020.png
)
[2] => UlozenkaLib\APIv3\Model\Link Object
(
[resourceName:protected] => self
[url:protected] => https://api.ulozenka.cz/v3/branches/50020
)
)
)
So the converted array should be
Array
(
[email] => info#ulozenka.cz
[phone] => +420777208204
[hours] => array
(
[regular] => Array
(
[monday] => Array
(
[0] => Array
(
[open] => 10:00
[close] => 16:00
)
)
)
[exceptions] => Array()
)
[links] => Array
(
[0] => Array
(
[resourceName] => website
[url] => https://www.ulozenka.cz/pobocky/50020/jablonec-nad-nisou-machova-877-4-czc-cz
)
[1] => Array
(
[resourceName] => picture
[url] => https://www.ulozenka.cz/cdn/images/branches/destination/50020.png
)
[2] => Array
(
[resourceName] => self
[url] => https://api.ulozenka.cz/v3/branches/50020
)
)
)
The array is received from the facebook api and i am not able to extract the likes array from the array,Please help me
[data] => Array (
[0] => Array (
[message] => Hello
[id] => 729659027165160_729651713832558
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 729659027165160
)
)
[paging] => Array (
[cursors] => Array (
[after] => NzI5NjU5MDI3MTY1MTYw
[before] => NzI5NjU5MDI3MTY1MTYw
)
)
)
)
[1] => Array (
[id] => 729659027165160_718306454967084
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1719747118259908
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTcxOTc0NzExODI1OTkwOA==
[before] => MTcxOTc0NzExODI1OTkwOA==
)
)
)
)
[2] => Array (
[id] => 729659027165160_541135166017548
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[3] => Array (
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[id] => 729659027165160_520677651396633
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
[1] => Array (
[id] => 806391372817118
)
[2] => Array (
[id] => 928633297192567
)
[3] => Array (
[id] => 824812004311172
)
[4] => Array (
[id] => 10207344532684729
)
[5] => Array (
[id] => 1188171664544003
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE4ODE3MTY2NDU0NDAwMw==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[4] => Array (
[id] => 729659027165160_110578795739856
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyO
)
)
)
)
)
I am able to extract the id from the above array,but unable to extract the count of likes and message.
Try this code, it iterates in your array and stores all ids and stores the message, the likes Array and the number of likes only if they exist (Supposing that your array is named $myarray):
$result = array();
foreach($myarray['data'] as $data){
$item = array();
$item['id'] = $data['id'];
if( isset($data['message']) || isset($data['likes']) ){
if(isset($data['message'])) $item['message'] = $data['message'];
if(isset($data['likes'])) {
$item['likes'] = array();
foreach($data['likes']['data'] as $like){
$item['likes'][] = $like['id'];
}
$item['countlikes'] = count( $data['likes']['data'] );
}
}
$result[] = $item;
}
print_r($result);
With your example Array the result will be:
Array
(
[0] => Array
(
[id] => 729659027165160_729651713832558
[message] => Hello
[likes] => Array
(
[0] => 729659027165160
)
[countlikes] => 1
)
[1] => Array
(
[id] => 729659027165160_718306454967084
[likes] => Array
(
[0] => 1719747118259908
)
[countlikes] => 1
)
[2] => Array
(
[id] => 729659027165160_541135166017548
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
[3] => Array
(
[id] => 729659027165160_520677651396633
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[likes] => Array
(
[0] => 1162428970453842
[1] => 806391372817118
[2] => 928633297192567
[3] => 824812004311172
[4] => 10207344532684729
[5] => 1188171664544003
)
[countlikes] => 6
)
[4] => Array
(
[id] => 729659027165160_110578795739856
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
)
I have to post a form element inventory like following structure
[inventory] => Array
(
[0] => Array
(
[inventory_id] => Array
(
[0] => 1
)
[inventory_name] => Array
(
[0] => Bed 90*200
)
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
[1] => Array
(
[inventory_id] => Array
(
[0] => 15
)
[inventory_name] => Array
(
[0] => Bed 90*200
)
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
[2] => Array
(
[inventory_id] => Array
(
[0] => 15
)
[inventory_name] => Array
(
[0] => Bed 90*200
)
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
)
When I tried to assign this array to inventory in $client->setParameterPost(), I received POST values like this
[inventory] => Array
(
[0] => Array
(
[inventory_id] => Array
(
[0] => 1
)
)
[1] => Array
(
[inventory_name] => Array
(
[0] => Bed 90*200
)
)
[2] => Array
(
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
[3] => Array
(
[inventory_id] => Array
(
[0] => 15
)
)
[4] => Array
(
[inventory_name] => Array
(
[0] => Bed 90*200
)
)
[5] => Array
(
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
[6] => Array
(
[inventory_id] => Array
(
[0] => 15
)
)
[7] => Array
(
[inventory_name] => Array
(
[0] => Bed 90*200
)
)
[8] => Array
(
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
)
)
I have verified my array structure that is fine. I also checked in setParameter method in Client.php (Zend library), no issues. Just receiving this post. How can I achieve this?
Zend_Http_Client ignores integer keys of multi dimensional parameters. So in your case,
[inventory] => Array
(
[0] => Array
(
[inventory_id] => Array
(
[0] => 1
)
[inventory_name] => Array
(
[0] => Bed 90*200
)
[inventory_photo] => Array
(
[0] => 1_bed_90x200.jpg
)
)
will be translated to
inventory[][inventory_id][] => 1
inventory[][inventory_name][] => Bed 90*200
inventory[][inventory_photo][] => 1_bed_90x200.jpg
The solution to this problem is
either to use a class extended from Zend_Http_Client and override its method _flattenParametersArray().
or convert the params arrays to strings yourself, such as:
$client->setParameterPost('inventory[0][inventory_id][]', 1);
$client->setParameterPost('inventory[0][inventory_name][]', 'Bed 90*200');
$client->setParameterPost('inventory[0][inventory_photo][]', '1_bed_90x200.jpg');
im using cakephp 1.3. Im trying to sort an array using Set::sort() function but is not working.. any idea on how to do this? below is the array im using.
Array (
[0] => Array
(
[Group] => Array
(
[name] => Team A
)
[Members] => Array
(
[0] => Array
(
[name] => George
[Code] => Array
(
[name] => C
)
)
[1] => Array
(
[name] => Hall
[Code] => Array
(
[name] => A
)
)
[2] => Array
(
[name] => Mike
[Code] => Array
(
[name] => B
)
)
)
)
im sorting the array using this :
$data = Set::sort($data, '{n}.Members.{n}.Code.name', 'asc');
im expecting an output like this:
Array
(
[0] => Array
(
[Group] => Array
(
[name] => Team A
)
[Members] => Array
(
[0] => Array
(
[name] => Hall
[Code] => Array
(
[name] => A
)
)
[1] => Array
(
[name] => Mike
[Code] => Array
(
[name] => B
)
)
[2] => Array
(
[name] => George
[Code] => Array
(
[name] => C
)
)
)
)
The sorting does not take in effect.how can i do this? any idea?
Using only Set::sort() its no doable. You can you this:
$result = array();
foreach($a as $arr) {
$res = Set::sort($arr['Member'], '{n}.Code.name', 'asc');
$result[] = array(
'Group' => $arr['Group'],
'Member' => $res
);
}
pr($result);