开发者

Variables as index keys?

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.


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.


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"


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜