Skip to main content

Posts

Showing posts with the label arrays

How to detect duplicate values in PHP array?

I am working with a one dimensional array in PHP. I would like to detect the presence of duplicate values, then count the number of duplicate values and out put the results. For example, given the following array: $array = array('apple', 'orange', 'pear', 'banana', 'apple', 'pear', 'kiwi', 'kiwi', 'kiwi'); I would like to print: apple (2) orange pear (2) banana kiwi (3) Any advice on how to approach this problem? Thanks. Mike Source: Tips4all

Javascript Funky array mishap

function a() { var b = ["b"]; console.log(b); //console.log(b.slice()); b = b.push("bb"); } a(); In a "perfect" world you would think that the console.log would show ["b"] , but wildly enough it shows ["b", "bb"] even though "bb" isn't pushed on until afterword . *wtf* If you do console.log(b.slice()); Then you will get the desired result of ["b"] . What gives? What's the reason behind this complication? I just want to understand this better so I can better avoid it from happening. *note I hit on this same point in a recent question of mine, but this is a much more concise example. @RightSaidFred has led me to this point and has been a HUGE help so far (big thanks). ** EDIT ** Runnable example on JSFiddle ** POST-ANSWER ** This is all due to a problem where console.log messes up sometimes. Thanks to everyone for all the help in figuring this out. Source: Tips4all

Bug or hack? $GLOBALS

$GLOBALS["items"] = array('one', 'two', 'three', 'four', 'five' ,'six', 'seven'); $alter = &$GLOBALS["items"]; // Comment this line foreach($GLOBALS["items"] as $item) { echo get_item_id(); } function get_item_id(){ var_dump(key($GLOBALS["items"])); } Check output of this code, with commented and uncommented second line. My result(PHP 5.3.0). With second line int(1) int(2) int(3) int(4) int(5) int(6) NULL Without second line: int(1) int(1) int(1) int(1) int(1) int(1) int(1) Why so strange result?

PHP array comparison algorithm

While trying to simulate a bit of PHP behaviour I stumbled across this: $a=array(0 => 1, 'test' => 2); $b=array('test' => 3, 0 => 1); var_dump($a==$b, $a>$b, $b>$a); According to the output from var_dump $b is bigger than $a . In the PHP manual there is a Transcription of standard array comparison which states that the values of the arrays are compared one by one and if a key from the first array is missing in the second array, the arrays are uncomparable. So far so good. But if I try this (change in the first element of $a only): $a=array(0 => 2, 'test' => 2); $b=array('test' => 3, 0 => 1); var_dump($a==$b, $a>$b, $b>$a); All three comparison results are false . This looks like "uncomparable" to me (because the > result is the same as the < result, while the arrays are not == either, which makes no sense) but this does not fit the transcription from the PHP manual. Both k

What is wrong with my code - circularly sorted array does not show any results

I had an interview today and the person asked me this question: How do you find easily an item in a circularly sorted array Since I didn't know the answer, I tried to find a solution. Here's what I have: Thanks <?php function searchincircularsorterlist($a, $len, $num) { $start=0; $end=$len-1; $mid = 0; while($start<$end) { $mid=$start+$end/2; if ($num == $a[$mid]) { return $num; } if($num<$a[$mid]) { if($num<$a[$start] && $a[$start]<=$a[$start+1]) $start=$mid++; else $end=$mid--; } else { if($num>$a[$end] && $a[$end-1]<=$a[end]) $end=$mid--; else $start=$mid++; } } if ($start == $end && $num == $a[$start]) { return $num; } return -1; } $array = array(7,8,9,0,1,2,3,4,5,6); var_dump(searchincircularsorterlist($array,sizeof(

"Encode&rdquo; integers from an array via a given scheme to characters

Since I'm rather new to Java, I am not familiar with all the ways of handling this type of an assignment. I studied C and C# throughout my high school but I just started Java few days ago. The task goes like this: Write a program that reads 10 single-digit integers and displays a string consisting of 10 characters using the coding scheme: Digit Corresponding Character 0-a 1-b 2-c … … 9-j For example, if input consists of the 10 digits 1 8 6 1 0 3 1 8 5 5, the application responds with "bigbadbiff." Since I moved from C and C# at first I had trouble allowing user to input the integers of the array, but I got that figured out. And this is what I got so far. import java.util.*; public class UnsualCoding { public static void main (String[] args) { Scanner util = new Scanner(System.in); int[] x = new int[5]; int counter = 0; for(int i = 0; i < x.length && util.hasNextInt(); i++) { x[i] = util.nextIn

Java - Order of Operations - Using Two Assignment Operators in a Single Line

What are the order of operations when using two assignment operators in a single line? public static void main(String[] args){ int i = 0; int[] a = {3, 6}; a[i] = i = 9; // this line in particular System.out.println(i + " " + a[0] + " " + a[1]); } Edit: Thanks for the posts. I get that = takes values from the right, but when I compile this I get: 9 9 6 I thought it would have been and ArrayOutOfBounds exception, but it is assigning 'a[i]' before it's moving over the 9. Does it just do that for arrays?

Trying to get the max/min in array

Im trying to get the min value in a column in the row of the max value. Im trying to using for loops but its not working. Let say a matrix A: 3 4 5 2 3 4 1 2 3 I want the program to find the max value in the [0]th row then find the min value in the colunm of the max. So result should be : max of row [0] = 5, min of column [2] = 3. I then want it to do the same of all the rows, that why I ysed a while loop. Here is the matrix: public int[][] createMatrix(int a, int b){ Scanner inputm = new Scanner(System.in); A = new int[a][b]; System.out.println("Enter elements for matrix A : "); for (int i=0 ; i < A.length ; i++){ System.out.println("Enter numbers for " + i +"th row"); for (int j=0 ; j < A[i].length ; j++){ A[i][j] = inputm.nextInt(); } } return A; } public int[][] displayMatrix(){ System.out.println("Matrix A: "); for (int i=0 ; i < A.length ; i++) { System.out.println(); for (int j=

Parsing JSON Feed iOS 5

I'm pretty new to iOS development, and I'm wanting to parse the values of a JSON twitter feed, so I can pull them through into a tableView. So far, I have the information from the feed logging into my console bar as a string, but I'm stuck with where to go next. My current code is: - (void)loadTweets { NSString *twitterURL = [NSString stringWithFormat:@"https://api.twitter.com/1/statuses/user_timeline.json?screen_name=evostikleague&count=%d", tweetCellCount]; NSURL *fullURL = [NSURL URLWithString:twitterURL]; NSError *error = nil; NSData *dataURL = [NSData dataWithContentsOfURL:fullURL options:0 error:&error]; NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]; NSLog(@"Everything:\n%@", strResult); } Which gives me the raw data from the feed: [{"id_str":"171930825225551872","coordinates":null,"in_reply_to_status_id_str":null,"place":null,"po

PHP: Merge/delete arrays with same subkeys

I'm writing a database class for my site based on a fluent interface. First, I collect all the meaningful terms then put them into the "stack", which is basically an array. Then, I sort them in order that they would appear in an actual SQL query. const stmt_select = 1; const stmt_insert = 2; const stmt_delete = 3; const sql_select = 10; const sql_from = 11; const sql_into = 12; const sql_where = 13; const sql_join = 14; const sql_group = 15; const sql_order = 16; const sql_limit = 17; For example, the query below (although in a total rubbish order, and purposely trying to throw the class off): Query::Select('name', 'age', 'height') ->Order('a') ->From('table') ->From('asd') ->Group('a') ->Execute(); .. produces: Array ( [0] => Array ( [0] => 10 [1] => Array ( [0] => name [1]