associative-array

PHP - associative array as an object [duplicate]

ε祈祈猫儿з 提交于 2019-12-03 16:29:19
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Convert Array to Object PHP I'm creating a simple PHP application and I would like to use YAML files as a data storage. I will get the data as an associative array, with this structure for example: $user = array('username' => 'martin', 'md5password' => '5d41402abc4b2a76b9719d911017c592') However, I would like to extend the associative array with some functions and use the -> operator, so I can write something

Reference key from same array

笑着哭i 提交于 2019-12-03 16:21:09
I'm trying to reference the key/value pair of an item in the same array: $glossary_args = array( 'name' => 'Glossary Terms', 'singular_name' => 'Glossary Term', 'add_new' => 'Add New Term', 'edit_item' => 'Edit Term', 'search_items' => 'Search'.$glossary_args["name"], ) Is this even possible? If so, how? You can use the fact that assignment is itself an expression in PHP: $glossary_args = array( 'name' => ($name = 'Glossary Terms'), 'singular_name' => 'Glossary Term', 'add_new' => 'Add New Term', 'edit_item' => 'Edit Term', 'search_items' => 'Search'.$name ) You can't do this when you're first

Difference between array_filter() and array_map()?

扶醉桌前 提交于 2019-12-03 15:22:45
问题 I looked into the similar topics in web as well stack overflow, but could get this one into my head clearly. Difference between array_map, array_walk and array_filter <?php error_reporting(-1); $arr = array(2.4, 2.6, 3.5); print_r(array_map(function($a) { $a > 2.5; },$arr)); print_r(array_filter($arr, function($a){ return $a > 2.5; })); ?> The above code returns me a filtered array whose value is > 2.5 . Can i achieve what an array_filter does with an array_map ?. 回答1: All three, array_filter

Popping the key & value from an associative array in PHP

≯℡__Kan透↙ 提交于 2019-12-03 14:53:31
问题 Let S be an associative array in PHP, I need to retrieve and extract from it the first element, both the value and the key. I would use value1=array_pop(S); but it only gives me the value. I can use K=array_keys(S); key1=array_pop(K); value1=array_pop(S); but it is complicated because it requires to have two copies of the same data. WHich is a confusing since the array is itself an element in an array of arrays. There must be a more elegant way to just read the couple key/value while

php simpleXMLElement to array: null value

烈酒焚心 提交于 2019-12-03 13:14:20
I've got following XML: <account> <id>123</id> <email></email> <status>ACTIVE</status> </account> I want to have it as an array variable. Therefore I read it with $xml = simplexml_load_file() . The simplest way to convert simpleXMLElement to an associative array I know is to grind it with: json_decode(json_encode((array) $xml),1); The problem is that I don't want to get the email key as an empty array, but rather as a NULL value. As SimpleXMLElement, it looks like: public 'email' => object(SimpleXMLElement)[205] whereas in array it looks like: 'email' => array (size=0) empty I'd like to get:

Javascript: Adding to an associative array

浪尽此生 提交于 2019-12-03 12:19:16
I have a function called insert which takes two parameters (name, telnumber). When I call this function I want to add to an associative array. So for example, when I do the following: insert("John", "999"); insert("Adam", "5433"); I want to it so be stored like this: [0] { name: John, number: 999 } [1] { name: Adam, number: 5433 } Something like this should do the trick: var arr = []; function insert(name, number) { arr.push({ name: name, number: number }); } Would use something like this; var contacts = []; var addContact = function(name, phone) { contacts.push({ name: name, phone: phone });

Use pop() with JavaScript Associative Arrays

僤鯓⒐⒋嵵緔 提交于 2019-12-03 11:52:23
How can I do something like the following in JS? I would like to imitate .pop() on an object rather than an array. var deck = { 'cardK' :'13', 'cardQ' :'12', 'cardAJ':'11' }; var val = deck.pop(); console.log("Key" + val.key ); console.log("Value" + val.val ); It seems like it's not possible. .pop is only available on an array. In JavaScript, objects (which are essentially associative arrays) are not ordered like an array, so there is no .pop method. You could use an array: var deck = [ { key: 'cardK', val: 13 }, { key: 'cardQ', val: 12 }, { key: 'cardAJ', val: 11 }, ]; var val = deck.pop();

create an associative array in jquery

南笙酒味 提交于 2019-12-03 10:54:17
This is what I have so far and the shoe types are boots, wellingtons, leather, trainers (in that order) I want to iterate through and assign the value so I haves something like var shoeArray = { boots : '3', wellingtons: '0', leather : '1', trainers: '3'}; at the moment I just get an array of {3,0,1,3} which I can work with but it is not very helpful. function shoe_types() { var shoeArray = []; $('[type=number]').each(function(){ $('span[data-field='+$(this).attr('id')+']').text($(this).val()); shoeArray.push ( parseInt($(this).val()) ); }); return shoeArray; } Check this function function

Using constants as indices for Javascript Associative Arrays

丶灬走出姿态 提交于 2019-12-03 09:58:01
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 be a constant this.EVENT_NEW_TASK_ADDED_AJAX : { "next_state": STATE_NEW_TASK_ADDED, "action" :

Using an associative array as data for D3

大兔子大兔子 提交于 2019-12-03 09:47:25
I have a very simple D3 example that first reads data into an associative array, then displays it in a bar graph. I can't seem to get anything to display using this method though. Instead, I have to insert a task in between: Read the data into an associative array, copy that data into a simple array, then display the bar graph using the simple array. chart.selectAll("div") .data(genreAssociative) .enter().append("div") .style("width", function(d) { return d * 10 + "px"; }) .text(function(d) { return d; }); The above does not work. genreSimple = []; for (var genre in genreAssociative)