What does this mean exactly? $_SESSION['sessionName '][$item['rowName ']] - c#

And how does it translate to in C#?
$item holds the results from mysql_fetch_array()
I'm not really familiar with PHP so this is all new to me. Thanks.

$_SESSION is a superglobal that stores session data in an associative array, see http://php.net/session.
In your example, the value at $_SESSION['sessionName'] is apparently an array itself, which is indexed into with the value of $item['rowName'] which smells like a string.
To simplify the expression, you could define these variables:
$sessionName = $_SESSION['sessionName'];
$rowName = $item['rowName'];
And then we could say that your example code is equivalent to
$sessionName[$rowName]

Related

Is it possible to get array length inline

I have to access Dictionary<TKey, TValue> value by key (that I get from array I'm creating inline) like so:
var someString = "1.2.3";
someDictionary[someString.Split('.').ToArray()[ /---> self.Length <---/ - 1 ]];
Question: Is it possible to get array Length inline without creating new variable and assigning array to it?
You cannot do this. You need to store intermediate value in a variable if you want to access it twice.
I see no sense in trying to do this without additional variable - at least, your approach is absolutely unreadable.
However, as I understand, by [self.Length - 1] you want to access the last value in this array.
If yes, then you can just use LINQ .Last:
var someString = "1.2.3";
someDictionary[someString.Split('.').Last()]; // someDictionary["3"]

Can I pass a PHP associative array to a ComVisible .NET function?

When I pass a PHP indexed array:
$myVar = new COM('MyClass');
$myVar->DubbleArray([1, 2]);
...it comes through to C# or F# as an System.Object[], and I can deal with it accordingly, e.g.:
[<ComVisible(true)>]
type LettersClass() =
member this.DubbleArray(array: obj[]) =
// do stuff with the values in the array
But when I pass an associative array:
$myVar->DubbleArray(array('first'=>'1st','second'=>'2nd'));
...it comes through as DBNull.
Is there any way to pass an associative array to a .NET function via COM?
Using a Dictionary in .NET allows you to deal with named keys. You could always encode the data in JSON format then decode in .NET as well... :)
(Note: this answer is based off of not seeing any .net in your question)
I suppose it really falls on how marshalling of an associative array is handled on the PHP side. The actual answer very well might be "not at all".
Unless you can figure that out, I would suggest a workaround - my best bet would be to convert the associative array to a multidimensional array storing keys and values in separate columns, and passing that through as an obj[,].

In C# can you split a string into variables?

I recently came across a php code where a CSV string was split into two variables:
list($this->field_one, $this->field_two) = explode(",", $fields);
I turned this into:
string[] tmp = s_fields.Split(',');
field_one = tmp[0];
field_two = tmp[1];
Is there a C# equivalent without creating a temporary array?
Jon Skeet said the right thing. GC will do the thing, don't you worry.
But if you like the syntax so much (which is pretty considerable), you can use this, I guess.
public static class MyPhpStyleExtension
{
public void SplitInTwo(this string str, char splitBy, out string first, out string second)
{
var tempArray = str.Split(splitBy);
if (tempArray.length != 2) {
throw new NotSoPhpResultAsIExpectedException(tempArray.length);
}
first = tempArray[0];
second = tempArray[1];
}
}
I almost feel guilty by writing this code. Hope this will do the thing.
The answer to your quite narrow question is no. C# does not provide a 'multi-assignment' capability, so you cannot extract an arbitrary set of values from anything (such as Split()) and break them out into individual named variables.
There is a workaround for a specific number of variables, by writing a parameter with out arguments. See #vlad for an answer based on that.
But why would you want to? C# provides an impressive range of features that will allow you to take apart strings and deal them with the parts in a such a wide range of different ways that the lack of 'multi-assignment' should barely be noticed.
Parsing strings usually involves other operations such as dealing with formatting errors, trimming white space, case folding. There could be less than 2 strings, or more. Requirements could change over time. When you are ready for a more capable string parser, C# will be waiting.
You may use the following approach:
-Create a class that inherits from dynamic object.
-Create a local dictionary that will store your variables values in the inherited class.
-Override the TryGetMember and TrySetMember functions to get and set values from and into the dictionary.
-Now, you can split your string and put it in the dictionary, then access your variable like:
dynamicObject.var1

How to make arrays values equal in C#?

Let's say that I have 2 string arrays with different values:
string[] sArray1 = new string[3]{"a","b","c"};
string[] sArray2 = new string[3]{"e","f","g"}
And I want to make values of sArray1 equal to values of sArray2 (I know I can write it like this) : sArray1[0] = sArray2[0]; sArray1[1]= sArray2[1]; sArray1[2]=sArray2[2];
For 3 values it's easy, but what if I had 100 values in an array? Is there any other way that I can make array values equal?
p.s. sorry for my bad English :(
Something like this (with a little error checking):
if (sArray2.Length == sArray1.Length)
{
sArray2.CopyTo(sArray1, 0);
}
Regards
I'm assuming you want to keep the reference to the original array in sArray1? Then do this:-
Array.Copy(sArray2, sArray1, sArray1.Length);
If you want them to function independently of each other than you can use .Clone() as of .NET 5.0
string[] sArray1 = (string[])sArray2.Clone();
In the above scenario if you change a value in one array it will not affect the other - this is called a "shallow copy" (AKA copy by val). If you want the values in both arrays to be tied to each other (typically not desirable) you can do a simple assignment like this:
string[] sArray1 = sArray2;
In this case if you change a value in either array the value(s) in the other array will update (AKA copy by ref).

I want to move 1 array to another in C#

This is just a quick question in C#.
I have a scenario where I am working with several devices that all have slightly different data to work with.
When I work out which device I am using, I want to set up a common array to use throughout the code, say arrayCommon.
So I want to move the info from device1 to the common array.
Do I have to do this in a loop for each occurance in the array or can u move the whole array into the common array, as you could in Cobol all those years ago ?
Thanks, George.
I think you are looking for that : Array.Copy
Just a note, if you are needing it in a performance critical section of code, rather use:
Buffer.BlockCopy()
Link here.
Array array = new char["String".Length];
"String".ToCharArray().CopyTo(array, 0);

Categories

Resources