associative-array

Are arrays implicitly created in PHP when one of its keys are assigned something?

馋奶兔 提交于 2020-01-03 18:32:07
问题 Just wishing to quickly verify this. It is different from my immediate experience from other languages whereby an array must first be declared before it can be filled with values. 回答1: Yes, PHP will automatically create an array given any of the following $foo[] = $bar; $foo[1] = $bar; $foo['bar'] = $bar; // and of course $foo = array(); // and soon to pass $foo = [1, 2, 3]; 回答2: PHP will create the array even without being implicitly declared, yes. $array[] = ... $array would be a valid

Are arrays implicitly created in PHP when one of its keys are assigned something?

走远了吗. 提交于 2020-01-03 18:32:04
问题 Just wishing to quickly verify this. It is different from my immediate experience from other languages whereby an array must first be declared before it can be filled with values. 回答1: Yes, PHP will automatically create an array given any of the following $foo[] = $bar; $foo[1] = $bar; $foo['bar'] = $bar; // and of course $foo = array(); // and soon to pass $foo = [1, 2, 3]; 回答2: PHP will create the array even without being implicitly declared, yes. $array[] = ... $array would be a valid

PHP associative arrays - how to treat integer as string

喜欢而已 提交于 2020-01-03 09:12:09
问题 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 that echo $a["1"]; //prints "d" How do I get it to treat it as a string? I don't want to use array_key_exists or in_array because my benchmarking shows isset will be a lot faster. 回答1: It doesn't appear that you can do what you want to do. from http://us.php.net/manual/en/language.types.array.php: A key may

Bash array assignment fails if you declare the array in advance

一世执手 提交于 2020-01-03 08:41:59
问题 This works: $ BAR=(a b c) $ echo "${BAR[1]}" b This, however, doesn't: $ declare -A FOO $ FOO=(a b c) bash: FOO: a: must use subscript when assigning associative array bash: FOO: b: must use subscript when assigning associative array bash: FOO: c: must use subscript when assigning associative array The docs claim the subscript is optional: Arrays are assigned to using compound assignments of the form name=(value1 ... valuen) , where each value is of the form [subscript]=string . Only string

array_splice() - Numerical Offsets of Associative Arrays

允我心安 提交于 2020-01-03 02:12:06
问题 I'm trying to do something but I can't find any solution, I'm also having some trouble putting it into works so here is a sample code, maybe it'll be enough to demonstrate what I'm aiming for: $input = array ( 'who' => 'me', 'what' => 'car', 'more' => 'car', 'when' => 'today', ); Now, I want to use array_splice() to remove (and return) one element from the array: $spliced = key(array_splice($input, 2, 1)); // I'm only interested in the key... The above will remove and return 1 element (third

Convert an indexed array into an associative array in Bash

旧时模样 提交于 2020-01-01 17:57:30
问题 At present, I’m struggling to find solution to either of the following problems: how to convert a normal array (indexed array with index starting at 0) into an associative array where value becomes a key and value itself is the value. Create a new assoc array from indexed array where values are keys. And this in a single statement. I know it can very well be done using a loop but for a huge sized array containing almost 500,000 elements, a loop is an overhead. Create an assoc array from the

Using constants as indices for Javascript Associative Arrays

混江龙づ霸主 提交于 2020-01-01 04:01:32
问题 I'm looking to create an associative array in JS, but use constants defined as part of the class as indices. The reason I want this is so that users of the class can use the constants (which define events) to trigger actions. Some code to illustrate: STATE_NORMAL = 0; STATE_NEW_TASK_ADDED = 0; this.curr_state = STATE_NEW_TASK_ADDED; this.state_machine = { /* Prototype: STATE_NAME: { EVENT_NAME: { "next_state": new_state_name, "action": func } } */ STATE_NEW_TASK_ADDED : { // I'd like this to

PHP : Remove duplicate values from associative array and return an associative array containing the duplicate values

♀尐吖头ヾ 提交于 2019-12-31 07:17:51
问题 I have associative array like below $arr = [1=>0, 2=>1, 3=>1, 4=>2] I would like to remove the duplicate values from the initial array and return those duplicates as a new array. So I would end up with something like; $arr = [1=>0, 4=>2] $new_arr = [2=>1, 3=>1] Does PHP provide such a function or if not how would I achieve this? 回答1: Try: Use array_filter() to get all duplicate values from array Use array_diff() to get all unique values from array $array = array(1=>0, 2=>1, 3=>1, 4=>2);

PHP Store Key Value from Associative Array into Simple Array

爱⌒轻易说出口 提交于 2019-12-31 03:22:08
问题 I'm having trouble wrapping my head around this, any help would be GREAT ... I have an array $stores that is structured like so: Array ( [0] => Array ( [id] => 123 [name] => 'Store A' ) [1] => Array ( [id] => 345 [name] => 'Store B' ) [2] => Array ( [id] => 567 [name] => 'Store C' ) [3] => Array ( [id] => 789 [name] => 'Store D' ) ) I want to extract the 'id' values from this array into a simple array that looks this: $simple = array(123,345,567,789); 回答1: If you use php 5.5+, array_column()

Multiple MYSQL queries vs. Multiple php foreach loops

断了今生、忘了曾经 提交于 2019-12-30 18:43:09
问题 Database structure: id galleryId type file_name description 1 `artists_2010-01-15_7c1ec` `image` `band602.jpg` `Red Umbrella Promo` 2 `artists_2010-01-15_7c1ec` `image` `nov7.jpg` `CD Release Party` 3 `artists_2010-01-15_7c1ec` `video` `band.flv` `Presskit` I'm going to pull images out for one section of an application, videos on another, etc. Is it better to make multiple mysql queries for each section like so: $query = mysql_query("SELECT * FROM galleries WHERE galleryId='$galleryId' &&