Search Results

Search found 15007 results on 601 pages for 'array'.

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

  • 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

  • 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

  • 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

  • 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

  • php transform array into multidim array

    - by fverswijver
    So I'm working on a website with Doctrine as ORM and I get the following array back as a result: Array ( [0] => Array ( [c_cat_id] => 1 [c_title] => Programas e projetos [p_menu] => PBA BR 163 [p_page_id] => 1 ) [1] => Array ( [c_cat_id] => 1 [c_title] => Programas e projetos [p_menu] => Outros projetos [p_page_id] => 3 ) ) Is it possible to transform this array (in PHP) to something like this: Array ( [0] => Array ( [c_cat_id] => 1 [c_title] => Programas e projetos [pages] => Array ([p_page_id] => 1 [p_menu] => PBA BR 163, [p_page_id] => 3 [p_menu] => Outros projetos)) Thanks for your help, always eager to learn new ways of doing things and that's why I love StackOverflow ;)

    Read the article

  • php foreach getting values from an array

    - by sea_1987
    I am having trouble accessing the values in an array, the array looks like this, Array ( [0] => Array ( [id] => 1661 [code] => 849651318 [job_status] => 4 [looking_for] => Lorem ipsum [keywords_education] => Derby University [sector_id_csv] => 10,21,9,22,26 [last_job_title_1] => Programmer [last_job_employer_1] => HBOS [city] => Bury [expected_salary_level] => LEVEL_2 [education_level] => COLLEGE [job_looking_for] => [is_contract] => Y [is_permanent] => N [is_temporary] => Y ) ) Array ( [0] => Array ( [id] => 402 [code] => 849650059 [job_status] => 3 [looking_for] => Lorem ipsum [keywords_education] => Paris College [sector_id_csv] => 27,22,19,21,12 [last_job_title_1] => Programmer [last_job_employer_1] => HSBC [city] => Bury [expected_salary_level] => LEVEL_2 [education_level] => COLLEGE [job_looking_for] => [is_contract] => N [is_permanent] => Y [is_temporary] => Y ) ) Array ( [0] => Array ( [id] => 1653 [code] => 849651310 [job_status] => 3 [looking_for] => Lorem ipsum [keywords_education] => Crewe University [sector_id_csv] => 27,15,19,21,24 [last_job_title_1] => Programmer [last_job_employer_1] => ICI [city] => Bury [expected_salary_level] => LEVEL_2 [education_level] => UNIVERSITY [job_looking_for] => [is_contract] => N [is_permanent] => Y [is_temporary] => Y ) ) I am trying to get the values out, I have tried doing the following, foreach ($result as $rslt) { echo $rslt->id; } I have also tried, foreach ($result as $rslt) { $rslt['id']; } But none of this works, I dont know why, can anyone help?

    Read the article

  • How to check if *number* is in a array

    - by Emil
    Pretty basic programming question, I know PHP have a function for it, but does the iPhone OS have one? I want to check if the current indexPath is a value in an array. PHP Example: <?php $indexPath = 3; $array = array("0", "1", "2", "3", "4"); if (in_array($indexPath, $array)) { // Do something } ?> Does anybody know how to do the same thing inthe iPhone OS?

    Read the article

  • return a php associative array to javascript array

    - by Eric Sim
    I am trying to return a php associative array to javascript array through ajaxRequest.responseText Here's what I do. First in php, I do this: $encoded = json_encode($thisarray); echo $encoded; If I echo $encoded, I get {"a":"apple,arrow","b":"boy,bank","c":"cat,camp"} Then in js script, thisarray = new Array(); thisarray = ajaxRequest.responseText; If I alert thisarray, I get {"a":"apple,arrow","b":"boy,bank","c":"cat,camp"} That's wrong since alerting an array should give error. But in this case, when I alert thisarray, I get the full array!! Needless to say, I can't call my value out of thisarray, since it is yet defined as an array. Anyone can tell me what am I missing here?

    Read the article

  • Copying 1-D array into 2 -D array

    - by Digvijay Yadav
    I have one two dimensional array and one single dimensional array. The two dimensional array is of NxM size. And the one dimensional array is of size N x M means it has N X M elements. Now I want to copy all the elements of the one dimensional array into the 2-D array. This is what I tried for(i = 0; i < M; i += 1) { for(j = 0; j < N; j += 1) { arr2d[i][j] = arr2d[(i*j +j)]; } } But not working Any suggestions???

    Read the article

  • Array sum of difference of first smallest k elment

    - by prateeak ojha
    Hi i come across this challenge on an online programing challange the task is so simple for ex WE have to variable N and K where N is where N is lenght of array and we have to find sum of duiffrence of K smallest element of array as Sample Input 10 4 1 2 3 4 10 20 30 40 100 200 Sample Output 10 Explanation #0 We have 10 as N size of array and K is 4 here the next N values are array's elments in order to find sum of diffrences we will takek smallest values from array which are 1,2,3,4 then will perform operation(sum of difference) as |1-2| + |1-3| + |1-4| + |2-3| + |2-4| + |3-4| = 10 I tried solving the problem and found a way through which i can solve the problem wit N^3 complexity but my solution is rejected i need a approach to solve the problem with n complexity i still can't figure out a way .. looked at some solution but coud'nt find the exact way to approach. if anybody have a better idea and would like to share it would be appreciable thanks in advance

    Read the article

  • loading data from file into 2d array

    - by Chris
    I am just starting with perl and would like some help with arrays please. I am reading lines from a data file and splitting the line into fields: open (INFILE, $infile); do { my $linedata = <INFILE>; my @data= split ',',$linedata; .... } until eof; I then want to store the individual field values (in @data) in and array so that the array looks like the input data file ie, the first "row" of the array contains the first line of data from INFILE etc. Each line of data from the infile contains 4 values, x,y,z and w and once the data are all in the array, I have to pass the array into another program which reads the x,y,z,w and displays the w value on a screen at the point determined by the x,y,z value. I can not pas the data to the other program on a row-by-row basis as the program expects the data to in a 2d matrtix format. Any help greatly appreciated. Chris

    Read the article

  • Sorting an array in ascending order without losing the index Objective-C

    - by thary
    Hello all, I have an array for instance, Array { 3.0 at Index 0 2.0 at Index 1 3.5 at Index 2 1.0 at Index 4 } I would like to get sort it in ascending order without losing the index in the first place, like this, Array { 1.0 at Index 4 2.0 at Index 1 3.0 at Index 0 3.5 at Index 2 } When I sort the array using this, NSArray *sortedArray = [hArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; [knnRecog sortUsingDescriptors:[NSArray arrayWithObject:sortAsc]]; I lose the index. Does anyone know a way to preserve the index after sorting the array? Thanks

    Read the article

  • array_merge vs array_value for resetting array index

    - by Jamex
    I have 1 array that I want to re-index. I have found that both array_values and array_merge functions can do the job (and I don't need 2 arrays for the array_merge function to work). Which is faster for a very large array? I would benchmark this, but I don't know how and don't have the large array yet. Before re-index: Array ( [0] => AB [4] => EA [6] => FA [9] => DA [10] => AF ) After re-index: Array ( [0] => AB [1] => EA [2] => FA [3] => DA [4] => AF )

    Read the article

  • Traverse multi dimensional array recusively without using foreach

    - by ejaz
    I have an array like this and the code using foreach loop. $arr = array( array ( array( 'CAR_TIR', 'Tires', 100 ), array( 'CAR_OIL', 'Oil', 10 ), array( 'CAR_SPK', 'Spark Plugs', 4 ) ), array ( array( 'VAN_TIR', 'Tires', 120 ), array( 'VAN_OIL', 'Oil', 12 ), array( 'VAN_SPK', 'Spark Plugs', 5 ) ), array ( array( 'TRK_TIR', 'Tires', 150 ), array( 'TRK_OIL', 'Oil', 15 ), array( 'TRK_SPK', 'Spark Plugs', 6 ) ) ); function recarray($array) { foreach($array as $key=>$value) { if(is_array($value)) { RecArray($value); } else { echo "key = $key value = $value"; } } } recarray($arr); I have to traverse the array using recursion and without using foreach. I would appreciate it if any one can help me

    Read the article

  • Converting 3 dimension byte array to a single byte array [on hold]

    - by Andrew Simpson
    I have a 3 dimensional byte array. The 3-d array represents a jpeg image. Each channel/array represents part of the RGB spectrum. I am not interested in retaining black pixels. A black pixel is represented by this atypical arrangement: myarray[0,0,0] =0; myarray[0,0,1] =0; myarray[0,0,2] =0; So, I have flattened this 3d array out to a 1d array by doing this byte[] AFlatArray = new byte[width x height x 3] and then assigning values respective to the coordinate. But like I said I do not want black pixels. So this array has to only contain color pixels with the x,y coordinate. The result I want is to re-represent the image from the i dimension byte array that only contains non-black pixels. How do I do that? It looks like I have to store black pixels as well because of the xy coordinate system. I have tried writing to a binary file but the size of that file is greater than the jpeg file as the jpeg file is compressed. I am using c#.

    Read the article

  • Convert Array to Multidimensional Array

    - by Tim
    I like to convert a single array into a multidimensional array. This is what I get have web scraping a page, except it is not the end result that I am looking for. Change: Rooms: Array ( [0] => name [1] => value [2] => size [3] => &nbsp; [4] => name [5] => value [6] => size [7] => &nbsp; [8] => name [9] => value [10] => size [11] => &nbsp; [12] => name [13] => value [14] => size [15] => &nbsp; ) Into: Rooms: Array ( Room: Array ( [0] => name [1] => value [2] => size ), Room: Array ( [0] => name [1] => value [2] => size ), Room: Array ( [0] => name [1] => value [2] => size ) )

    Read the article

  • Matching array elements to get an array element at another index

    - by bccarlso
    I have a PHP array that I'm using to generate an HTML form. The PHP array is this: <?php $vdb = array ( array( "Alabama", 275), array( "Alaska", 197), array( "Arizona", 3322)); ?> The PHP to generate the HTML form is below. I need to have the value be the name of the state, because there is some AJAX I'm using to display which states a user has chosen. <?php echo "<table border='1'><thead><tr><th></th><th>State</th><th>Contacts</th><th>Email</th></tr></thead>"; for ($row = 0; $row < 42; $row++) { echo "<tr><td class='input_button'><input type='checkbox' name='vdb[]' value='".$vdb[$row][0]."' title='".$vdb[$row][1]."' /></td>"; echo "<td>".$vdb[$row][0]."</td>"; echo "<td>".$vdb[$row][1]."</td>"; } echo "</table>"; ?> What I'm trying to do is, on submission of the form, with the states the user selected, loop through the PHP array and total the numbers from the selected states. So if I checked Alabama and Alaska, I'd want to add 275 + 197. This is what I thought would have worked, but it's not: <?php $vendors = array(); if (isset($_POST["vdb"])) { $vendors = $_POST["vdb"]; } $ven_i = 0; $ven_j = 0; $ven_total = 0; foreach ($vendors as $value) { foreach ($vdb as $vdb_value) { if ($vendors[$ven_i] == $vdb[$ven_j][0]) { $ven_total += $vdb[$ven_j][1]; } $ven_j++; } $ven_i++; } ?> and then $ven_total should be the total I'm looking for. However, $ven_total just ends up being the first checkbox selected, and it ignores the rest. I am doing this correctly with the AJAX, displaying the total on the front end, but I don't know how to pass that on to the form submission. I'd rather not using GET and URL variables, because a user could type something into the URL and modify the count. Any idea what I'm doing wrong, or a better way to approach this that I would be able to understand? (Very much a novice programmer.)

    Read the article

  • Formula needed: Sort array to array-"snaked"

    - by aw
    After the you guys helped me out so gracefully last time, here is another tricky array sorter for you. 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 and render it 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 to have a "snake" later: // rearrange the array according to this schema 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 // the original array should look like this a = [1,2,3,4,12,13,14,5,11,16,15,6,10,9,8,7] Now I'm looking for a smart formula / smart loop to do that ticker = 0; rows = 4; // can be n cols = 4; // can be n 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++; } Thanks again for the help.

    Read the article

  • Finding position of each word in a sub-array of a multidimensional array

    - by Shreyas Satish
    I have an array: tokens = [["hello","world"],["hello","ruby"]] all_tokens = tokens.flatten.uniq # all_tokens=["hello","world","ruby"] Now I need to create two arrays corresponding to all_tokens, where the first array will contain the position of each word in sub-array of tokens. I.E Output: [[0,0],[1],[1]] # (w.r.t all_tokens) To make it clear it reads, The index of "hello" is 0 and 0 in the 2 sub-arrays of tokens. And second array contains index of each word w.r.t tokens.I.E Output: [[0,1],[0],[1]] To make it clear it reads,the index of hello 0,1. I.E "hello" is in index 0 and 1 of tokens array. Cheers!

    Read the article

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