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
)
)
Related
I need help modifying arrays to reach a desired structure. I am sorry the example structure maybe big, but i wanted to show the structure better.
I have a target Array like below that I need to generate
[bf_1040242] => Array
(
[326] => Just some Information. Additional Text
[17565] => Array
(
[0] => 2
[1] => 1
[2] => 3
)
[other] => Array
(
[17565] => Testing
[28623] =>
[42284] => Something Else
)
[597] => 1
[327] => This is some text
[328] => asdasd
[11880] => wwwww
[329] => xxxxx
[28622] => 2
[42283] => 1
[42284] => Array
(
[0] => 2
[1] => 3
[2] => 4
)
The data for me to generate this comes in a different format where the structure of these values are in string.
Array
(
[0] => Array
(
[name] => bf_1040242[326]
[value] => Just some Information. Additional Text
)
[1] => Array
(
[name] => bf_1040242[17565][]
[value] => 2
)
[2] => Array
(
[name] => bf_1040242[17565][]
[value] => 1
)
[3] => Array
(
[name] => bf_1040242[17565][]
[value] => 3
)
[4] => Array
(
[name] => bf_1040242[other][17565]
[value] => Testing
)
[5] => Array
(
[name] => bf_1040242[597]
[value] => 1
)
[6] => Array
(
[name] => bf_1040242[327]
[value] => This is some text
)
[7] => Array
(
[name] => bf_1040242[328]
[value] => asdasd
)
[8] => Array
(
[name] => bf_1040242[11880]
[value] => wwwww
)
[9] => Array
(
[name] => bf_1040242[329]
[value] => xxxxx
)
[10] => Array
(
[name] => bf_1040242[28622]
[value] => 2
)
[11] => Array
(
[name] => bf_1040242[other][28623]
[value] =>
)
[12] => Array
(
[name] => bf_1040242[42283]
[value] => 1
)
[13] => Array
(
[name] => bf_1040242[42284][]
[value] => 2
)
[14] => Array
(
[name] => bf_1040242[42284][]
[value] => 3
)
[15] => Array
(
[name] => bf_1040242[42284][]
[value] => 4
)
[16] => Array
(
[name] => bf_1040242[other][42284]
[value] => Something Else
)
)
In the above array, the key 'name' represents the structure of individual array in string format and the 'value' holds the value that the array structure needs to be updated to.
This is have managed to get done by using
$extra = []
foreach ($mainArray as $key => $value)
{
parse_str($value['name'], $tempArr);
$m = $this->multiArr($k, $value);
array_push($extra, $m);
}
protected function multiArr($k, $val)
{
foreach($k as $key => $value)
{
if(is_array($value) ) $k[$key] = $this->multiArr($k[$key], $val);
if(!is_array($value))
{
$k[$key] = (string) $val['value'];
}
}
return $k;
}
$m holds individual array values like
Array
(
[bf_1040242] => Array
(
[other] => Array
(
[42284] => Something Else
)
)
)
Using these individual array values i want to create the the Main required array so i used
array_push($extra, $m);
but this adds each $m array below each other instead of updating the way expected above.
Below is the way it is coming in my end.
Array
(
[0] => Array
(
[bf_1040242] => Array
(
[326] => Just some Information. Additional Text
)
)
[1] => Array
(
[bf_1040242] => Array
(
[17565] => Array
(
[0] => 2
)
)
)
[2] => Array
(
[bf_1040242] => Array
(
[17565] => Array
(
[0] => 1
)
)
)
[3] => Array
(
[bf_1040242] => Array
(
[17565] => Array
(
[0] => 3
)
)
)
[4] => Array
(
[bf_1040242] => Array
(
[other] => Array
(
[17565] => Testing
)
)
)
[5] => Array
(
[bf_1040242] => Array
(
[597] => 1
)
)
[6] => Array
(
[bf_1040242] => Array
(
[327] => This is some text
)
)
[7] => Array
(
[bf_1040242] => Array
(
[328] => asdasd
)
)
[8] => Array
(
[bf_1040242] => Array
(
[11880] => wwwww
)
)
[9] => Array
(
[bf_1040242] => Array
(
[329] => xxxxx
)
)
[10] => Array
(
[bf_1040242] => Array
(
[28622] => 2
)
)
[11] => Array
(
[bf_1040242] => Array
(
[other] => Array
(
[28623] =>
)
)
)
[12] => Array
(
[bf_1040242] => Array
(
[42283] => 1
)
)
[13] => Array
(
[bf_1040242] => Array
(
[42284] => Array
(
[0] => 2
)
)
)
[14] => Array
(
[bf_1040242] => Array
(
[42284] => Array
(
[0] => 3
)
)
)
[15] => Array
(
[bf_1040242] => Array
(
[42284] => Array
(
[0] => 4
)
)
)
[16] => Array
(
[bf_1040242] => Array
(
[other] => Array
(
[42284] => Something Else
)
)
)
)
I am not sure if array_push is what i need to use, or something else.
Also, is there a cleaner or a more efficient way to this.
Any help is much appreciated, Thanks
Normally you'd use array_merge_recursive for this, but it wont preserve named keys, so I wrote a little array_join function to make life easier :)
function array_join($value, &$result) {
if (!is_array($value)) {
$result = $value;
return;
}
foreach ($value as $k => $v) {
array_join($v, $result[$k]);
}
}
$result = [];
foreach ($mainArray as $entry)
{
parse_str($entry['name'] . '=' . $entry['value'], $nameArray);
array_join($nameArray, $result);
}
We simply call array_join recursively until the value is no longer an array. Notice the & in &$result of array_join. We pass the reference of our current position in the array.
To make full use of parse_str I've added the value aswell by appending =<value>
Working example.
Pro tip for your next post. Use var_export to give us an array, which can be copy pasted as code :)
EDIT
More simple solution with escaped values for "evil" input.
foreach ($mainArray as $entry)
{
$stringVarArray[] = $entry['name'] . '=' . urlencode($entry['value']);
}
parse_str(implode('&', $stringVarArray), $result);
var_dump($result);
Working example.
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.
How can i parse the below multi dimensional array ($array) and pull values of [productType] , [totalPrice]and [productCategory] if [packageCode] is matching with the value of $pkgcodes[1]...[z]
$pkgcodes is an array of codes
print_r of $pkgcodes
Array ( [0] => TRA1I2 [1] => TREZEC [n] ...)
The array $array is a response from SOAP client
print_r of $array
Array (
[0] => Array (
[packageCode] => TRA1I2
[totalPrice] => 17
[productType] => product Only
[products] => Array (
[0] => Array (
[productCategory] => Simple
[paxes] => Array (
[0] => Array (
[paxType] => Adult
[age] => 30 )
[1] => Array (
[paxType] => Adult
[age] => 30 ) )
[totalproductRate] => 17
[ratesPerNight] => Array (
[0] => Array (
[date] => 2015-01-28
[amount] => 17 ) ) ) ) )
[1] => Array (
[packageCode] => TREZEC
[totalPrice] => 17
[productType] => product Only
[products] => Array (
[0] => Array (
[productCategory] => Complicated
[paxes] => Array (
[0] => Array (
[paxType] => Adult
[age] => 30 )
[1] => Array (
[paxType] => Adult
[age] => 30 ) )
[totalproductRate] => 17
[ratesPerNight] => Array (
[0] => Array (
[date] => 2015-01-28
[amount] => 17 ) ) ) ) ) ).
You help is more appreciated
Try with -
$newArr = array();
foreach($array as $value) {
if (in_array($value['packageCode'], $pkgcodes)) {
$temp['productType'] = $value['productType'];
$temp['totalPrice'] = $value['totalPrice'];
$temp['packageCode'] = $value['packageCode'];
$temp['productCategory'] = $value['products']['productCategory'];
$newArr[] = $temp;
}
}
var_dump($newArr);
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.
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);