how do it turn a multidimensional array like:
$fruits['apples']['blue'] = 24;
$fruits['bananas']['blue'] = 12;
$fruits['apple']['red'] = 34;
$fruits['gooseberries']['orange'] = 4;
$fruits['oranges']['red'] = 12;
into a cross referenced table like:
Thanks!
I have this months array:
["January", "March", "December" , "October" ]
And I want to have it sorted like this:
["January", "March", "October", "December" ]
I'm currently thinking in a "if/else" horrible cascade but I wonder if there is some other way to do this.
The bad part is that I need to do this only with "string" ( that is, without using Date object or anything like that )
What would be a good approach?
I have an array of numbers:
@numbers = 1,2,3,6,8,9,11,12,13,14,15,20
and I want to print it this way:
1-3,6,8-9,11-15,20
Any thoughts? Of course I tried using the most common "looping", but still didn't get it.
I have a 4D array of measurements in Matlab. Each dimension represents a different parameter for the measurement. I want to find the maximum and minimum value and the index (i.e. which parameter) of each.
What's the best way to do it? I figure I can take the max of the max of the max in each dimension, but that seems like a kludge.
I have an array of numbers:
@numbers = 1,2,3,6,8,9,11,12,13,14,15,20
and I want to print it this way:
1-3,6,8-9,11-15,20
Any thoughts? Of course I tried using the most common "looping", but still didn't get it.
I'm getting "illegal offset type" error for every iteration of this code. Here's the code in case anyone can help:
$s = array();
for($i=0;$i<20;$i++){
$source = $xml->entry[$i]->source;
$s[$source] += 1;
}
print_r($s)
Any ideas. Thanks in advance.
What language is smart so that it could understand 'variable a = 0 , 20, ..., 300' ? so you could esely create arrays with it giving step start var last var (or, better no last variable (a la infinit array)) and not only for numbers (but even complex numbers...)
Hello, with the hash below, I would like the clients array's reference :
my $this =
{
'name' => $name,
'max_clients' => $max_clients,
'clients' => ()
};
I can't do "\$this{'clients'};" to retrieve the reference.
Hey folks,
I have a variable holding values separated by a comma (Implode), I'm trying to get the total count of the values in that variable however, count() is just returning 1.
I've tried converting the comma separated values to a properly formatted array which still spits out1.
So heres the quick snippet where the sarray session equals to value1,value2,value3:
$schools = $_SESSION['sarray'];
$result = count($schools);
Any help would be appreciated.
This is the first time I have manipulated hashes and arrays in this way -- and it is working. Basically, for every key there are multiple values that I want to record and then print out in the form "key -- value -- value -- val..."
My code is as follows. I am surprised that it works, so concerned that it works "by mistake". Is this the correct way to accomplish this task, or is there a more efficient or appropriate method?
while ($source =~ m/(regex)/g) { #Get all key names from source
$listkey = $1; #Set current list key to the current regex result.
$list{$listkey} = ++$i unless $list{$listkey}; #Add the key to the hash unless it already exists.
$list{$listkey} = [] unless exists $list{$listkey}; #Add an array for the hash unless the hash already exists.
while ($loopcount==0) {
if ($ifcount==0) {
$listvalue=result_of_some_function_using_list_key; #Get the first list value from the list key.
$ifcount++; #Increment so we only get the first list value once.
} else {
$listvalue=result_of_some_function_using_list_value; #Update the last list value.
}
if ($listvalue) { #If the function returned a value...
push @{$list{$listkey}}, $listvalue; #...then add the value to the hash array for the key.
} else { #There are no more values and we need a new key.
$listkey=0; #Reset variable.
$domain=0; #Reset variable.
$loopcount++; #Increment loop counter to exit loop.
}
}
$ifcount=0; #Reset count variable so the next listvalue can be generated from the new key.
$loopcount=0; #Reset count variable so another loop can begin for a new key.
}
foreach $listkey (keys %list) { #For each key in the hash.
print "$listkey --> "; #Print the key.
@values = @{$list{$listkey}}; #Reference the arrays of the hash.
print join ' --> ', @values; #Print the values.
print "\n"; #Print new line.
}
Can I pass arrays to functions just as I would do with primitives such as int and bool?
Can I pass them by value?
How does the function know of the size of the array it is passed?
What would be the best way to extract a column out of mysql results set? My result set is like:
[0] = ('id'=>1, 'name'=>a),
[1] = ('id'=>2, 'name'=>b),
[2] = ('id'=>3, 'name'=>c)
How can I extract 'name' column as an array from this? I can do it using for loop. But wondering if there are any better reusable solution?
Is there any efficient way in python to count the times an array of numbers is between certain intervals? the number of intervals i will be using may get quite large
like:
mylist = [4,4,1,18,2,15,6,14,2,16,2,17,12,3,12,4,15,5,17]
some function(mylist, startpoints):
# startpoints = [0,10,20]
count values in range [0,9]
count values in range [10-19]
output = [9,10]
i need a solution for array_replace_recursive, because my php-version isnt high enough.
i wat to use this code
$_GET = array_replace_recursive($_GET, array("amp"="&"));
easy, isnt it?
Glubber
I just wonder how array_diff() works.And obviously it couldn't work as follows
function array_diff($arraya, $arrayb)
{
$diffs = array();
foreach ($arraya as $keya => $valuea)
{
foreach ($arrayb as $valueb)
{
if ($valuea == $valueb)
{
break;
}
$diffs[$keya]=$valuea;
}
}
return $diffs;
} //couldn't be worse than this
Hope someone can show me some better solution.
Thanks.
If I understood properly you can add value to an array by using :
$myArray[] = 123;
or
array_push($myArray, 123);
Is one cleaner/faster then the other one ?
Hello, how I can use array acces with my static class?
F.e. I like to execute next script:
class A {
...
}
A['p'] = 15;
echo isset(A['p']) ? A['p'] : 0;
Given the following interface
public interface ISomething {
void DoMany(string[] strs);
void DoManyRef(ref string[] strs);
}
I would like to verify that the DoManyRef method is called, and passed any string array as the strs parameter. The following test fails:
public void CanVerifyMethodsWithArrayRefParameter() {
var a = new Mock<ISomething>().Object;
var strs = new string[0];
a.DoManyRef(ref strs);
var other = It.IsAny<string[]>();
Mock.Get(a).Verify(t => t.DoManyRef(ref other));
}
While the following not requiring the array passed by reference passes:
public void CanVerifyMethodsWithArrayParameter() {
var a = new Mock<ISomething>().Object;
a.DoMany(new[] { "a", "b" });
Mock.Get(a).Verify(t => t.DoMany(It.IsAny<string[]>()));
}
I am not able to change the interface to eliminate the by reference requirement.
Hello all,
I've built a small activex control using c#. I've got a function that returns an array of bytes. From some reason when I try to consue the returned array in js I receive an undefined value. Why is this happenning? Is there anyway to solve it?
Here's a simple demonstration of my code:
Activex:
[ComVisible(true)]
public byte[] Close()
{
try
{
MessageBox.Show("called from activex Close");
return Stop();
}
catch (Exception e)
{
//ExceptionHandling.AppException(e);
throw e;
}
}
Javascript Call:
function CloseActiveX(){
var myRslt = document.OurActiveX.Close();
}
his question is about converting between a struct and a byte array. Many solutions are based around GCHandle.Alloc() and Marshal.StructureToPtr(). The problem is these calls generate garbage. For example, under Windows CE 6 R3 about 400 bytes of garbarge is made with a small structure. If the code below could be made to work the solution could be considered cleaner. It appears the sizeof() happens too late in the compile to work.
public struct Data
{
public double a;
public int b;
public double c;
}
[StructLayout(LayoutKind.Explicit)]
public unsafe struct DataWrapper
{
private static readonly int val = sizeof(Data);
[FieldOffset(0)]
public fixed byte Arr[val]; // "fixed" is to embed array instead of ref
[FieldOffset(0)]
public Data; // based on a C++ union
}