douhuang2673 2016-06-12 11:19
浏览 15

什么是关联数组? [关闭]

QUESTION:

What is an Associative Array?


ELABORATING ON QUESTION:

The syntax I see when searching an Associative Array is as follows:

$myArray = array(
    "car" => "truck", 
    "price" => 50000
);

and I would love to know what the structure of the array does, the purpose of an Associative Array, and some examples so that I can have a complete understanding for future use.

  • 写回答

1条回答 默认 最新

  • douchen2025 2016-06-12 11:20
    关注

    ANSWER:

    The first thing that comes up when a conventional programmer thinks of an array (in PHP) would be something similar to:

    $myArray1 = array(2016, "hello", 33);//option 1
    
    $myArray2 = [2016, "hello", 33];//option 2
    
    $myArray3 = [];//option 3
    $myArray3[] = 2016; 
    $myArray3[] = "hello"; 
    $myArray3[] = 33;
    

    Where, if we wanted to call the array in some later part of the code, we could do:

    echo $myArray1[1];// output: hello
    echo $myArray2[1];// output: hello
    echo $myArray3[1];// output: hello
    

    The previous example showing the use of a Sequential Array (since they increment the index used in a predetermined sequence, by incrementing by 1 for each following value).

    So far so good. However, as humans, we might find it hard to remember that index [0] of the array is the value of the year 2016, index [1] of the array is a greetings, and index [2] of the array is a simple integer value. The alternative we would then have is to use what is called an Associative Array. An Associative array has a few differences from a Sequential Array.

    Differences (between a sequential and associative array):

    • Durring the declaration of an Associative Array, you don't only include the value of what you want to put in the array, but you also put the index value (called the key) which you want to use when calling the array in later parts of the code. The following syntax is used during it's declaration: "key" => "value".

    • When using the Associative Array, the key value would then be placed inside the index of the array to retrieve the desired value.

    For instance:

        $myArray1 = array( 
            "Year" => 2016, 
            "Greetings" => "hello", 
            "Integer_value" => 33);//option 1
    
        $myArray2 = [ 
            "Year" =>  2016, 
            "Greetings" => "hello", 
            "Integer_value" => 33];//option 2
    
        $myArray3 = [];//option 3
        $myArray3["Year"] = 2016; 
        $myArray3["Greetings"] = "hello"; 
        $myArray3["Integer_value"] = 33;
    

    And now, to receive the same output as before, the key value would be used in the arrays index:

    echo $myArray1["Greetings"];// output: hello
    echo $myArray2["Greetings"];// output: hello
    echo $myArray3["Greetings"];// output: hello
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么