dpd7195 2011-01-09 17:07
浏览 13

PHP~如何测试完全关联的数组

There is many - good and less good - ways to check associative arrays, but how would you check a "fully associative" array?

$john = array('name' => 'john', , 8 => 'eight', 'children' => array('fred', 'jane'));
$mary1 = array('name' => 'mary', 0 => 'zero', 'children' => array('jane'));
$mary2 = array('name' => 'mary', 'zero', 'children' => array('jane'));

Here $john is fully associative, $mary1 and $mary2 are not.

  • 写回答

2条回答 默认 最新

  • dongle0396 2011-01-09 17:21
    关注

    To make it short, you can't because every array is implemented the same way. From the docs:

    An array in PHP is actually an ordered map. A map is a type that associates values to keys.

    If have no insight in the implementation, but I'm pretty sure that array(1,2,3) is just shorthand for array(0=>1, 1=>2, 2=>3), i.e. in the end it is exactly the same. There is nothing with which you could distinguish that.

    You could only assume that arrays created via array(value, value,...) have an index with 0 and the others have not. But you have already seen that this must not always be the case.

    And every attempt to detect an "associative" array would fail at some point.

    The actual question is: Why do you need this?

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度