arrays

Php array_push() vs myArray[]

If I understood properly you can add value to an array by using : $myArray[] = 123; or array_push($myArray, 123); Is one cleaner/faster then the other one ? ...

Inserting and removing a string into/from an array

Hello all! I have an array and variable. If the variable does not exist in the array it has to be added, otherwise it has to be removed. Why the following code does not work? $ar = ["a","b","c"]; $vr = "b"; foreach ($ar as $i => $value) { if ($value == $vr) { unset ($ar[$i]); } else { $ar[] = $vr; $ar = ...

How do I use Ajax and Jquery to get an array from PHP and post it?

Hello, I am using a separate PHP file as a configuration file for everything else on a basic ecommerce site. Here is config/products.php: $product1["short"] = "product one"; $product1["menuimgslideshowsrc"] = "image/product1.jpg"; When I hover over a button, I want to get $product1["menuimgslideshowsrc"] and swap it out with an IMG ...

How to initialize std::vector from C-style array?

What is the cheapest way to initialize a std::vector from a C-style array? Example: In the following class, I have a vector, but due to outside restrictions, the data will be passed in as C-style array: class Foo { std::vector<double> w_; public: void set_data(double* w, int len){ // how to cheaply initialize the std::vector? } ...

Use array as jQuery POST Variables?

When sending data via POST or GET with jQuery you use for format { name:"value" } so i thought, is there a way to do it with this kind of code: var postdata = array(); postdata['name'] = "data"; $.post("page.php", postdata, function(data) { alert(data); } I tried this, and it doesn't seem to work. Is there a proper way to do this?...

Objective C Array and Object Release

Hi, I have a newbie question regarding when to release the elements of a NSArray. See following pseudo code: NSMutalbeArray *2DArray = [[NSMutableArray alloc] initWithCapacity:10]; for (int i=0;i<10;i++) { NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:5]; for (int j=0;j<5;j++) { MyObject *obj = [[MyObject allo...

two arrays defining 2d coordinates, as array indices, in matlab/octave

Hi, I have a 2D array, call it 'A'. I have two other 2D arrays, call them 'ix' and 'iy'. I would like to create an output array whose elements are the elements of A at the index pairs provided by x_idx and y_idx. I can do this with a loop as follows: for i=1:nx for j=1:ny output(i,j) = A(ix(i,j),iy(i,j)); end end H...

PHP How to create comma separated list from array?

Hi All, I know how to loop through items of an array using foreach and append a comma, but it's always a pain having to take off the final comma. Is there an easy PHP way of doing it? $fruit = array('apple', 'banana', 'pear', 'grape'); Ultimately I want $result = "apple, banana, pear, grape" ...

PHP: How to copy elements from an associative array and place them at the beginning of the array?

I have an array of countries that I will be using in a select menu: array( [0] => " -- Select -- " [1] => "Afghanistan" [3] => "Albania" [4] => "Algeria" [39] => "Canada" [47] => "USA" ) //etc... I want to copy create copies of the Canada and USA entries and place them at the front of my array. So the array sho...

php Checking if value exists in array of array

I have an array within an array. $a = array ( 0 => array ( 'value' => 'America', ), 1 => array ( 'value' => 'England', ), ) How do I check if 'America' exists in the array? The America array could be any key, and there could be any number of subarrays, so a generalized solution please. Looking on the php manual I see in_array, but th...

php Getting the 'new' values in an array

Trying to learn about php's arrays today. I have a set of arrays like this: $a = array ( 0 => array ( 'value' => 'America', ), 1 => array ( 'value' => 'England', ), 2 => array ( 'value' => 'Australia', ), ) $b = array ( 0 => array ( 'value' => 'America', ), 1 => array ( 'value' => 'England', ), 2 => arra...

How do I declare an array as a constant in Objective-c?

The following code is giving me errors: // constants.h extern NSArray const *testArray; // constants.m NSArray const *testArray = [NSArray arrayWithObjects: @"foo", @"bar"]; The error I get is initializer element is not constant Or if I take away the pointer indicator (*) I get: statically allocated instance of Objective-C class ...

PHP: Count values of array

I know the function count() of php, but what's the function for counting how often a value appear in an array? Example: $array = array( [0] => 'Test', [1] => 'Tutorial', [2] => 'Video', [3] => 'Test', [4] => 'Test' ); Now I want to count how often "Test" appears. ...

Array inside of Arrays; Java (Help with Lab Assignment)

We are working on a lab assignment for my CS&E class and I think I can ask this question without going into detail of the entire lab requirements, but is it possible for an array to be inside of an array? For example, would this work: int [] arrayOne = new int[3]; arrayOne[0] = Start of an array If this is possible how do you go about...

Variable Scope Problem, iPhone

Hello all, I need some help here so I will do my best to explain. I have worked on this all day and had no success (just learning!) I have: NSArray *getValue(NSString *iosearch) { mach_port_t masterPort; CFTypeID propID = (CFTypeID) NULL; unsigned int bufSize; kern_return_t kr = IOMasterPo...

How can I initialize a 2d array in Perl?

How do I initialize a 2d array in perl? I am trying the following code: 0 use strict; 10 my @frame_events = (((1) x 10), ((1) x 10)); 20 print "$frame_events[1][1]\n"; but it gives the following error: Can't use string ("1") as an ARRAY ref while "strict refs" in use at ./dyn_pf.pl line 20. This syntax only seems to initialize a ...

Button to add value to array and then compare result with another array on IPhone.

I have ten buttons that each correspond to a different number. I'm looking to record the order that these buttons are pressed and enter them into an array and then compare it to another array that is static in the app. The check should be done on the Nth button press, where N equals the number of items in the other, static array. How do...

What is array portability?

From this page: http://www.doctrine-project.org/documentation/manual/1_2/en/working-with-models#dealing-with-relations:creating-related-records You can see that it says $obj['property']; is the recommended way of referring to an object's property in Doctrine for array portability purposes. I never heard about this term before and goog...

Simple PHP Array Problem - IF EXIT

Hi, how can i convert this into an array? if someone searches for "lo" he gets the text "no query", but how can i do this for more words? i tried it with array('1','2').. if ($query == 'lo') { exit ('No Query.'); } i want something like this if ($query == 'lo', 'mip', 'get') { exit ('No Query.'); } so, if someone types mip he g...

How do i add all the values of this multidimensional array or turn it into an array by its values.

The array is here: Array ( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 1 [1] => 6 [2] => 0 ) ...