associative-array

PHP - Automatically creating a multi-dimensional array

本小妞迷上赌 提交于 2019-12-10 22:48:57
问题 So here's the input: $in['a--b--c--d'] = 'value'; And the desired output: $out['a']['b']['c']['d'] = 'value'; Any ideas? I've tried the following code without any luck... $in['a--b--c--d'] = 'value'; // $str = "a']['b']['c']['d"; $str = implode("']['", explode('--', key($in))); ${"out['$str']"} = 'value'; 回答1: This seems like a prime candidate for recursion. The basic approach goes something like: create an array of keys create an array for each key when there are no more keys, return the

Circularly shifting associative array?

断了今生、忘了曾经 提交于 2019-12-10 22:04:54
问题 Working in PHP, assume you have an associative array: 'Monday' => 'mon' 'Tuesday' => 'tue' 'Wednesday' => 'wed' 'Thursday' => 'thur' 'Friday' => 'fri' 'Saturday' => 'sat' 'Sunday' => 'sun' How could you perform a "circular" array shift? Say shifting things so that the array starts with Wednesday, and proceeds through all 7 days, ending with Tuesday? An important note: I need to do this by key, as I have other code determining what day the shift needs to start at. 回答1: No looping required $arr

Array Unique with Associative Array - Remove Duplicates

我们两清 提交于 2019-12-10 18:24:25
问题 I've got an associative array with some duplicate items. For example, I have: <? $group_array = array('user_id'=>array(), 'user_first'=>array()); Which outputs something like below: Array ( [user_id] => Array ( [0] => 594 [1] => 597 [2] => 594 ) [user_first] => Array ( [0] => John [1] => James [2] => John ) ) I'd like to sanitize this entire array so that only the user John will show up once (based on user_id). I've tried the following: <?php $unique = array_unique($group_array); print_r(

PHP - Make an associative array unique, key -> value and value -> key

放肆的年华 提交于 2019-12-10 17:39:27
问题 I have a little problem in php, which i find hard to explain in words. I have an associative array which contains key-value. I would like to make a function (or if there is already one) which would take an array as input and remove the duplicates but both ways. For example: In my array I have {a -> b} {a -> c} {b -> a} {b -> c} ... From this view it does not seem like there is any duplicate, but to me {a -> b} and {b -> a} are duplicate. So I would like the function to see it as a duplicate

ksh associate array

佐手、 提交于 2019-12-10 14:18:16
问题 I have a script that needs to use associative arrays. Being new to ksh, I am unable to find anywhere that ksh supports associative arrays. When I try to use regular array syntax and assign, I get an error that index cannot be that big. Does ksh support associative arrays? If not, what is the alternative solution? need to do the following: ${array[$name]}=value and later in the code, I need to read value for ${array[$name]}. I have about 2000 values to be stored and read from the array

PHP: How to delete all array elements after an index [duplicate]

本秂侑毒 提交于 2019-12-10 13:38:23
问题 This question already has answers here : php - how to remove all elements of an array after one specified (3 answers) Closed 5 years ago . Is it possible to delete all array elements after an index? $myArrayInit = array(1=>red, 30=>orange, 25=>velvet, 45=>pink); now some "magic" $myArray = delIndex(30, $myArrayInit); to get $myArray = array(1=>red, 30=>orange); due to the keys in $myArray are not successive, I don't see a chance for array_slice() Please note : Keys have to be preserved! + I

How do I access the first key of an ‘associative’ array in JavaScript?

自作多情 提交于 2019-12-10 12:57:30
问题 I have a js 'associative' array, with array['serial_number'] = 'value' serial_number and value are strings. e.g. array['20910930923'] = '20101102' I sorted it by value, works fine. Let's say I get back the object 'sorted'; Now I want to access the first KEY of the 'sorted' array. How do I do it? I can't think I need an iteration with for (var i in sorted) and just stop after ther first one... thanks edit: just to clarify, I know that js does not support associative arrays (that's why I put it

Best way to represent sets of 3 values

放肆的年华 提交于 2019-12-10 12:52:33
问题 Assume that I have 100 sets of 3 values. first value is time, second value is x position and 3rd value is y position. this is going to be the xy position of a point over 100 seconds for example. I have a PHP background. in PHP I would do it with a 3D associative array like this : position["time"]["x"]["y"] so I can access x,y values at a certain time. How would you do this in c#? I think generics like list and dictionary would do that. but I don't know how to implement key values for a 3D set

php associative arrays, regex, array

你离开我真会死。 提交于 2019-12-10 11:56:39
问题 I currently have the following code : $content = " <name>Manufacturer</name><value>John Deere</value><name>Year</name><value>2001</value><name>Location</name><value>NSW</value><name>Hours</name><value>6320</value>"; I need to find a method to create and array as name=>value . E.g Manufacturer => John Deere . Can anyone help me with a simple code snipped I tried some regex but doesn't even work to extract the names or values, e.g.: $pattern = "/<name>Manufacturer<\/name><value>(.*)<\/value>/";

Appending to a hash table in Bash

为君一笑 提交于 2019-12-10 11:38:33
问题 I am trying to make a hash table with the file name of all files in a directory and another integer number. Like "File Name" : "number". The code should be in bash 4.x. This is the code which I wrote: #!/bin/bash DIR=`ls` declare -A ARRAY ZERO=0 for FILES in $DIR do echo "We have $FILES" ARRAY+=(["$FILES"]="$ZERO") done echo "Done with filling up array!" for file in "${ARRAY[@]}" ; do KEY="${file%%:*}" VALUE="${file##*:}" printf "%s has number %s.\n" "$KEY" "$VALUE" done echo "We are done