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);
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 want to generate a nested associative array from the result of SQL query from the table which should look like this:
[0] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Sciuridae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Marmota
[children] => Array
(
[0] => Array
(
[name] => Marmota himalayan
[children] => Himalayan Marmot
)
)
)
)
),
[1] => Array
(
[name] => Spalacidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Cannomys
[children] => Array
(
[0] => Array
(
[name] => Cannomys badius
[children] => Lesser Bamboo Rat
)
)
)
)
),
[2] => Array
(
[name] => Spalacidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Cannomys
[children] => Array
(
[0] => Array
(
[name] => Cannomys badius
[children] => Lesser Bamboo Rat
)
)
)
)
),
[3] => Array
(
[name] => Cricetidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Alticola
[children] => Array
(
[0] => Array
(
[name] => Alticola roylei
[children] => Royle's Mountain V
)
)
)
)
)
)
)
Problem: I am unable to figure it out how to nest array if there are same subsequent particular column values, for example, Rodentia has 3 different families (according to the table) it should nest inside children array and similarly for other fields as shown in the above array. My code is as follows:
$sql = "SELECT * FROM `mamallia_table`";
$result = $conn->query($sql);
$genus = array();
if ($result->num_rows > 0) {
foreach($result as $row){
$data[] = classification_order($row);
}
echo "<pre>";
print_r($data);
} else {
echo "0 results";
}
}
function classification_order($row){
$species[] = array("name"=>$row['species'],"children"=>$row['common_name']);
$genus[] = array("name"=>$row['genus'],"children"=>$species);
$family_count[] = array("name"=>$row['family'],"children_no"=>$row['no_of_species'],"children"=>$genus);
$data = ["name"=>$row['order'],"children_no"=>$row['no_of_family'],"children"=>$family_count];
return $data;
}
Output: Which gives me the resulting array:
Array
(
[0] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Sciuridae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Marmota
[children] => Array
(
[0] => Array
(
[name] => Marmota himalayan
[children] => Himalayan Marmot
)
)
)
)
)
)
)
[1] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Spalacidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Cannomys
[children] => Array
(
[0] => Array
(
[name] => Cannomys badius
[children] => Lesser Bamboo Rat
)
)
)
)
)
)
)
[2] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Cricetidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Alticola
[children] => Array
(
[0] => Array
(
[name] => Alticola roylei
[children] => Royle's Mountain V
)
)
)
)
)
)
)
)
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.
i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.
Using foreach loops in PHP I would like to add ids to the following object...
$array_before
Array
(
[1111] => Array
(
[Name] => Name A
[Subcats] => Array
(
[11111] => Array
(
[Name] => Name A.1
)
[11112] => Array
(
[Name] => Name A.2
)
)
)
[2222] => Array
(
[Name] => Name B
[Subcats] => Array
(
[22221] => Array
(
[Name] => Name B.1
)
[22222] => Array
(
[Name] => Name B.2
)
)
)
)
... so it looks similar to the below:
$array_after
Array
(
[1111] => Array
(
[Id] => 1
[Name] => Name A
[Subcats] => Array
(
[11111] => Array
(
[Id] => 1
[Name] => Name A.1
)
[11112] => Array
(
[Id] => 2
[Name] => Name A.2
)
[11113] => Array
(
[Id] => 3
[Name] => Name A.2
)
)
)
[2222] => Array
(
[Id] => 2
[Name] => Name B
[Subcats] => Array
(
[22221] => Array
(
[Id] => 1
[Name] => Name B.1
)
[22222] => Array
(
[Id] => 2
[Name] => Name B.2
)
)
)
)
Could someone point me in the right direction?
Thanks,
LG
Try this:
$id = 0;
array_walk($array,function(&$a) use (&$id) {$a['id'] = ++$id;});
This will modify the originl array to add the IDs, rather than create a new one.