associative-array

Google Chrome: JavaScript associative arrays, evaluated out of sequence

限于喜欢 提交于 2019-11-28 01:56:03
Ok, so on a web page, I've got a JavaScript object which I'm using as an associative array. This exists statically in a script block when the page loads: var salesWeeks = { "200911" : ["11 / 2009", "Fiscal 2009"], "200910" : ["10 / 2009", "Fiscal 2009"], "200909" : ["09 / 2009", "Fiscal 2009"], "200908" : ["08 / 2009", "Fiscal 2009"], "200907" : ["07 / 2009", "Fiscal 2009"], "200906" : ["06 / 2009", "Fiscal 2009"], "200905" : ["05 / 2009", "Fiscal 2009"], "200904" : ["04 / 2009", "Fiscal 2009"], "200903" : ["03 / 2009", "Fiscal 2009"], "200902" : ["02 / 2009", "Fiscal 2009"], "200901" : ["01 /

JQuery - Serialize form data to Associated Array [closed]

我的未来我决定 提交于 2019-11-27 21:49:49
问题 How do you serialise HTML form data to an Associated Array as opposed to the numeric index format produced by $.serializeArray() using jQuery? The output given by jQuery.serializeArray makes it difficult to directly select values using a numeric index key, Slight index shifts can occur when check boxes are used in form input.. Output of serializeArray [ 0: [name: 'field-1', value: 'val1'], 1: [name: 'check', value: 'val2'], 2: [name: 'check', value: 'val3'] ] Desired Output - More reliable

How to use PHP in_array with associative array?

雨燕双飞 提交于 2019-11-27 20:17:29
Is there any php function such as in_array for associative arrays you get by the mysql function "mysql_fetch assoc" ? For example, if I have an $array that looks like this: array(0=>(array(ID=>1, name=>"Smith"), 1=>(array(ID=>2, name=>"John")) Can I do something like in_array(key,value,array)? Or in my case, if I am looking for the ID value of "1", in_array("ID",1,$array) . This is my solution, comment on it if you think it's the right way: function in_assoc_array($key,$value,$array) { if (empty($array)) return false; else { foreach($array as $a) { if ($a[$key] == $value) return true; } return

Return first key of associative array in PHP

不想你离开。 提交于 2019-11-27 20:07:09
问题 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 the array argument by reference, so neither seem to be viable results. With PHP 5.4 I'll be in heaven; array_keys($array)[0]; , but unfortunately this of course is not an option either. I could create a function to serve the purpose, but I can only imagine there is some concoction of PHP's array_

Read associative array from json in $_POST

筅森魡賤 提交于 2019-11-27 19:07:33
I am using jQuery to post a json object to my php application. jQuery.post("save.php",JSON.stringify(dataToSend), function(data){ alert(data); }); The json string as pulled from firebug looks like this { "data" : [ { "contents" : "This is some content", "selector" : "DIV.subhead" }, { "contents" : "some other content", "selector" : "LI:nth-child(1) A" } ], "page" : "about_us.php" } In php I am trying to turn this into an associative array. My php code so far is <?php $value = json_decode(stripcslashes($_POST)); echo $value['page']; ?> The response to the ajax call should be "about_us.php" but

php compare two associative arrays

ぃ、小莉子 提交于 2019-11-27 16:09:59
问题 i have these two associative arrays // the needle array $a = array( "who" => "you", "what" => "thing", "where" => "place", "when" => "hour" ); // the haystack array $b = array( "when" => "time", "where" => "place", "who" => "you", "what" => "thing" ); i want to check if the $a has a match with the b with it's exact key and value and if each key and value from $a has an exact match in $b .... i want to increment the value of a variable $c by 1 and so on... as we've seen from above there 3

PHP - Merge two arrays (same-length) into one associative?

心不动则不痛 提交于 2019-11-27 15:59:30
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 the associative array? I could ofcourse do this, but I'm looking for another (built-in) function, or more efficient solution..? function Combine($array1, $array2) { if(count($array1) == count($array2)) { $assArray = array(); for($i=0;$i<count($array1);$i++) { $assArray[$array1[$i]] = $array2[$i]; } return $assArray; } } array_combine($keys, $values) PS: Click on my answer! Its also a link! you need

php loop through associative arrays

旧时模样 提交于 2019-11-27 15:54:43
Editing this code here on Stackoverflow and I'm really near to get the result I need. So I have this code posted down here: $friends = $facebook->api('/me/friends'); if(!empty($friends['data'])){ $size = variable_get('facebook_graph_pic_size_nodes','square'); $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http'; foreach($friends['data'] as $data){ $fbid = $data['id']; $fbfriendlikes[$fbid]=$facebook->api('/'.$fbid.'/likes'); } The $fbfriendlikes outputs me an array like this one : http://penelope-ns.net/fb/fig.jpg What do I need to do is save the names in a

Selecting a random element from a PHP associative array

陌路散爱 提交于 2019-11-27 15:52:22
问题 I've got an associative array in PHP and want to select a random key/value pair out of it. Here's what I have so far: Initialize. $locations = array(); Loops through a SQL query and adds key/val pairs: array_push($locations, "'$location_id' => '$location_name'"); Later on, I select a random index of the array: $rand = array_rand($locations); Rand is just a number. So locations[$rand] gives me something like: '1' => 'Location 1' OK great, an assoc array element. At this point, I do not know

How does PHP index associative arrays?

蹲街弑〆低调 提交于 2019-11-27 15:37:59
As I know, in associative arrays , if the keys is not set, it will be set automatically. But it seem doesn't make sense in this case: $a = array( '1' => 'One', '3', '2' => 'Two'); print_r($a); Outputs: Array ( [1] => One [2] => Two ) So where is the '3'? Uchiha Within your user defined array you are assigning the keys manually your array means as array(1 => 'One',3, 2 => 'Two');//[1] => One [2] => 3 [2] => Two Here we have two identical index and as per DOCS its mentioned that the last overwrite the first Syntax "index => values", separated by commas, define index and values. index may be of