dplase3140 2016-12-09 14:37
浏览 12
已采纳

PHP数组键名

For a project i found out i have to start using array intersect. http://php.net/manual/en/function.array-intersect.php

There i found this code which explains what the function does:

<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>

The output is what confuses me.

(
    [a] => green
    [0] => red
)

What do a and 0 mean in this case?

Shouldn't it be 0 1 why does it start with a and then goes to 0.

  • 写回答

3条回答 默认 最新

  • dpdhsq0783 2016-12-09 14:45
    关注
    $array1 = array("a" => "green", "red", "blue");
    $array2 = array("b" => "green", "yellow", "red");
    $result = array_intersect($array1, $array2);
    

    First of all we must write those dictionaries as they really are; the form above is "condensed", with implicit numeric keys.

    $array1 is a: green, 0: red, 1: blue
    $array2 is b: green, 0: yellow, 1: red
    

    Now array_intersect checks what values are common. They are green, and red. The corresponding items from array1 are taken:

    a: green
    0: red
    

    and so you have two keys, 'a' and 0, mapped to green and red respectively.

    At this point I'll add that mixing numeric and non-numeric keys is a recipe for disaster, unless you're incredibly careful about what you're doing; lots of PHP functions will not preserve keys and renumber the values converting dictionaries to arrays.

    And as you have seen, it's not immediate to tell an array from a dictionary. To add to the risk, JSON encoding is completely different for the two, so a tiny change in a structure might make a Web service conversation to abruptly collapse.

    JSON

    This is my favourite parlor trick.

    $arr = array( 'Hello', 'World' );
    

    This JSON-encodes, as you would expect, to:

    [ 'Hello', 'World' ]
    

    Let's say I delete the last element and re-JSON:

    [ 'Hello' ]
    

    But let's say I delete an element that is not the last. What does PHP do? It removes the element and the key, but does not renumber the array. The array has now a hole. And arrays don't have holes -- dictionaries do.

    So this is now a dictionary. And in JSON it suddenly becomes:

    { "1": "World" }
    

    Which means that this example code is subtly bugged:

    $arr = functionReturningArrayOfElements();
    
    if (-1 != $killThisElement) {
        unset($arr[$killThisElement]);
    }
    
    header('Content-Type: application/json');
    die(json_encode($arr));
    

    When $killThisElement is the very last entry ($count($arr)-1), then the JSON will be encoded as an array. Otherwise, it will be encoded as a dictionary.

    Before returning, I need to be sure of what I return:

    $arr = array_values($arr); // This renumbers the keys, forcing it to always be an array
    

    or

    $arr['count'] = count($arr);
    // This adds a non-numeric key, forcing $arr to always be a dictionary.
    // The extra key is called 'count' just so it makes sense, but it is also
    // a BAD IDEA, since it encourages to loop through the object instead of
    // using the proper Javascript Object methods. A better choice from this
    // point of view would be
    $arr['comment'] = 'This is a dictionary.';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在