I have an array like below
Array
(
[0] => Array
(
[text] => one
[mp3] => 1.mp3
)
[1] => Array
(
[text] => two
[mp3] => 2.mp3
)
[2] => Array
(
[text] => three
[mp3] => 3.mp3
)
)
And I have another array with index to sort array(1,0,2) ,So With these I want following
Array
(
[0] => Array(
[text] => two[mp3] => 2. mp3
)
[1] => Array(
[text] => one[mp3] => 1. mp3
)
[2] => Array(
[text] => three[mp3] => 3. mp3
)
)
I googled and Found Few solutions on stackoverflow , But None seems to be successful for me
$order=array(1,0,2);
$orderedarray = array_merge(array_flip($order),$myarr);
$myarr = $orderedarray;
print_r($myarr);
Which outputs following
(
[0] => 0
[1] => 1
[2] => 2
[3] => Array
(
[text] => one
[mp3] => 1.mp3
)
[4] => Array
(
[text] => two
[mp3] => 2.mp3
)
[5] => Array
(
[text] => three
[mp3] => 3.mp3
)
)
You need to use array_multisort.
$order =[1,0,2];
array_multisort($myarr, $order);
Var_dump($myarr);
https://3v4l.org/cCT9d
Related
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.
This is the array which i get from post
Array
(
[data] => Array
(
[Invoice] => Array
(
[itemNo] => Array
(
[0] => rtgrg
[1] => 4t4t
[2] => ththt
[3] => thth
)
[itemName] => Array
(
[0] => rtgrt
[1] => 4t5t5
[2] => hthtyh
[3] => gnghnn
)
[itemDiscription] => Array
(
[0] => 5tr5t
[1] => t45t4
[2] => tyhthtyh
[3] => gnghnh
)
[price] => Array
(
[0] => 2
[1] => 10
[2] => 9
[3] => 12
)
[itemQuantity] => Array
(
[0] => 2
[1] => 12
[2] => 9
[3] => 9
)
[itemDiscount] => Array
(
[0] => 11
[1] => 0.14
[2] => 0.13
[3] => 0.1
)
[itemTotal] => Array
(
[0] => 333333
[1] => 34535
[2] => 55555555555555
[3] => 666666666
)
[itemStartDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] => 2016-06-17
)
[itemEndDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[itemCustomcol] => Array
(
[0] => 4t4t4
[1] => t5trgr
[2] => htht
[3] => gngh
)
[itemCustomcol2] => Array
(
[0] => t4t4t
[1] => rtgtr
[2] => thth
[3] => gng
)
[itemttax2] => Array
(
[0] => tax1
)
[itemttax3] => Array
(
[0] => tax1
)
[itemttax4] => Array
(
[0] => tax1
)
[itemtCsTax] => Array
(
[0] => gngn
)
[itemtCsTaxPer] => Array
(
[0] => 0.1
)
[itemtDiscount] => Array
(
[0] => 0.18
)
[itemtInvReceived] => Array
(
[0] => gngn
)
[itemtInvToReturn] => Array
(
[0] => gngng
)
[itemInvDue] => Array
(
[0] => nghnghng
)
[itemInvComment] => Array
(
[0] => hngnhg
)
)
)
There is a separate table for dynamic input like itemName, itemDiscription to itemCustomcol2 and another table for itemttax2 to itemInvComment .
I tried foreach loop for multiple element array but this doent work for me as it returns a string. one of them is shown below
$capture_field_vals ="";
foreach($_POST["data"]["Invoice"]["itemNo"] as $key => $text_field){
$capture_field_vals .= $text_field .", ";
}
echo $capture_field_vals;
What i want is collect all element[0] and then insert it in a row mysql and then another element another row and so on.
Is there any reason you can't use the following format?
Array
(
[data] => Array
(
[Invoice] => Array
(
[0] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),
[1] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),...
with this method you can loop through retrieving all details of each item in the invoice?
If I have misunderstood your problem, I apologise
-----problem solved, see the update 2 below----
I put all MySQL query results into a 2D array:
$suppDescription=mysql_query($query);
$rows = mysql_num_rows($results);
$allSupplierInfo=array();
for($i=0; $i<$rows; $i++){
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
But now I cannot access the $allSuppliersInfo fields.
echo $allSupplierInfo[1][1]; // prints out 'Array'
echo $allSupplierInfo[1]['id']; //prints out nothing
What am I doing wrong?
------- UPDATE-----
print_r($allSupplierInfo) prints the following, so it looks like the loop is not working as I wanted it to:
Array (
[0] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) )
[1] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) )
[2] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) )
[3] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) )
[4] => Array ( [0] => Array ( [0] =>ID_A[1] => name_A [2] => Address_A[3] => Link_A ) [1] => Array ( [0] =>ID_B[1] => Name_B [2] => Address_B [3] => Link_B ) [2] => Array ( [0] =>ID_C[1] => Name_C [2] => Address_C [3] => Link_C ) [3] => Array ( [0] =>ID_D[1] => Name_D [2] => Address_D [3] => Link_D ) [4] => Array ( [0] =>ID_E[1] => Name_E [2] => Address_E [3] => Address_E ) ) )
------ UPDATE 2-----
Using the while loop, as suggested by RiggsFolly, solved the problem and I can access the fields as I initially wanted. I still do not understand why the for loop I used did not loop as I thought it would - any explanation would be greatly appreciated.
I think you got confused with your mysql result processing, you are using $result when checking for the number of rows returned but it should be $suppDescription.
This means that your for loop will not run as you will be getting 0 or probbaly FALSE as a response to mysql_num_rows($suppDescription);
$suppDescription=mysql_query($query);
$rows = mysql_num_rows($suppDescription);
$allSupplierInfo=array();
for($i=0; $i<$rows; $i++){
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
Also this is easier done with a while loop, then you just process whatever is returned and dont need to bother getting the number of rows.
$suppDescription=mysql_query($query);
$allSupplierInfo=array();
while ( $rows = mysql_fetch_row($suppDescription) ) {
$allSupplierInfo[]=mysql_fetch_row($suppDescription);
}
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.
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.