tags:

views:

38

answers:

3

Okay so the array is $nv = array();

$nv[$kk] = $value;

And index key is $kk a variable. If you have $kk then you can access them as the variables $kk via the index key $kk?

But what if I did this:

$nv[$kk][$jj] = $value;

I can now access the variables $kk and $jj both assigned to $value, even though they are index keys, sounds powerful and scary at the same time.

Question#1: I am a noob and was just wondering if this is the correct interpretation of the code above; furthermore, what are the benefits of this structure?

Question#2: What else can you do with this structure that seems, so very powerful, unless if I am wrong?

Apparently just copying the entire structure "$nv[$kk][$jj] = $value;" into Google does not yield significant results and most examples on php arrays don't seem to really explain this in detail.

  $nv = array();
  foreach($vars as $key => $value)
  {
    $kk = "#".strtoupper($key)."#";
    $nv[$kk] = $value;
  }
  unset($vars);
  $tdata = strtr($nv,$tdata);
  return true;

Or from php.net

A multi-dimensional array, okay, so the first index can be for row and the second for column in terms of accessing "a"?

$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

Just need some clarification, I am trying to build a solid foundation for my understanding of php concepts, thank you in advance, please no flaming for the noob.

+1  A: 

That expression $nv[$kk][$jj] is dimensional access. Try to run this in php.

$k="k";
$j="j";
$a[$k][$j]=10;
print_r($a);
echo "\n";

This will print:

Array
(
    [k] => Array
        (
            [j] => 10
        )

)

So k is the first dimension and j is another.

You can use print_r or var_dump to print all elements in the array.

Hope this helps.

NawaMan
Thanks, so $k => Array and $j => 10?I was thinking $k and $j both get 10, if you don't do the assignment $k = "k"; and $j = "j";
Newb
And what if I did not initialize the variables as above for $k and $j and just printed them out like this?print_r($a[$k][$j]);So that $k and $j both get 10.Legal or illegal?
Newb
If you didn't initialize the variables, you will get similar array but with null as keys (empty keys). Like this: `Array ( [] => Array ( [] => 10 ) )`.
NawaMan
A: 

What you are looking at is a Multidimensional Array.

You would typically use these for organizing your data into groups and subgroups. I personally tend to avoid going much farther than a few levels in order to avoid unnecessary complexity.

If you were to var_export your array var_export($nv) you should be able to see the structure of the array when you execute your code.

In order to expand your knowledge I would highly recommend taking a good look at the PHP Documentation on arrays to avoid any confusion/misconceptions.

houmam
+1  A: 

Thinking "multi-dimensional array" as a Fortran type matrix will lead you astray!

$hash_of_hashes[$a][$b] = "bonjour";

is really shorthand for:-

$tempar = array();
$tempar[$b] = "bonjour";
$hash_of_hashes[$a] = $tempar;

php has a single "array" structure which can be an array or a hash depending on usage. Arrays are single dimensional an array entry can be any valid php structure a scaler, another array or a class. And you can mix all of these within a single array.

So rather than "multidimensional array" think "a hash with an entry thats another hash"

James Anderson
$a = array();$a[0][0] = "a";$a[0][1] = "b";$a[1][0] = "y";$a[1][1] = "z";print_r($a[0][0]);// will print a and so on?So is this wrong?
Newb
$hash_of_hashes[$a][$b] = "bonjour";print_r($hash_of_hashes[$a][$b]);Gives me bonjour, so is this the wrong way to think about arrays?
Newb
It works for the limited case where you populate the array as if it was a fortran type multi dimensional array. But you can do $myarray[0] = 1; myarray[1] = array(); myarray[1][0] = 2; myarray[1][1] = new myObject(); This clearly is not a staighforward multidimensional array!
James Anderson