Search Results

Search found 16433 results on 658 pages for 'array sorting'.

Page 3/658 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Array Multiplication and Division

    - by Narfanator
    I came across a question that (eventually) landed me wondering about array arithmetic. I'm thinking specifically in Ruby, but I think the concepts are language independent. So, addition and subtraction are defined, in Ruby, as such: [1,6,8,3,6] + [5,6,7] == [1,6,8,3,6,5,6,7] # All the elements of the first, then all the elements of the second [1,6,8,3,6] - [5,6,7] == [1,8,3] # From the first, remove anything found in the second and array * scalar is defined: [1,2,3] * 2 == [1,2,3,1,2,3] But What, conceptually, should the following be? None of these are (as far as I can find) defined: Array x Array: [1,2,3] * [1,2,3] #=> ? Array / Scalar: [1,2,3,4,5] / 2 #=> ? Array / Scalar: [1,2,3,4,5] % 2 #=> ? Array / Array: [1,2,3,4,5] / [1,2] #=> ? Array / Array: [1,2,3,4,5] % [1,2] #=> ? I've found some mathematical descriptions of these operations for set theory, but I couldn't really follow them, and sets don't have duplicates (arrays do). Edit: Note, I do not mean vector (matrix) arithmetic, which is completely defined. Edit2: If this is the wrong stack exchange, tell me which is the right one and I'll move it. Edit 3: Add mod operators to the list. Edit 4: I figure array / scalar is derivable from array * scalar: a * b = c => a = b / c [1,2,3] * 3 = [1,2,3]+[1,2,3]+[1,2,3] = [1,2,3,1,2,3,1,2,3] => [1,2,3] = [1,2,3,1,2,3,1,2,3] / 3 Which, given that programmer's division ignore the remained and has modulus: [1,2,3,4,5] / 2 = [[1,2], [3,4]] [1,2,3,4,5] % 2 = [5] Except that these are pretty clearly non-reversible operations (not that modulus ever is), which is non-ideal. Edit: I asked a question over on Math that led me to Multisets. I think maybe extensible arrays are "multisets", but I'm not sure yet.

    Read the article

  • Sorting a file with 55K rows and varying Columns

    - by Prasad
    Hi I want to find a programmatic solution using C++. I have a 900 files each of 27MB size. (just to inform about the enormity ). Each file has 55K rows and Varying columns. But the header indicates the columns I want to sort the rows in an order w.r.t to a Column Value. I wrote the sorting algorithm for this (definitely my newbie attempts, you may say). This algorithm is working for few numbers, but fails for larger numbers. Here is the code for the same: basic functions I defined to use inside the main code: int getNumberOfColumns(const string& aline) { int ncols=0; istringstream ss(aline); string s1; while(ss>>s1) ncols++; return ncols; } vector<string> getWordsFromSentence(const string& aline) { vector<string>words; istringstream ss(aline); string tstr; while(ss>>tstr) words.push_back(tstr); return words; } bool findColumnName(vector<string> vs, const string& colName) { vector<string>::iterator it = find(vs.begin(), vs.end(), colName); if ( it != vs.end()) return true; else return false; } int getIndexForColumnName(vector<string> vs, const string& colName) { if ( !findColumnName(vs,colName) ) return -1; else { vector<string>::iterator it = find(vs.begin(), vs.end(), colName); return it - vs.begin(); } } ////////// I like the Recurssive functions - I tried to create a recursive function ///here. This worked for small values , say 20 rows. But for 55K - core dumps void sort2D(vector<string>vn, vector<string> &srt, int columnIndex) { vector<double> pVals; for ( int i = 0; i < vn.size(); i++) { vector<string>meancols = getWordsFromSentence(vn[i]); pVals.push_back(stringToDouble(meancols[columnIndex])); } srt.push_back(vn[max_element(pVals.begin(), pVals.end())-pVals.begin()]); if (vn.size() > 1 ) { vn.erase(vn.begin()+(max_element(pVals.begin(), pVals.end())-pVals.begin()) ); vector<string> vn2 = vn; //cout<<srt[srt.size() -1 ]<<endl; sort2D(vn2 , srt, columnIndex); } } Now the main code: for ( int i = 0; i < TissueNames.size() -1; i++) { for ( int j = i+1; j < TissueNames.size(); j++) { //string fname = path+"/gse7307_Female_rma"+TissueNames[i]+"_"+TissueNames[j]+".txt"; //string fname2 = sortpath2+"/gse7307_Female_rma"+TissueNames[i]+"_"+TissueNames[j]+"Sorted.txt"; string fname = path+"/gse7307_Male_rma"+TissueNames[i]+"_"+TissueNames[j]+".txt"; string fname2 = sortpath2+"/gse7307_Male_rma"+TissueNames[i]+"_"+TissueNames[j]+"4Columns.txt"; //vector<string>AllLinesInFile; BioInputStream fin(fname); string aline; getline(fin,aline); replace (aline.begin(), aline.end(), '"',' '); string headerline = aline; vector<string> header = getWordsFromSentence(aline); int pindex = getIndexForColumnName(header,"p-raw"); int xcindex = getIndexForColumnName(header,"xC"); int xeindex = getIndexForColumnName(header,"xE"); int prbindex = getIndexForColumnName(header,"X"); string newheaderline = "X\txC\txE\tp-raw"; BioOutputStream fsrt(fname2); fsrt<<newheaderline<<endl; int newpindex=3; while ( getline(fin, aline) ){ replace (aline.begin(), aline.end(), '"',' '); istringstream ss2(aline); string tstr; ss2>>tstr; tstr = ss2.str().substr(tstr.length()+1); vector<string> words = getWordsFromSentence(tstr); string values = words[prbindex]+"\t"+words[xcindex]+"\t"+words[xeindex]+"\t"+words[pindex]; AllLinesInFile.push_back(values); } vector<string>SortedLines; sort2D(AllLinesInFile, SortedLines,newpindex); for ( int si = 0; si < SortedLines.size(); si++) fsrt<<SortedLines[si]<<endl; cout<<"["<<i<<","<<j<<"] = "<<SortedLines.size()<<endl; } } can some one suggest me a better way of doing this? why it is failing for larger values. ? The primary function of interest for this query is Sort2D function. thanks for the time and patience. prasad.

    Read the article

  • Build associative array based on values of another associative array

    - by macek
    I'm looking for an elegant way to turn this array: Array ( [foo] => 1 [bar] => 1 [zim] => 3 [dib] => 6 [gir] => 1 [gaz] => 3 ) Into this array: Array ( [1] => Array ( foo, bar, gir ), [3] => Array ( zim, gaz ), [6] => Array ( dib ) ) Note:, there is no relationship between the keys or values. They are completely arbitrary and used as examples only. The resulting array should be an associative array grouped by the values of the input array. Thanks!

    Read the article

  • Reverse alphabetic sort multidimensional PHP array maintain key

    - by useyourillusiontoo
    I'm dying here, any help would be great. I've got an array that I can sort a-z on the value of a specific key but cannot sort in reverse z-a. sample of my array which i'd like to sort by ProjectName (z-a): Array ( [0] => Array ( [count] => 1 [ProjectName] => bbcjob [Postcode] => 53.471922,-2.2996078 [Sector] => Public ) [1] => Array ( [count] => 1 [ProjectName] => commercial enterprise zone [Postcode] => 53.3742081,-1.4926439 [Sector] => Public ) [2] => Array ( [count] => 1 [ProjectName] => Monkeys eat chips [Postcode] => 51.5141492,-0.2271227 [Sector] => Private the desired results would be to maintain the entire array key - value structure but with the order: Monkeys eat chips Commericial enterprise zone bbcjob I hope this makes sense

    Read the article

  • rearrange multidimensional array on basis of value of inner array

    - by I Like PHP
    i have an array like this Array ( [0] => Array ( [cat_name] => Clothing [cat_id] => 1 [item_name] => shirt [item_id] => 1 [src] => 177 [sic] => 78 ) [1] => Array ( [cat_name] => Stationary [cat_id] => 3 [item_name] => note book [item_id] => 8 [src] => 50 [sic] => 10 ) [2] => Array ( [cat_name] => Stationary [cat_id] => 3 [item_name] => ball pen [item_id] => 10 [src] => 59 [sic] => 58 ) [3] => Array ( [cat_name] => Expandable [cat_id] => 4 [item_name] => vim powder [item_id] => 14 [src] => 34 [sic] => 23 ) [4] => Array ( [cat_name] => Clothing [cat_id] => 1 [item_name] => pant [item_id] => 16 [src] => 100 [sic] => 10 ) ) now what i want first it sorted by cat_id and then a create a new array having below structure Array ( [0] =>"Clothing"=>Array ( [0]=>Array ( [item_name] => shirt [item_id] => 1 [src] => 177 [sic] => 78 ) [1] => Array ( [item_name] => pant [item_id] => 16 [src] => 100 [sic] => 10 ) ) [1] => "Stationary"=>Array ( [0] => Array ( [item_name] => note book [item_id] => 8 [src] => 50 [sic] => 10 ) [1] => Array ( [item_name] => ball pen [item_id] => 10 [src] => 59 [sic] => 58 ) ) [2]=>"Expandable => Array ( [0] => Array ( [item_name] => vim powder [item_id] => 14 [src] => 34 [sic] => 23 ) ) )

    Read the article

  • Sort an array by a child array's value

    - by Evan
    I have an array composed of arrays. I want to sort the parent array by a property of the child arrays. Here's an example array(2) { [0]=> array(3) { [0]=> string(6) "105945" [1]=> string(10) "First name" [2]=> float(0.080878465391) } [1]=> array(3) { [0]=> string(6) "109145" [1]=> string(11) "Second name" [2]=> float(0.0504154818384) } I would like to sort the parent array by [2] ascending in the child arrays, so in this case the result would be the child arrays reversed (.05, 08). Is this possible using any of the numerous PHP sort functions?

    Read the article

  • How to retrieve an array from Multidimensional Array.

    - by Mike Smith
    So I have a multi-dimensional array looks like this. $config = array( "First Name" => array( "user" => $_POST['firstname'], "limit" => 35, ), "Last Name" => array( "user" => $_POST['lastname'], "limit" => 40, ), ); I want use the array that's within the config array, so my approach is to use a foreach loop. foreach($config as $field => $data) { } Now I know that $data will be my array, but it seems I can't use it outside of the foreach statement because I only get half of whats already there. Using print_r you can see what it shows outside the loop: Array ( [user] => lastname [limit] => 40 ) But when inside the loop and I use print_r here is my result: Array ( [user] => firstname [limit] => 35 ) Array ( [user] => lastname [limit] => 40 ) I imagine it has to do something with it being with the foreach loop. I've tried to run a foreach on the $data array to populate another array, but that didn't work as well. Is there a way to use this outside of a foreach loop? Sorry if this a dumb question, I'm sure there is a quite a simple answer to this, but I'm just stumped, and can't think of a way to do this. Thanks.

    Read the article

  • PHP manipulating multidimensional array values

    - by Joker
    I have a result set as an array from a database that looks like: array ( 0 => array ( "a" => "something" "b" => "something" "c" => "something" ) 1 => array ( "a" => "something" "b" => "something" "c" => "something" ) 2 => array ( "a" => "something" "b" => "something" "c" => "something" ) ) How would I apply a function to replace the values of an array only on the array key with b? Normally I would just rebuild a new array with a foreach loop and apply the function if the array key is b, but I'm not sure if it's the best way. I've tried taking a look at many array functions and it seemed like array_walk_recursive is something I might use, but I didn't have luck in getting it to do what I want. If I'm not describing it well enough, basically I want to be able to do as the code below does: $arr = array(); foreach ($result as $key => $value) { foreach ($value as $key2 => $value2) { $arr[$key][$key2] = ($key2 == 'b' ? $this->_my_method($value2) : $value2); } } Should I stick with that, or is there a better way?

    Read the article

  • (PHP) Converting an array of arrays from one format into another

    - by Richard Carter
    Hi, I currently have an array, created from a database, an example of which looks like the following: Array( [0] => Array ( objectid => 2, name => title, value => apple ), [1] => Array ( objectid => 2, name => colour, value => red ), [2] => Array ( objectid => 3, name => title, value => pear ), [3] => Array ( objectid => 3, name => colour, value => green ) ) What I would like to do is group all the items in the array by their objectid, and convert the 'name' values into keys and 'value' values into values of an associative array....like below: Array ( [0] => Array ( objectid => 2, title => apple, colour => red ), [1] => Array ( objectid => 3, title => pear, colour => green ) ) I've tried a few things but haven't really got anywhere.. Any ideas? Thanks in advance

    Read the article

  • Loop through multi-dimensional array and remove certain keys

    - by Webkungen
    Hi! I've got a nested tree structure which is based on the array below: Array ( [1] = Array ( [id] = 1 [parent] = 0 [name] = Startpage [uri] = 125 [basename] = index.php [child] = ) [23] = Array ( [id] = 23 [parent] = 0 [name] = Events [uri] = 0 [basename] = [child] = Array ( [24] = Array ( [id] = 24 [parent] = 23 [name] = Public news [uri] = 0 [basename] = [child] = Array ( [27] = Array ( [id] = 27 [parent] = 24 [name] = Add [uri] = 100 [basename] = news.public.add.php [child] = ) [28] = Array ( [id] = 28 [parent] = 24 [name] = Overview [uri] = 101 [basename] = news.public.overview.php [child] = ) ) ) [25] = Array ( [id] = 25 [parent] = 23 [name] = Private news [uri] = 0 [basename] = [child] = Array ( [29] = Array ( [id] = 29 [parent] = 25 [name] = Add [uri] = 67 [basename] = news.private.add.php [child] = ) [30] = Array ( [id] = 30 [parent] = 25 [name] = Overview [uri] = 68 [basename] = news.private.overview.php [child] = ) ) ) [26] = Array ( [id] = 26 [parent] = 23 [name] = Calendar [uri] = 0 [basename] = [child] = Array ( [31] = Array ( [id] = 31 [parent] = 26 [name] = Add [uri] = 69 [basename] = news.event.add.php [child] = ) [32] = Array ( [id] = 32 [parent] = 26 [name] = Overview [uri] = 70 [basename] = news.event.overview.php [child] = ) ) ) ) ) ) I'm looking for a function to loop (recursive?) through the array and remove some keys. I my system I can allow users to certain functions/pages and if I deny access to the whole "block" "Events", the array will look like this: Array ( [1] = Array ( [id] = 1 [parent] = 0 [name] = Startpage [uri] = 125 [basename] = index.php [child] = ) [23] = Array ( [id] = 23 [parent] = 0 [name] = Events [uri] = 0 [basename] = [child] = Array ( [24] = Array ( [id] = 24 [parent] = 23 [name] = Public news [uri] = 0 [basename] = [child] = ) [25] = Array ( [id] = 25 [parent] = 23 [name] = Private news [uri] = 0 [basename] = [child] = ) [26] = Array ( [id] = 26 [parent] = 23 [name] = Calendar [uri] = 0 [basename] = [child] = ) ) ) ) As you can see above, the whole "block" "Events" is useless right now, becuase there is no page associated with each option. So I need to find all "keys" where "basename" is null AND where child is not an array or where the array is empty and remove them. I found this function when searching the site: function searchAndDestroy(&$a, $key, $val){ foreach($a as $k = &$v){ if(is_array($v)){ $r = searchAndDestroy($v, $key, $val); if($r){ unset($a[$k]); } }elseif($key == $k && $val == $v){ return true; } } return false; } It can be used to remove a key any where in the array, but only based in one thing, for example remove all keys where "parent" equals "23". But I need to find and remove (unset) all keys where "basename" is null AND where child isn't an array or where the array is empty. Can anyone help me out and possibly tweak the function above? Thank you,

    Read the article

  • Sort an array with special characters - iPhone

    - by ncohen
    Hi everyone, I have an array with french strings let say: "égrener" and "exact" I would like to sort it such as égrener is the first. When I do: NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor]; NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors]; I get the é at the end of the list... What should I do? Thanks

    Read the article

  • adding up value of array and getting the average

    - by sea_1987
    I have an array that looks similar to this, [4] => Common_Model Object ( [id] => 4 [name] => [date_created] => [last_updated] => [user_id_updated] => [_table] => [_aliases] => Array ( [id] => 4 [name] => [date_created] => [date_updated] => [user_id_updated] => [rating] => 3 [recipe_id] => 5 ) [_nonDBAliases] => Array ( ) [_default] => Array ( ) [_related] => Array ( ) [_enums] => [_alsoDelete] => Array ( ) [_readOnly] => Array ( [0] => date_updated ) [_valArgs] => Array ( ) [_valArgsHash] => Array ( [default] => Array ( ) ) [_valAliases] => Array ( ) [_extraData] => Array ( ) [_inputs] => Array ( ) [_tableName] => jm_ratings [_tablePrefix] => [_niceDateUpdated] => 1st Jan 70 [_niceDateCreated] => 1st Jan 70 [_fetchAdminData] => [_mCache] => [_assets] => Array ( ) ) [3] => Common_Model Object ( [id] => 3 [name] => [date_created] => [last_updated] => [user_id_updated] => [_table] => [_aliases] => Array ( [id] => 3 [name] => [date_created] => [date_updated] => [user_id_updated] => [rating] => 1 [recipe_id] => 5 ) [_nonDBAliases] => Array ( ) [_default] => Array ( ) [_related] => Array ( ) [_enums] => [_alsoDelete] => Array ( ) [_readOnly] => Array ( [0] => date_updated ) [_valArgs] => Array ( ) [_valArgsHash] => Array ( [default] => Array ( ) ) [_valAliases] => Array ( ) [_extraData] => Array ( ) [_inputs] => Array ( ) [_tableName] => jm_ratings [_tablePrefix] => [_niceDateUpdated] => 1st Jan 70 [_niceDateCreated] => 1st Jan 70 [_fetchAdminData] => [_mCache] => [_assets] => Array ( ) ) [2] => Common_Model Object ( [id] => 2 [name] => [date_created] => [last_updated] => [user_id_updated] => [_table] => [_aliases] => Array ( [id] => 2 [name] => [date_created] => [date_updated] => [user_id_updated] => [rating] => 1 [recipe_id] => 5 ) [_nonDBAliases] => Array ( ) [_default] => Array ( ) [_related] => Array ( ) [_enums] => [_alsoDelete] => Array ( ) [_readOnly] => Array ( [0] => date_updated ) [_valArgs] => Array ( ) [_valArgsHash] => Array ( [default] => Array ( ) ) [_valAliases] => Array ( ) [_extraData] => Array ( ) [_inputs] => Array ( ) [_tableName] => jm_ratings [_tablePrefix] => [_niceDateUpdated] => 1st Jan 70 [_niceDateCreated] => 1st Jan 70 [_fetchAdminData] => [_mCache] => [_assets] => Array ( ) ) I wanting to add up the [rating] and get the mean average. But I dont know how do this with PHP, my attempt looks like this, <?php foreach ($rt as $rating) { $total = $rating->rating + $rating->rating } $total / count($rt); ?>

    Read the article

  • Easiest way to remove Keys from a 2D Array?

    - by dbemerlin
    Hi, I have an Array that looks like this: array( 0 => array( 'key1' => 'a', 'key2' => 'b', 'key3' => 'c' ), 1 => array( 'key1' => 'c', 'key2' => 'b', 'key3' => 'a' ), ... ) I need a function to get an array containing just a (variable) number of keys, i.e. reduce_array(array('key1', 'key3')); should return: array( 0 => array( 'key1' => 'a', 'key3' => 'c' ), 1 => array( 'key1' => 'c', 'key3' => 'a' ), ... ) What is the easiest way to do this? If possible without any additional helper function like array_filter or array_map as my coworkers already complain about me using too many functions. The source array will always have the given keys so it's not required to check for existance. Bonus points if the values are unique (the keys will always be related to each other, meaning that if key1 has value a then the other key(s) will always have value b). My current solution which works but is quite clumsy (even the name is horrible but can't find a better one): function get_unique_values_from_array_by_keys(array $array, array $keys) { $result = array(); $found = array(); if (count($keys) > 0) { foreach ($array as $item) { if (in_array($item[$keys[0]], $found)) continue; array_push($found, $item[$keys[0]]); $result_item = array(); foreach ($keys as $key) { $result_item[$key] = $item[$key]; } array_push($result, $result_item); } } return $result; } Addition: PHP Version is 5.1.6.

    Read the article

  • Define 2D array with loops in php

    - by Michael
    I have an array $rows where each element is a row of 15 tab-delimited values. I want to explode $rows into a 2D array $rowData where each row is an array element and each tab-delimited value is assigned to a different array element. I've tried these two methods without success. I know the first one has a coding error but I do not know how to correct it. Any help would be amazing. for ($i=0; $i<count($rows); $i++){ for ($j=0; $j<15; $j++){ $rowData = array([$i] => array (explode(" ", $rows[$j]))); } } foreach ($rows as $value){ $rowData = array( array (explode(" ", $rows[$value]))); }

    Read the article

  • Convert complex numerical array to associative array [PHP]

    - by user1500412
    I have an array data that look like this : Array ( [0] => Array ( [0] => Name: [1] => John W. [2] => Registration ID: [3] => 36 ) [1] => Array ( [0] =>Age: [1] => 35 [2] => Height: [3] => 5'11" ) [3] => Array ( [0] => Sex: [1] => M [2] => Weight: [3] => 200lbs ) [4] => Array ( [0] => Address ) [5] => Array ( [0] => 6824 crestwood dr delphi, IN 46923 )) And I want to convert it to associative array like this : Array( ['Name']=> John W. ['Registration ID']=> 36 ['Age']=> 35 ['Height'] => 5'11'' ['Sex']=>M ['Weight']=>200lbs ['Address']=>6824 crestwood dr delphi, IN 46923 ) I have no idea at all how to do this, since the supposed to be array column header were also in sequence, so it makes difficult to convert this array. Any help I appreciate, thx.

    Read the article

  • php sorting a seriously multidimensional array...

    - by BigDogsBarking
    I'm trying to sort a multidimensional object, and, after looking on php.net and around here, I get that I should write a function that I can then call via usort. I'm having some trouble with the syntax. I haven't ever written something this complicated before, and trying to figure it out feels like a mindbender... I'm working with the array posted at the end of this post. I want to filter out duplicate [n] values. But, and this is the tricky part for me, I want to keep the [n] value that has the smallest [d] value. So, if I have (and this example is simplified, the real array is at the end of this post): Array ( [7777] => Array ( [0] => Array ( [n] => '12345' [d] => 1 ) [1] => Array ( [n] => '67890' [d] => 4 ) ) [8888] => Array ( [2] => Array ( [n] => '12345' [d] => 10 ) [3] => Array ( [n] => '67890' [d] => 2 ) ) ) I want to filter out duplicate [n] values based on the [d] value, so that I wind up with this: Array ( [7777] => Array ( [0] => Array ( [n] => '12345' [d] => 1 ) ) [8888] => Array [3] => Array ( [n] => '67890' [d] => 2 ) ) ) I've tried writing different variations of the function cmp example posted on php.net, but I haven't been able to get any to work, and I think it's because I'm not altogether clear on how to traverse it using their example... I tried: function cmp($a, $b) { if($a['n'] == $b['n']) { if($a['d'] == $b['d']) { return 0; } } return ($a['n'] < $b['n']) ? -1 : 1; } But, that really did not work at all... Anyway, here's the real array I'm trying to work with... Help is greatly appreciated! Array ( [32112] => Array ( [0] => Array ( [n] => '02124' [d] => '0' ) [1] => Array ( [n] => '02124' [d] => '0.240101905123744' ) [2] => Array ( [n] => '11050' [d] => '0.441758632682761' ) [3] => Array ( [n] => '02186' [d] => '0.317514080260304' ) ) [43434] => Array ( [4] => Array ( [n] => '02124' [d] => '5.89936971664429e-05' ) [5] => Array ( [n] => '02124' [d] => '0.145859264792549' ) [6] => Array ( [n] => '11050' [d] => '0.327864593457739' ) [7] => Array ( [n] => '11050' [d] => '0.312135345168295' ) ) )

    Read the article

  • how to change type of value in an php array and sorting it..is it possible ?

    - by justjoe
    hi, i got problem with my code and hopefully someone able to figure it out. The main purpose is to sort array based on its value (then reindex its numerical key). i got this sample of filename : $filename = array("index 198.php", "index 192.php", "index 144.php", "index 2.php", "index 1.php", "index 100.php", "index 111.php"); $alloutput = array(); //all of index in array foreach ($filename as $name) { preg_match('#(\d+)#', $name, $output); // take only the numerical from file name array_shift($output); // cleaned. the last code create duplicate numerical in $output, if (is_array($hasilku)) { $alloutput = array_merge($alloutput, $output); } } //try to check the type of every value in array foreach ($alloutput as $output) { if (is_array($hasil)) { echo "array true </br>"; } elseif (is_int($hasil)) { echo "integer true </br>"; } elseif (is_string($hasil)) { //the numerical taken from filename always resuld "string". echo "string true </br>"; } } the output of this code will be : Array ( [0] = 198 [1] = 192 [2] = 144 [3] = 2 [4] = 1 [5] = 100 [6] = 111 ) i have test every output in array. It's all string (and not numerical), So the question is how to change this string to integer, so i can sort it from the lowest into the highest number ? the main purpose of this code is how to output array where it had been sort from lowest to highest ?

    Read the article

  • Resize Array By Last and not by First in C#

    - by Leen15
    Hi all! I have an Array of Class elements, and by an int variable i need to resize this array to the last X elements. So for example i have an array with: Array[0] = Msg1 Array[1] = Msg2 Array[2] = Msg3 Array[3] = Msg4 Array[4] = Msg5 Array[5] = Msg6 Array[6] = Msg7 Array[7] = Msg8 Array[8] = Msg9 Array[9] = Msg10 and i need to have only the last 8 elements in the array. i cannot use the Array.Resize function because the result would be: Array[0] = Msg1 Array[1] = Msg2 Array[2] = Msg3 Array[3] = Msg4 Array[4] = Msg5 Array[5] = Msg6 Array[6] = Msg7 Array[7] = Msg8 and i need something like this: Array[0] = Msg3 Array[1] = Msg4 Array[2] = Msg5 Array[3] = Msg6 Array[4] = Msg7 Array[5] = Msg8 Array[6] = Msg9 Array[7] = Msg10 How can i do this? i hope my problem is clear. Thanks.

    Read the article

  • Merging multiple array then sorting by array value count

    - by Sofyan
    Hi, Please help me, i need to merge multiple arrays then sorting it by array value count. Below is the problem: $array1 = array("abc", "def", "ghi", "jkl", "mno"); $array2 = array("mno", "jkl", "mno", "ghi", "pqr", "stu"); $array3 = array_merge($array1, $array2); $array4 = ??? print_r($array4); I want the returns of $array4 like this: Array ( [0] => mno [1] => ghi [2] => jkl [3] => abc [4] => def [5] => pqr [6] => stu )

    Read the article

  • Formula needed: Sort Array

    - by aw
    I have the following array: a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] I use it for some visual stuff like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Now I want to sort the array like this: 1 3 6 10 2 5 9 13 4 8 12 15 7 11 14 16 //So the original array should look like this: a = [1,5,2,9,6,3,13,10,7,4,14,11,8,15,12,16] Yeah, now I'm looking for a smart formula to do that ticker = 0; originalArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] newArray = []; while(ticker < originalArray.length) { //do the magic here ticker++; }

    Read the article

  • how to do loop for array which have different data for each array

    - by Suriani Salleh
    i have this file XML file.. I need to convert it form XMl to MYSQL. if it have only one array than i know how to do it.. now my question how to extract this two array Each array will have different value of data..for example for first array, pmIntervalTxEthMaxUtilization data : 0,74,0,0,48 and for second array pmIntervalRxPowerLevel data: -79,-68,-52 , pmIntervalTxPowerLevel data: 13,11,-55 . can some one help to guide how write php code to extract this xml file to MY SQL <mi> <mts>20130618020000</mts> <gp>900</gp> <mt>pmIntervalRxUndersizedFrames</mt> [ this is first array] <mt>pmIntervalRxUnicastFrames</mt> <mt>pmIntervalTxUnicastFrames</mt> <mt>pmIntervalRxEthMaxUtilization</mt> <mt>pmIntervalTxEthMaxUtilization</mt> <mv> <moid>port:1:3:23-24</moid> <sf>FALSE</sf> <r>0</r> [the data for 1st array i want to insert in DB] <r>0</r> <r>0</r> <r>5</r> <r>0</r> </mv> </mi> <mi> <mts>20130618020000</mts> <gp>900</gp> <mt>pmIntervalRxSES</mt> [this is second array] <mt>pmIntervalRxPowerLevel</mt> <mt>pmIntervalTxPowerLevel</mt> <mv> <moid>client:1:3:23-24</moid> <sf>FALSE</sf> <r>0</r> [the data for 2nd array i want to insert in DB] <r>-79</r> <r>13</r> </mv> </mi> this is the code for one array that i write..i dont know how to write code for two array because the field appear two times and have different data value for each array // Loop through the specified xpath foreach($xml->mi->mv as $subchild) { $port_no = $subchild->moid; $rx_ses = $subchild->r[0]; $rx_es = $subchild->r[1]; $tx_power = $subchild->r[10]; // dump into database; ........................... i have do a little research on it this is the out come... $i = 0; while( $i < 5) { // Loop through the specified xpath foreach($xml->md->mi->mv as $subchild) { $port_no = $subchild->moid; $rx_uni = $subchild->r[10]; $tx_uni = $subchild->r[11]; $rx_eth = $subchild->r[16]; $tx_eth = $subchild->r[17]; // dump into database; .............................. $i++; if( $i == 5 )break; } } // Loop through the specified xpath foreach($xml->mi->mv as $subchild) { $port_no = $subchild->moid; $rx_ses = $subchild->r[0]; $rx_es = $subchild->r[1]; $tx_power = $subchild->r[10]; // dump into database; .......................

    Read the article

  • How do I access data within this multidimensional array?

    - by dmanexe
    I have this array: $items_pool = Array ( [0] => Array ( [id] => 1 [quantity] => 1 ) [1] => Array ( [id] => 2 [quantity] => 1 ) [2] => Array ( [id] => 72 [quantity] => 6 ) [3] => Array ( [id] => 4 [quantity] => 1 ) [4] => Array ( [id] => 5 [quantity] => 1 ) [5] => Array ( [id] => 7 [quantity] => 1 ) [6] => Array ( [id] => 8 [quantity] => 1 ) [7] => Array ( [id] => 9 [quantity] => 1 ) [8] => Array ( [id] => 19 [quantity] => 1 ) [9] => Array ( [id] => 20 [quantity] => 1 ) [10] => Array ( [id] => 22 [quantity] => 1 ) [11] => Array ( [id] => 29 [quantity] => 0 ) ) I'm trying to loop through this array and perform a conditional based on $items_pool[][id]'s value. I want to then report back TRUE or NULL/FALSE, so I'm just testing the presence of to be specific.

    Read the article

  • Adding array to an object breaks the array

    - by DisgruntledGoat
    I have an array like this (output from print_r): Array ( [price] => 700.00 [room_prices] => Array ( [0] => [1] => [2] => [3] => [4] => ) [bills] => Array ( [0] => Gas ) ) I'm running a custom function to convert it to an object. Only the top-level should be converted, the sub-arrays should stay as arrays. The output comes out like this: stdClass Object ( [price] => 700.00 [room_prices] => Array ( [0] => Array ) [bills] => Array ( [0] => Array ) ) Here is my conversion function. All it does is set the value of each array member to an object: function array_to_object( $arr ) { $obj = new stdClass; if ( count($arr) == 0 ) return $obj; foreach ( $arr as $k=>$v ) $obj->$k = $v; return $obj; } I can't figure this out for the life of me!

    Read the article

  • How can I sort an array, yet exclude certain elements (to be kept at the same position in the array)

    - by calumbrodie
    This will be implemented in Javascript (jQuery) but I suppose the method could be used in any language. I have an array of items and I need to perform a sort. However there are some items in the array that have to be kept in the same position (same index). The array in question is build from a list of <li> elements and I'm using .data() values attached to the list item as the value on which to sort. What approach would be best here? <ul id="fruit"> <li class="stay">bananas</li> <li>oranges</li> <li>pears</li> <li>apples</li> <li class="stay">grapes</li> <li>pineapples</li> </ul> <script type="text/javascript"> var sugarcontent = new Array('32','21','11','45','8','99'); $('#fruit li').each(function(i,e){ $(this).data(sugarcontent[i]); }) </script> I want the list sorted with the following result... <ul id="fruit"> <li class="stay">bananas</li> <!-- score = 32 --> <li>pineapples</li> <!-- score = 99 --> <li>apples</li> <!-- score = 45 --> <li>oranges</li> <!-- score = 21 --> <li class="stay">grapes</li> <!-- score = 8 --> <li>pears</li> <!-- score = 11 --> </ul> Thanks!

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >