Questions tagged [arrays]

An array is an ordered data structure consisting of a collection of elements (values or variables), each identified by one or multiple indexes. When asking about specific variants of arrays, use these related tags instead: [vector], [arraylist], [matrix]. When using this tag, in a question which is ...

1005
votes
28answers
1.4m views

“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP

I'm running a PHP script, and continue to receive errors like:Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php on line 10Notice: Undefined index: my_index C:\wamp\...
687
votes
20answers
418k views

Access / process (nested) objects, arrays or JSON

I have a nested data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or keys)?For example:var data={code: 42,...
226
votes
23answers
501k views

What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

What does ArrayIndexOutOfBoundsException mean and how do I get rid of it? Here is a code sample that triggers the exception:String[] name={ "tom", "dick", "harry" };for (int i=0; i <=name....
1599
votes
25answers
742k views

Why is using “for…in” with array iteration a bad idea?

I've been told not to use for...in with arrays in JavaScript. Why not?
260
votes
9answers
52k views

How can I sort arrays and data in PHP?

This question is intended as a reference for questions about sorting arrays in PHP. It is easy to think that your particular case is unique, and worthy of a new question, but most are actually minor ...
1642
votes
31answers
1.8m views

What's the simplest way to print a Java array?

In Java, arrays don't override toString(), so if you try to print one directly, you get the className + @ + the hex of the hashCode of the array, as defined by Object.toString():int[] intArray=new ...
950
votes
43answers
545k views

How to randomize (shuffle) a JavaScript array?

I have an array like this:var arr1=["a", "b", "c", "d"];How can I randomize / shuffle it?
305
votes
8answers
41k views

What is array decaying?

What is decaying of an array? Is there any relation to array pointers?
441
votes
5answers
117k views

How do I use arrays in C++?

C++ inherited arrays from C where they are used virtually everywhere. C++ provides abstractions that are easier to use and less error-prone (std::vector<T> since C++98 and std::array<T, n> ...
2015
votes
39answers
1.0m views

Sort array of objects by string property value

I have an array of JavaScript objects:var objs=[ { first_nom: 'Lazslo', last_nom: 'Jamf' },{ first_nom: 'Pig', last_nom: 'Bodine' },{ first_nom: 'Pirate', last_nom: '...
47
votes
4answers
32k views

How can I access an array/object?

I have the following array and when I do print_r(array_values($get_user));, I get:Array ([0]=> 10499478683521864[1]=> 07/22/1983[2]=> email@saya.com...
3985
votes
29answers
3.5m views

For-each over an array in JavaScript?

How can I loop through all the entries in an array using JavaScript?I thought it was something like this:forEach(instance in theArray)Where theArray is my array, but this seems to be incorrect.
931
votes
29answers
648k views

How to create a generic array in Java?

Due to the implementation of Java generics, you can't have code like this: public class GenSet<E> {private E a[];public GenSet() {a=new E[INITIAL_ARRAY_LENGTH]; // error: ...
931
votes
9answers
649k views

Sort Multi-dimensional Array by Value

How can I sort this array by the value of the "order" key? Even though the values are currently sequential, they will not always be.Array([0]=> Array([hashtag]=> ...
257
votes
13answers
303k views

How to find the 'sizeof' (a pointer pointing to an array)?

First off, here is some code:int main() {int days[]={1,2,3,4,5};int *ptr=days;printf("%u\n", sizeof(days));printf("%u\n", sizeof(ptr));return 0;}Is there a way to ...
3381
votes
44answers
2.2m views

How do I check if an array includes an object in JavaScript?

What is the most concise and efficient way to find out if a JavaScript array contains an object?This is the only way I know to do it:function contains(a, obj) {for (var i=0; i < a.length;...
55
votes
8answers
72k views

Arrays, linked lists and other data structures in cmd.exe (batch) script

I was playing with cmd.exe, but in its help I didn't find any info, how to define arrays.I have found, how to define simple variables:set a=10echo %a%But, I want to create arrays, linked list ...
1077
votes
25answers
781k views

Sorting an array of JavaScript objects

I read the following objects using Ajax and stored them in an array:var homes=[{"h_id": "3","city": "Dallas","state": "TX","zip": "75201","price": ...
732
votes
49answers
626k views

How to compare arrays in JavaScript?

I'd like to compare two arrays... ideally, efficiently. Nothing fancy, just true if they are identical, and false if not. Not surprisingly, the comparison operator doesn't seem to work.var a1=[1,2,...
167
votes
9answers
53k views

Is an array name a pointer?

Is an array's name a pointer in C?If not, what is the difference between an array's name and a pointer variable?
2569
votes
38answers
3.0m views

Loop through an array in JavaScript

In Java you can use a for loop to traverse objects in an array as follows:String[] myStringArray={"Hello", "World"};for (String s : myStringArray){// Do something}Can you do the same in ...
6622
votes
69answers
5.3m views

How do I remove a particular element from an array in JavaScript?

I have an array of integers, and I'm using the .push() method to add elements to it.Is there a simple way to remove a specific element from an array? The equivalent of something like array.remove(...
1476
votes
17answers
86k views

With arrays, why is it the case that a[5]==5[a]?

As Joel points out in Stack Overflow podcast #34, in C Programming Language (aka: K & R), there is mention of this property of arrays in C: a[5]==5[a]Joel says that it's because of pointer ...
972
votes
54answers
984k views

Remove duplicate values from JS array [duplicate]

I have a very simple JavaScript array that may or may not contain duplicates.var names=["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];I need to remove the duplicates and put the unique ...
1178
votes
39answers
711k views

How do you convert a byte array to a hexadecimal string, and vice versa?

How can you convert a byte array to a hexadecimal string, and vice versa?
98
votes
6answers
17k views

Is Chrome's JavaScript console lazy about evaluating arrays?

I'll start with the code:var s=["hi"];console.log(s);s[0]="bye";console.log(s);Simple, right? In response to this, Firebug says:["hi"]["bye"]Wonderful, but Chrome's JavaScript console ...
1461
votes
30answers
713k views

Copy array by value

When copying an array in JavaScript to another array:var arr1=['a','b','c'];var arr2=arr1;arr2.push('d'); //Now, arr1=['a','b','c','d']I realized that arr2 refers to the same array as ...
521
votes
21answers
180k views

Can I bind an array to an IN() condition?

I'm curious to know if it's possible to bind an array of values to a placeholder using PDO. The use case here is attempting to pass an array of values for use with an IN() condition. I'd like to ...
268
votes
13answers
121k views

Why aren't variable-length arrays part of the C++ standard?

I haven't used C very much in the last few years. When I read this question today I came across some C syntax which I wasn't familiar with.Apparently in C99 the following syntax is valid:void foo(...
1195
votes
30answers
1.1m views

Find object by id in an array of JavaScript objects

I've got an array:myArray=[{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}, etc.]I'm unable to change the structure of the array. I'm being passed an id of 45, and I want to get 'bar' for that ...
927
votes
63answers
892k views

Get all unique values in a JavaScript array (remove duplicates)

I have an array of numbers that I need to make sure are unique. I found the code snippet below on the internet and it works great until the array has a zero in it. I found this other script here on SO ...
148
votes
10answers
79k views

How to Update Multiple Array Elements in mongodb

I have a Mongo document which holds an array of elements.I'd like to reset the .handled attribute of all objects in the array where .profile=XX.The document is in the following form: {"...
431
votes
21answers
742k views

How do I declare a 2d array in C++ using new?

How do i declare a 2d array using new?Like, for a "normal" array I would:int* ary=new int[Size]butint** ary=new int[sizeY][sizeX]a) doesn't work/compile and b) doesn't accomplish what:...
1742
votes
21answers
3.9m views

How do I declare and initialize an array in Java?

How do I declare and initialize an array in Java?
137
votes
17answers
89k views

Accessing an array out of bounds gives no error, why?

I am assigning values in a C++ program out of the bounds like this:#include <iostream>using namespace std;int main(){int array[2];array[0]=1;array[1]=2;array[3]=3;...
790
votes
64answers
449k views

Merge/flatten an array of arrays in JavaScript?

I have a JavaScript array like:[["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]]How would I go about merging the separate inner arrays into one like:["$6", "$12", "$25", ...]
1053
votes
64answers
799k views

How to merge two arrays in JavaScript and de-duplicate items

I have two JavaScript arrays:var array1=["Vijendra","Singh"];var array2=["Singh", "Shakya"];I want the output to be:var array3=["Vijendra","Singh","Shakya"];The output array should have ...
339
votes
44answers
345k views

Split array into chunks

Let's say that I have an Javascript array looking as following: ["Element 1","Element 2","Element 3",...]; // with close to a hundred elements.What approach would be appropriate to chunk (split) ...
188
votes
25answers
370k views

Random shuffling of an array

I need to randomly shuffle the following Array:int[] solutionArray={1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1};Is there any function to do that?
40
votes
2answers
3k views

Correctly allocating multi-dimensional arrays

The intent of this question is to provide a reference about how to correctly allocate multi-dimensional arrays dynamically in C. This is a topic often misunderstood and poorly explained even in some C ...
201
votes
25answers
181k views

How to Flatten a Multidimensional Array?

Is it possible, in PHP, to flatten a (bi/multi)dimensional array without using recursion or references?I'm only interested in the values so the keys can be ignored, I'm thinking in the lines of ...
260
votes
17answers
197k views

How to remove duplicate values from a multi-dimensional array in PHP

How can I remove duplicate values from a multi-dimensional array in PHP?Example array:Array([0]=> Array([0]=> abc[1]=> def)[1]=> Array(...
162
votes
6answers
66k views

How come an array's address is equal to its value in C?

In the following bit of code, pointer values and pointer addresses differ as expected.But array values and addresses don't!How can this be?Outputmy_array=0022FF00&my_array=0022FF00...
526
votes
22answers
580k views

How to convert a byte array to a hex string in Java?

I have a byte array filled with hex numbers and printing it the easy way is pretty pointless because there are many unprintable elements. What I need is the exact hexcode in the form of: 3a5f771c
834
votes
19answers
1.6m views

How to initialize all members of an array to the same value?

I have a large array in C (not C++ if that makes a difference). I want to initialize all members to the same value. I could swear I once knew a simple way to do this. I could use memset() in my case, ...
244
votes
12answers
495k views

Passing a 2D array to a C++ function

I have a function which I want to take, as a parameter, a 2D array of variable size. So far I have this: void myFunction(double** myArray){myArray[x][y]=5;etc...}And I have ...
95
votes
13answers
70k views

Why isn't the size of an array parameter the same as within main?

Why isn't the size of an array sent as a parameter the same as within main?#include <stdio.h>void PrintSize(int p_someArray[10]);int main () {int myArray[10];printf("%d\n", ...
521
votes
60answers
381k views

How to get the difference between two arrays in Javascript?

Is there a way to return the difference between two arrays in JavaScript?For example:var a1=['a', 'b'];var a2=['a', 'b', 'c', 'd'];// need ["c", "d"]Any advice greatly appreciated.
261
votes
33answers
232k views

Most efficient method to groupby on a array of objects

What is the most efficient way to groupby objects in an array?For example, given this array of objects:[ { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },{ Phase: "Phase ...
16
votes
9answers
22k views

What is a NumberFormatException and how can I fix it?

Error Message:Exception in thread "main" java.lang.NumberFormatException: For input string: "Ace of Clubs"at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at ...

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,