associative-array

ooPHP - How do I create an array of objects from an associative array?

微笑、不失礼 提交于 2019-12-13 05:20:49
问题 I'm not sure if this is even possible after trying to figure it out for hours but here goes... I have an class, UserPicture, which has properties for filename, filetype, created etc. (i.e. it doesn't store the actual picture as a blob, rather references it by using $filename.$filetype). I want to be able to have a page that displays all of a user's pictures, so I need to retrieve all rows from the DB relevant to that user. I've got the associative array out of the DB successfully and have

Converting form layout to table (csv) layout in perl

心不动则不痛 提交于 2019-12-13 04:28:36
问题 I have an input file inputfile.txt name: George age: 5 nature: curious likes: banana This is what I call a form layout. I am trying to convert this in to a table layout, CSV. For instance: name,age,nature,likes George,5,curious,banana So, I read the file, split on ": and \n" and place values in to a hash. Then push that hash in to an array so I can take them out later. Here is what I have done so far. #!/usr/bin/perl use strict; open (MYFILE, 'inputfile.txt'); my @records; while (<MYFILE>) {

simple array into associative array, based on 'directory tree' like values of the first one

好久不见. 提交于 2019-12-13 02:49:43
问题 For example, I have array Array ( [0] => folder1/file1.txt [1] => folder1/file2.txt [2] => file2.txt [3] => folder2/file1.txt [4] => folder1/subfolder1/file1.txt [5] => folder1/subfolder2/file2.txt [6] => file1.txt [7] => file3.txt [8] => folder1/subfolder2/file1.txt ) I need a clue to figure how to create 'directory tree' array, based on the given values, that it could look like this: Array ( [folder1] => Array ( [0] => file1.txt [1] => file2.txt [subfolder1] => Array ( [0] => file1.txt )

Dynamic associative Array - list, count, sum, min, max

Deadly 提交于 2019-12-13 02:39:46
问题 I've got an array with about 40 keys. I'd like to have a small function that returns a summary array. Right now I've got the following that works: foreach ($all_data as $value){ $new_array[ $value['location'] ][ $value['manufacturer'] ][ $value['model'] ] += 1; } This returns an array with everything I need. However, the location, manufacturer and model could be changed up for a bunch of other values. what I am trying to do is have something simple as: $new_array = summarize($all_data,array(

Retrieve an associative array value with a variable as key

让人想犯罪 __ 提交于 2019-12-13 02:26:01
问题 Here's some code that I have trouble with. I don't know why, I feel I have used this code many times without any problems. $people['firstname'] = "Fred"; $t = "firstname"; echo $people[$t] ; echo returns nothing, whereas i expect it to return Fred. Thanks for your help, Marc 回答1: Not sure why this isn't working for you. $people['firstname'] = 'testvalue'; $key = 'firstname'; $value = $people[$key]; echo $value; Works as expected, echos out "testvalue" Double check your spelling and be

Array creates duplicate tags (wordpress)

北城余情 提交于 2019-12-13 00:36:36
问题 I have an associative array which outputs a list of values. Under each value, there are supposed to be links to wordpress posts with that value. These links should output as: <a href="url">Title</a> For some reason, they output as: <a href="">Title</a><a href="url"></a> It looks like the <a> tag is being created for both title and URL. Here's the code: <?php $the_query = new WP_Query(array( 'post_type' => 'post', 'post_status' => 'publish', 'meta_key' => 'colors', )); $results = []; while (

Compare two associative arrays regarding the order of keys

爱⌒轻易说出口 提交于 2019-12-12 15:16:54
问题 Suppose, we have 2 associative arrays: <?php $array1 = array( 1 => 2, 2 => 1, ); $array2 = array( 2 => 1, 1 => 2, ); They contain same elements, but in different order. I wanted to write a comparison function, that will give true only if: Arrays have the same key => value pairs. Order of pairs is the same. So, I tried the following: 1 try if ($array1 == $array2) { print "equal\n"; } Prints: equal 2 try print count(array_diff_assoc($array1, $array1)); Prints: 0 My custom function Then I

java - Calling a PL/SQL Stored Procedure With Arrays

随声附和 提交于 2019-12-12 14:34:13
问题 I have a PL/SQL stored procedure similar to the following that I need to call in Java: TYPE AssocArrayVarchar20_t is table of VARCHAR2(20) index by BINARY_INTEGER TYPE AssocArrayVarchar4100_t is table of VARCHAR2(4100) index by BINARY_INTEGER TYPE AssocArrayNumber_t is table of NUMBER index by BINARY_INTEGER PROCEDURE DATA_WRITE( I_NAME IN AssocArrayVarchar20_t, I_NUM IN AssocArrayNumber_t, I_NOTE IN AssocArrayVarchar4100_t) // Do Stuff END DATA_WRITE; I tried the following in Java:

How to remove empty associative array entries

六眼飞鱼酱① 提交于 2019-12-12 10:57:55
问题 I have an associative array: $csv_arr Array ( [0] => Array ( [Enfalac] => alpha linolenic acid 300 mg [Enfapro] => alpha linolenic acid 200 mg ) [1] => Array ( [Enfalac] => arachidonic acid 170 mg [Enfapro] => ) [2] => Array ( [Enfalac] => [Enfapro] => ) [3] => Array ( [Enfalac] => calcium 410 mg [Enfapro] => calcium 550 mg ) ) How can I remove all completely empty entries such as $csv_arr[2] but preserve partial entries such as $csv_arr[1] I've tried $csv_arr = array_filter(array_map('array

Javascript associative array modification during for loop

倖福魔咒の 提交于 2019-12-12 10:43:38
问题 The javascript for keyword will iterate over all properties of an object. If the object is modified within the loop body, what happens? For example, is the following code OK? for(var key in obj) if (whatever(obj[key])) delete obj[key]; OK would be if this code works in a deterministic fashion and preferably that all keys in obj are tested exactly once. By contrast, in .NET or Java similar constructs will typically throw an exception. 回答1: I think it works. Just be careful to ask for