0

I have this array

$array = array(
    "one" => "bar",
    "two"    => "21",
    "three"    => "22",
    "four"    => "25",
    "five" => array(
         "xxxxxxxxxxxxxxxxxx" => array(
             "ar1" => "food",
             "ar2" => "dr",
             "ar3" => "ch",
             "ar4" => "ju"
         ),
        "yyyyyyyyyyyyyyyyyyyy" => array(
             "ar1" => "food",
             "ar2" => "dr",
             "ar3" => "ch",
             "ar4" => "ju"
         )),
    "six" => "et",
    "seven" => "op",
    "eight" => "hjs",
    "nine" => array(
         "1" => array(
             "ar5" => "food",
             "ar87" => "dr",
             "ar21" => "ch",
             "ar443" => "ju"
         ),
         "2" => array(
             "73" => "food",
             "82" => "dr",
             "90" => "ch",
             "2123" => "ju"
         )),
    "ten" => "bar",
    "eleven" => "bar",
    "twelve" => "bar"
);

and am finding all the array keys at a given level like this

foreach ($array['five'] as $keyed=> $user) {
foreach ($user as $key => $value) {
       echo "Key: $key; Value: $value Keyed: $keyed<br />\n";
}
}

However,i would like to get all the found array keys for use in array_key_exists so i need to convert the found keys to array.I am trying that by having this array

function the_keys($val){
foreach ($val['five'] as $keyed=> $user) {
foreach ($user as $key => $value) {
    $keyed = array();
}
}
}

but even that does not cast the found keys into an array as i had thought.What do i need to do to cast the found keys to array?.

5
  • Do you want do get all $keyed into an array? Commented Sep 7, 2013 at 13:59
  • Yes,i wan't it into an array. Commented Sep 7, 2013 at 14:00
  • So all you care about, in example given, is that x or y get placed into an array for further access at a later time? Not what x or y contain within them, that can be determined later? Commented Sep 7, 2013 at 14:03
  • So for "level five" you want to have an array('xxx...', 'yyy...') ?? Commented Sep 7, 2013 at 14:03
  • An example of what you want the final output to be would help us understand what you are trying to do. Commented Sep 7, 2013 at 14:04

2 Answers 2

3

To get all keys from $val['five'] you can use array_keys():

$keyed = array_keys($val['five']);

You don't need a loop in that case.

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

Comments

0

Perhaps this would solve your problem

foreach($val['five'] as $arrFive) {
    //Here you have both arrays
    foreach($arrFive as $arrXXXX) {
        //Parse array with index xxxx
    }
    foreach($arrFive as $arrYYYY) {
        //Parse array with index yyyy
    }
}

4 Comments

I'm not clear how this would help - you are just looping over the same array ($arrFive) twice...
$arrFive contains both arrays. The inner loop extracts contents of the array with index xxxxxxx, meanwhile the other inner loop extracts contents of the array with index yyyy
That's not what this code does - $arrFive has the same value in both inner loops, so they are both looping over the same data. The first time round the outer loop, $arrFive will be the xxx... array, the second time the yyy... array, but you don't need to write any code twice for that, that's just what a foreach loop does. By having two inner loops, you are simply visiting every item twice.
Here's a live demo based on your code. I've changed the values in the array slightly so that it's a bit easier to see what's going on.

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.