【问题标题】:array_keys returns in addition to correct results wrong resultsarray_keys 除了正确结果返回错误结果
【发布时间】:2019-02-11 15:11:47
【问题描述】:

(问题已编辑) 我一直在为这个脚本苦苦挣扎,但无法让它正常工作。

$array = array(
    0 => array(
        'description'     => 'Permanent modification of a ship\'s shield facilities.',
        'hasTypes'        => 'false',
        'iconID'          => 2703,
        'marketGroupID'   => 954,
        'marketGroupName' => 'Shield Rigs',
        'parentGroupID'   => 943
    ),
    1 => array('description'     =>
        'Equipment modules for spaceships, including many types of weapon, electronic, engineering, armor, shield and propulsion systems',
        'hasTypes'        => 'false',
        'iconID'          => 1432,
        'marketGroupID'   => 9,
        'marketGroupName' => 'Ship Equipment',
        'parentGroupID'   => 'TesT'
    )
);

$keys = array_keys(array_column($array, 'parentGroupID'), 'TesT',true);

var_dump($keys);

这可行,但完整的脚本会返回 parentGroupID 值不同于 TesT 的键。

complete script with full array

在完整脚本中添加以下内容表明在 array_keys 返回的所有键上 parentgroupid 不等于 TestT。

$response = array();
foreach($keys as $key){
$response[$key] = $array[$key]['parentGroupID'];


}

var_dump($response);

【问题讨论】:

  • 输出是什么,您希望发生什么?
  • 它完全按原样返回,匹配。 3v4l.org/ti0oZ
  • d3vi0ussander,您需要编辑您的问题并添加有关您希望看到的@JensV 建议的信息。说“误报”和“完整数组”之类的东西太抽象了;人们需要详细了解您要实现的目标才能提供帮助:)
  • 一段时间后,我能够添加整个脚本来揭示问题。
  • 您的“带有完整数组的完整脚本”按预期工作。 $col = array_column($array, 'parentGroupID'); foreach ($keys as $k) echo $col[$k], PHP_EOL;所有值都是"TesT"

标签: php arrays


【解决方案1】:

使用

$op = array();
foreach($ar as $value){
if($value['parentGroupID'] === 'TesT'){
array_push($op,$value);
}

}

而不是

$keys = array_keys(array_column($array, 'parentGroupID'), 'TesT',true);

给了我正确的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 2011-01-15
    • 1970-01-01
    • 2021-05-05
    • 2020-04-28
    • 1970-01-01
    相关资源
    最近更新 更多