associative-array

Ordered array to associative array, odd values as keys

你。 提交于 2019-12-06 04:30:44
问题 Pretty straightforward: // turn array('foo', 'bar', 'hello', 'world'); // into array('foo' => 'bar', 'hello' => 'world'); Right now I'm using: do{ $arrayOut[current($arrayIn)] = next($arrayIn); }while(next($arrayIn)); I'm wondering if there's a way to do it without the intermediary variable, $arrayOut . I could write a function, however this is a single use case, and I'm trying to keep my script uncluttered. I'm just wondering if there's something I missed in the docs that would serve this

How to merge/combine two values into single key in the same array

邮差的信 提交于 2019-12-06 02:24:17
I am not sure what is the appropriate terms to use for my title but just wondering to have below solution where need to merge/combine two values into one within a associative array. For example I have this array: Array ( [1] => Array ( [agent_name_1] => Agent 1 [agent_phone_1] => 0123456 [agent_company_1] => My Company [agent_email_1] => agent@yahoo.com [agent_address_1] => United States ) ) Here I would like to combine company_1 with address_1 . So the output should be like this: Array ( [1] => Array ( [agent_name_1] => Agent 1 [agent_phone_1] => 0123456 [agent_email_1] => agent@yahoo.com

How to loop over and access various elements in an array that is both multidimentional and associative? PHP, either JSON or XML

爱⌒轻易说出口 提交于 2019-12-06 01:12:54
问题 I'm retrieving bibliographic data via an API (zotero.org), and it is similar to the sample at the bottom (just way more convoluted - sample is typed). I want to retrieve one or more records and display certain values on the page. For example, I would like to loop through each top level record and print the data in a nicely formated citation. Ignoring the proper bib styles for the moment, let's say I want to just print out the following for each record returned: author1 name, author2 name,

Reverse an associative array with preserving keys in PHP

与世无争的帅哥 提交于 2019-12-05 21:41:51
问题 I spent half an hour but I haven't found a solution. Following example of an array: array(14) { ["label_text"]=> string(10) "Label text" ["e-mail"]=> string(6) "E-Mail" ["company"]=> string(7) "Company" ["last_name"]=> string(9) "Last name" ["first_name"]=> string(10) "First name" } What I want to do is just reverse the elements, so that the result is this: array(14) { ["first_name"]=> string(10) "First name" ["last_name"]=> string(9) "Last name" ["company"]=> string(7) "Company" ["e-mail"]=>

Quickest way to replace associative array keys with numerical keys

蓝咒 提交于 2019-12-05 20:35:26
I have an array: array('something' => 'like this', 'something' => 'like this', 'something' => 'like this'); I would like to replace it (quickly as possible, using a simple inline function) to be like this: array(0 => 'like this', 1 => 'like this', 2 => 'like this'); Possible using any built-in php-array functions? check out array_values $new_array=array_values($array); print_r($new_array); Rukmi Patel $arr = array( 'something'=>'something', 'something'=>'something' ); $new_arr = array(); $new_arr = array_values(arr); print_r($new_arr); $arr = array( 'something'=>'something', 'something'=>

Build associative array based on values of another associative array

℡╲_俬逩灬. 提交于 2019-12-05 13:57:41
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] => Array ( foo, bar, gir ), [3] => Array ( zim, gaz ), [6] => Array ( dib ) ) Note: , there is no relationship between the keys or values. They are completely arbitrary and used as examples only. The resulting array should be an associative array grouped by the values of the input array. Thanks! $input = array( 'foo' => 1, 'bar' => 1, 'zim' => 3, 'dib' => 6, 'gir' => 1, 'gaz' => 3 ) $output = array(); foreach ( $input as $k => $v ) { if (

JS associative arrays: add new pair

早过忘川 提交于 2019-12-05 13:50:20
问题 I have an associative array in JS. var array = { 'one' : 'first', 'two' : 'second', 'three' : 'third' }; How can I add new pair in it 回答1: array['newpair'] = 'new value'; or array.newpair = 'newvalue'; This is quite a decent read on the subject. 回答2: It's an object literal, not really an "associative array". Just do array['something'] = 'something'; 来源: https://stackoverflow.com/questions/6936636/js-associative-arrays-add-new-pair

php foreach over an array and assignment of this array

风流意气都作罢 提交于 2019-12-05 11:29:50
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), 0); } error_log(print_r($array, TRUE), 0); The first printr is ok but the object added disapear when I leave the loop ant print the array. I guess this is a normal behaviour, what is the best way to work around this "feature" ? Indranil Just call $cell by reference like this: foreach($array as &$cell) {...} And it should retain the value. Passing by reference . When you iterate over the array, $cell is a copy of the

create multidimensional associative array from CSV in PHP

馋奶兔 提交于 2019-12-05 08:54:37
问题 I'm trying to create a multidimensional array in PHP where the inner arrays are associative for the following example CSV string $csv: # Results from 2015-06-16 to 2015-06-16. date,time,label,artist,composer,album,title,duration 2015-06-16,12:00 AM,Island,U2,"Clayton- Adam,The Edge,Bono,Mullen- Larry- Jr",Songs Of Innocence,SONG FOR SOMEONE,03:46 2015-06-16,12:04 AM,Lowden Proud,"Fearing & White, Andy White, Stephen Fearing","White- Andy,Fearing- Stephen",Tea And Confidences,SECRET OF A LONG

How to find first/second element of associative array when keys are unknown?

五迷三道 提交于 2019-12-05 08:05:47
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 the keys ? Is there something like in a C# LINQ statement where you can say: $mostFrequentGroup = $groups->first()? or $mostFrequentGroup = $groups->getElementWithIndex(0)? Or do I have to use a foreach statement and pick them out as I do at the bottom of this code example: //should return "paragraph" echo getMostFrequentlyOccurringItem(array('line', 'paragraph', 'paragraph')); //should return "line"