【问题标题】:How to get values for multiple keys?如何获取多个键的值?
【发布时间】:2019-06-12 16:00:49
【问题描述】:

我想使用多个键获取值。 我有一个适用于单个键的 php 代码,但我想获取多个键的值。我该怎么做?

<?php
$arr=array(
'1'=>'India',
'2'=>'Canada',
'3'=>'United',
'4'=>'China',
'5'=>'London',
'6'=>'New Delhi',
);

$key1='4';
$key2='3';
$key3='4';
echo $arr[$key1, $key2, $key3];

?>

我希望以正确的顺序输出这样的输出

China
United
China

提前致谢。

【问题讨论】:

  • 您尝试过.. umm.. 分别呼应它们吗?这是当前无效的语法
  • 您必须回显 $arr[$key1],然后回显 $arr[$key2],然后回显 $arr[$key3]。这不是 Matlab 或 Sas。您不能将向量作为索引传递。
  • echo $arr[$key1], $arr[$key2], $arr[$key3]; ?
  • 谢谢兄弟!我们可以在单行中定义所有键,例如 $key='4','3','4';并得到这样的输出 eho $arr[$key];
  • @GopalMeena 否。如上所述,键不能是索引向量。它必须是 SINGLE 索引。

标签: php arrays


【解决方案1】:

我们在 PHP 中有接口 ArrayAccess: https://www.php.net/manual/en/class.arrayaccess.php

所以我们可以编写如下代码来支持多个键(更新了上页的示例): 您必须对其进行更新以满足您的要求。

<?php

class MultipleKeyArray implements ArrayAccess {

    private $container = array();
    private $separator = ',';

    public function __construct($arr ) {
        $this->container = $arr;
    }

    public function setSeparator($str){
        $this->separator = $str;
    }

    public function offsetSet($offsets, $values) {
        $os = explode(',',$offsets);
        $vs = explode(',',$values);
        $max = max(count($os),count($vs));
        for($i=0;$i<$max;$i++){
          $offset = $os[$i];
          $value  = $vs[$i];
          if (is_null($offset)) {
            $this->container[] = $value;
          } else {
            $this->container[$offset] = $value;
          }
        }
    }

    public function offsetExists($offsets) {
        $os = explode(',',$offsets);
        for($i=0;$i<count($os);$i++){
            $offset = $os[$i];
            if( !isset($this->container[$offset]) ){
                return false;
            }
        }
        return true;
    }

    public function offsetUnset($offsets) {
        $os = explode(',',$offsets);
        for($i=0;$i<count($os);$i++){
          $offset = $os[$i];
          unset($this->container[$offset]);
        }
    }

    public function offsetGet($offsets) {
        $os = explode(',',$offsets);
        $result = '';
        for($i=0;$i<count($os);$i++){
          $offset = $os[$i];
          $result .= ($i>0 ? $this->separator:'') . (isset($this->container[$offset]) ? $this->container[$offset] : '');
        }
        return $result;
    }
}

$arr=array(
    '1'=>'India',
    '2'=>'Canada',
    '3'=>'United',
    '4'=>'China',
    '5'=>'London',
    '6'=>'New Delhi',
);

$o = new MultipleKeyArray($arr);
$o[] = 'new0';
$o['f,g']='new1,new2';

var_dump(isset($o['f,g']));
var_dump(isset($o['1,2,f']));
var_dump(isset($o['f,not,there']));

echo $o['4,3,4']."\n";
echo $o['2,f,g']."\n";

$o->setSeparator("|");
echo $o['4,3,4']."\n";

输出:

bool(true)
bool(true)
bool(false)
China,United,China
Canada,new1,new2
China|United|China

【讨论】:

  • 不错的一个! +1。我建议在输入和键中也使用分隔符,因为有人可能想做echo $o['4|3|4']."\n";
  • 非常感谢@hytest 兄弟!
【解决方案2】:

PHP 索引不能使用数组 - 你应该使用循环或 PHP 数组函数。

首先定义你需要的键的数组:

$keys = [$key1, $key2, $key3];

现在使用foreach 循环将它们回显为:

foreach($keys as $k)
    echo $arr[$k] . PHP_EOL;

还有单线:

array_walk($keys, function($k) use ($arr) {echo $arr[$k] . PHP_EOL;});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2019-04-09
    • 2012-04-26
    • 1970-01-01
    • 2022-07-01
    • 2018-05-19
    • 1970-01-01
    相关资源
    最近更新 更多