0

I am stuck with a part of my code and i can't see to figure out why i get a certain result. What my goal is to loop through the array and echo this result as a string:

First Array

  1. validate.required
  2. validate.remote

Second array

shop.cart.string

Current result is:

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

It returns the index from the array, how can solve this/remove this from my string?

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

​ This is my function:

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
  }
}

}

Second version of the function

  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>";
  }
}

}

1
  • What is $a? all you want is the inner path to all object? Commented Nov 28, 2018 at 11:42

2 Answers 2

2

Here's a function that will give you the result you desire. It recurses through each element of the array that is an array, concatenating the key with the values, or just returns the value if it is not an array:

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));

Output:

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

Sign up to request clarification or add additional context in comments.

Comments

0

try something like this

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);

result:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.