遍历多维数组php-获取所有索引并内爆到字符串

时间:2018-11-28 11:26:31

标签: php arrays loops multidimensional-array

我只保留了部分代码,却看不出为什么能得到一定的结果。我的目标是遍历数组并将该结果作为字符串回显:

第一个数组

  1. validate.required
  2. validate.remote

第二个数组

shop.cart.string

当前结果是:

  1. validate.0.required
  2. validate.1.remote

它从数组中返回索引,如何解决这个问题/从我的字符串中删除呢?

  private $translationKeys = [
    'validate' => [
      'required',
      'remote',
      'email',
      'url',
      'date',
      'dateISO',
      'number',
      'digits',
      'creditcard',
      'equalTo',
      'extension',
      'maxlength',
      'minlength',
      'rangelength',
      'range',
      'max',
      'min',
      'step'
    ],
    'shop' => [
        'cart' => [
         'string'
        ],
    ],
];

这是我的功能:

function listArrayRecursive($translationKeys) {
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($translationKeys), RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $k => $v) {
  if ($iterator->hasChildren()) {
  } else {
    for ($p = array(), $i = 0, $z = $iterator->getDepth(); $i <= $z; $i++) {
      $p[] = $iterator->getSubIterator($i)->key();
      $y = array();
      foreach ($p as $value) {
        array_push($y, $value);
      }
    }
    $path = implode('.', $y); 
    $a[] = "$path.$v<br>";
    // Here i want to echo the string
  }
}

}

该功能的第二版

  function listArrayRecursive($translationKeys) {
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($translationKeys), RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $k => $v) {
  if ($iterator->hasChildren()) {
  } else {
    for ($p = array(), $i = 0, $z = $iterator->getDepth(); $i <= $z; $i++) {
      $p[] = $iterator->getSubIterator($i)->key();
    }
    $path = implode('.', $p); 
    $a[] = "$path.$v<br>";
  }
}

}

2 个答案:

答案 0 :(得分:2)

这是一个可以为您提供所需结果的功能。它遍历作为数组的数组的每个元素,将键与值连接在一起,或者如果它不是数组,则仅返回值:

function listArrayRecursive($array) {
    $list = array();
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            foreach (listArrayRecursive($value) as $v) {
                $list[] = "$key.$v";
            }
        }
        else {
            $list[] = $value;
        }
    }
    return $list;
}
print_r(listArrayRecursive($translationKeys));

输出:

Array (
    [0] => validate.required
    [1] => validate.remote
    [2] => validate.email
    [3] => validate.url
    [4] => validate.date
    [5] => validate.dateISO
    [6] => validate.number
    [7] => validate.digits
    [8] => validate.creditcard
    [9] => validate.equalTo
    [10] => validate.extension
    [11] => validate.maxlength
    [12] => validate.minlength
    [13] => validate.rangelength
    [14] => validate.range
    [15] => validate.max
    [16] => validate.min
    [17] => validate.step
    [18] => shop.cart.string 
)

Demo on 3v4l.org

答案 1 :(得分:0)

尝试这样的事情

function disp_array_rec($arr, $upper = null) {
    foreach ($arr as $k => $v) {
        echo ($upper != null ? $upper : "");
        if (is_array($v)) {
            disp_array_rec($v, $k . ".");
        } else {
            echo "$v\n";
        }
    }
}

disp_array_rec($translationKeys);

结果:

validate.required
validate.remote
validate.email
validate.url
validate.date
validate.dateISO
validate.number
validate.digits
validate.creditcard
validate.equalTo
validate.extension
validate.maxlength
validate.minlength
validate.rangelength
validate.range
validate.max
validate.min
validate.step
shop.cart.string