Tagged Questions
366
votes
3answers
161k views
Checking if an associative array key exists in Javascript
How do I check if a particular key exists in a Javascript associative array?
If a key doesn't exist and I try to access it, will it return false? Or throw an error?
173
votes
7answers
94k views
How do I remove objects from a javascript associative array?
Suppose I have this code:
var myArray = new Object();
myArray["firstname"] = "Bob";
myArray["lastname"] = "Smith";
myArray["age"] = 25;
Now if I wanted to remove "lastname"?....is there some ...
68
votes
6answers
59k views
In PHP, how do you change the key of an array element?
I have an associative array in the form key => value where key is a numerical value, however it is not a sequential numerical value. The key is actually an ID number and the value is a count. This ...
30
votes
6answers
33k views
Fastest way to implode an associative array with keys
I'm looking for a fast way to turn an associative array in to a string. Typical structure would be like a URL query string but with customizable separators so I can use '&' for xhtml links or ...
16
votes
3answers
5k views
PHP prepend associative array with literal keys?
Is it possible to prepend an associative array with literal key=>value pairs? I know that array_unshift() works with numerical keys, but I'm hoping for something that will work with literal keys.
As ...
16
votes
5answers
12k views
How to determine if an associative array has a key?
In ActionScript 3, is there any convenient way of determining if an associative array (dictionary) has a particular key?
I need to perform additional logic if the key is missing. I could catch the ...
14
votes
9answers
10k views
Slicing a multi-dimensional PHP array across one of its elements
Say for example you just queried a database and you recieved this 2D array.
$results = array(
array('id' => 1, 'name' => 'red' , 'spin' => 1),
array('id' => 2, 'name' => ...
13
votes
4answers
42k views
How to sort a date array in PHP
I have an array in this format:
Array
(
[0] => Array
(
[28th February, 2009] => 'bla'
)
[1] => Array
(
[19th March, 2009] => 'bla'
...
13
votes
11answers
2k views
How to add an array value to the middle of an associative array?
Lets say I have this array:
$array = array('a'=>1,'z'=>2,'d'=>4);
Later in the script, I want to add the value 'c'=>3 before 'z'. How can I do this?
EDIT: Yes, the order is important. ...
12
votes
14answers
9k views
Is there a way to find how how “deep” a PHP array is?
A PHP array can have arrays for its elements. And those arrays can have arrays and so on and so forth. Is there a way to find out the maximum nesting that exists in a PHP array? An example would be a ...
12
votes
6answers
7k views
Associative arrays in C
I am implementing a way to transfer a set of data to a programmable dongle. The dongle is based on a smart card technology and can execute an arbitrary code inside. The input and output data is passed ...
12
votes
4answers
179 views
How does JavaScript [] really work?
I'm writing a JavaScript interpreter for extremely resource-constrained embedded devices (http://www.espruino.com), and every time I think I have implemented some bit of JavaScript correctly I realise ...
10
votes
7answers
23k views
Iterating over a complex Associative Array in PHP
Is there an easy way to iterate over an associative array of this structure in PHP:
The array $searches has a numbered index, with between 4 and 5 associative parts. So I not only need to iterate ...
9
votes
7answers
500 views
In PHP, is there a function that returns an array made up of the value of a key from an array of associative arrays?
I'm sure this question has been asked before, my apologies for not finding it first.
The original array:
[0] => Array
(
[categoryId] => 1
[eventId] => 2
...
8
votes
2answers
2k views
Extracting a subset of values from an associative array (php)
I want to do something seemingly very simple, but I can't find anything about it: simply extract a subset of an array similar to array_splice, but using keys to retrieve the values :
$data = ...
8
votes
6answers
2k views
Formatting associative array declaration
When declaring an associative array, how do you handle the indentation of the elements of the array? I've seen a number of different styles (PHP syntax, since that's what I've been in lately). This ...
7
votes
5answers
3k views
How to pass an associative array as argument to a function in Bash?
How do you pass an associative array as an argument to a function? Is this possible in Bash?
The code below is not working as expected:
function iterateArray
{
local ADATA="${@}" # ...
6
votes
6answers
3k views
How are javascript arrays implemented?
Namely, how does the following code:
var sup = new Array(5);
sup[0] = 'z3ero';
sup[1] = 'o3ne';
sup[4] = 'f3our';
document.write(sup.length + "<br />");
output '5' for the length, when all ...
5
votes
9answers
9k views
PHP - How to loop through a associative array and get the key name?
My associative array:
$arr = array(
1 => "Value1",
2 => "Value2",
10 => "Value10"
);
Using the following code, $v is filled with $arr's values
foreach($arr as $v){
...
5
votes
3answers
1k views
Why do associative arrays don't work in localStorage[“”]?
For example I have the following code:
localStorage["screenshots"] = new Array();
localStorage["screenshots"]["a"] = 9;
alert(localStorage["screenshots"]["a"]);
Arr = new Array();
...
5
votes
6answers
3k views
How do you remove a value that has an empty key from an associative array in PHP?
I have a key that appears to be an empty string, however using unset($array[""]); does not remove the key/value pair. I don't see another function that does what I want, so I'm guessing it's more ...
5
votes
1answer
61 views
Comparing array of keys of associative array to integer indexed array
I have written the following code to check whether an array is associative or not
function is_associative( $arr ) {
$arr = array_keys( $arr );
return $arr != array_keys( $arr );
}
It ...
5
votes
1answer
2k views
Comment associative array in PHP Documentor
Hey Guys,
i hope someone can help me out on this one here.
I use several associative arrays in my php application and i'm using php documentor to comment my sources. I never really did specified ...
5
votes
4answers
468 views
PHP: Expose 'get' and 'set' for object with nested associative arrays
I have a class which stores values with a multi-level associative array:
I need to add a way to access and modify nested values. Here is a working solution for my problem, but it is rather slow. Is ...
5
votes
2answers
1k views
Compare two associative arrays and create a new array with the matched arrays, PHP
I have this two arrays:
$arr1=array( array("id" => 8, "name" => "test1"),
array("id" => 4, "name" => "test2"),
array("id" => 3, "name" => "test3")
...
5
votes
4answers
76 views
re arrange php associative array
I have an array:
$example = array();
$example ['one'] = array('first' => 'blue',
'second' => 'red');
$example ['two'] = array('third' => 'purple',
...
4
votes
3answers
5k views
How do I turn a PHP array into $keys and $values?
If I have an array as $keys => $values, how can I get two arrays of $keys and $values?
4
votes
5answers
7k views
PHP Implode Associative Array
So I'm trying to create a function that generates a SQL query string based on a multi dimensional array.
Example:
function createQueryString($arrayToSelect, $table, $conditionalArray) {
$queryStr = ...
4
votes
4answers
83 views
Selecting an element in an Associative Array in a different way in PHP
Ok I have this kind of associative array in PHP
$arr = array(
"fruit_aac" => "apple",
"fruit_2de" => "banana",
"fruit_ade" => "grapes",
"other_add" => "sugar",
...
4
votes
4answers
5k views
Return first key of associative array in PHP
I'm trying to obtain the first key of an associative array, without creating a temporary variable via array_keys() or the like, to pass by reference. Unfortunately both reset() and array_shift() take ...
4
votes
6answers
86 views
Create an associative array from another array's values only - PHP
I have a simple array where key is always followed by the value:
Array (
[0] => x
[1] => foo
[2] => y
[3] => bar
)
which I would like to convert to associative one:
...
4
votes
2answers
5k views
How to find first/second element of associative array when keys are unknown?
In PHP when you have an associative array, e.g.:
$groups['paragraph'] = 3
$groups['line'] = 3
what is the syntax to access the first or second element of the array when you don't know the value of ...
4
votes
3answers
330 views
Ordered array to associative array, odd values as keys
Pretty straightforward:
// turn
array('foo', 'bar', 'hello', 'world');
// into
array('foo' => 'bar', 'hello' => 'world');
Right now I'm using:
do{
$arrayOut[current($arrayIn)] = ...
4
votes
3answers
1k views
Doing a “Diff” on an Associative Array in javascript / jQuery?
If I have two associative arrays, what would be the most efficient way of doing a diff against their values?
For example, given:
array1 = {
foreground: 'red',
shape: 'circle',
...
4
votes
5answers
1k views
How to remove values from an array whilst renumbering numeric keys
I have an array which may contain numeric or associative keys, or both:
$x = array('a', 'b', 'c', 'foo' => 'bar', 'd', 'e');
print_r($x);
/*(
[0] => a
[1] => b
[2] => c
...
4
votes
2answers
46 views
Access newly added key=>value in associative array during loop
I am trying to add a key=>value pair to an array while using a foreach loop, when that value is added the foreach loop needs to process the new key=>value pair.
$array = array(
'one' => 1,
...
4
votes
7answers
482 views
Javascript Array Object vs Array Like Objects - Clarification
I was trying to stringify an array-like object that was declared as an array object and found that JSON.stringify wasn't processing correctly array-like object when it is defined as an array object.
...
4
votes
3answers
946 views
PHP associative arrays - how to treat integer as string
I have a simple associative array.
$a = array("a"=>"b", "c"=>"d");
I want to check if the key "1" exists in the array, e.g.
isset($a["1"]);
This string is being treated as an integer, so ...
3
votes
7answers
7k views
Can strings be used as an array index?
Can a string be used as array index in C?
Ex:
String Corresponding value
"ONE" 1
"TWO" 2
"FIVE" 5
"TEN" 10
When a string in the above list is passed to the function, the ...
3
votes
3answers
776 views
Multiple foreach without nesting
This first block of code works as expected. It's a foreach to print values from an $fnames key-value array.
foreach($fnames as $fname){
echo $fname;
}
The $fnames array has an $lnames array that ...
3
votes
2answers
332 views
Build associative array based on values of another associative array
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] => ...
3
votes
3answers
5k views
PHP - Merge two arrays (same-length) into one associative?
pretty straightforward question actually..
is it possible in PHP to combine two separate arrays of the same length to one associative array where the values of the first array are used as keys in ...
3
votes
2answers
408 views
php foreach over an array and assignment of this array
I want to add a value to an array while foreaching it :
foreach ($array as $cell) {
if ($cell["type"] == "type_list") {
$cell["list"] = $anObject;
error_log(print_r($cell, TRUE), ...
3
votes
5answers
11k views
PHP Arrays, appending depth of array item recursively to an array with the key of 'depth'
Per the example array at the very bottom, i want to be able to append the depth of each embedded array inside of the array. for example:
array (
53 =>
array (
'title' => ...
3
votes
5answers
208 views
Alternative to PHP's associative arrays in JAVA
In PHP I could use an array with strings as keys. eg
$some_array["cat"] = 123;
$some_array["dog"] = 456;
I just switched to Java and I can't find a data structure capable of doing this. Is this ...
3
votes
2answers
6k views
Awk array iteration for multi-dimensional arrays
Awk offers associative indexing for array processing. Elements of 1 dimensional array can be iterated:
e.g.
for(index in arr1)
print "arr1[" index "]=" arr1[index]
But how this kind done for a ...
3
votes
2answers
115 views
Does php conserver order in associative array? [duplicate]
Possible Duplicate:
Are PHP Associative Arrays ordered?
If I add items to associative array with different keys, does order of addition conserved? How can I access "previous" and "next" ...
3
votes
4answers
104 views
Least verbose way of defining a default array value
Is there a cleaner way to check for array values to prevent PHP notices? Currently I do this:
$email = (isset($_POST['user_email'])) ? $_POST['user_email'] : '';
$first_name = ...
3
votes
3answers
415 views
Add an associated pair to a PHP array
I have an array, i tried writing
array_push($json['Request']['Header'], "key" => "val");
but i received an error. Writing the below works but it adds an array instead of just the key/val
...
3
votes
3answers
103 views
How to update specific key's value in an associative array in PHP?
I've a following associative array named $data
Array
(
[0] => Array
(
[transaction_user_id] => 359691e27b23f8ef3f8e1c50315cd506
[transaction_no] => ...