0

I have an array in PHP defined like this:

array(4) {
  [2]=>
  array(35) {
    [64055]=>
    int(1)
    [63682]=>
    int(1)
    [63441]=>
    int(1)
    [63180]=>
    int(1)
    [62867]=>
    int(1)
    [62866]=>
    int(1)
    [62801]=>
    int(1)
    [62425]=>
    int(1)
    [61557]=>
    int(1)
    [61432]=>
    int(1)
    [60777]=>
    int(1)
    [60473]=>
    int(1)
    [60181]=>
    int(1)
    [56466]=>
    int(1)
    [54520]=>
    int(1)
    [54368]=>
    int(1)
    [52155]=>
    int(1)
    [52090]=>
    int(1)
    [51399]=>
    int(1)
    [51081]=>
    int(1)
    [48540]=>
    int(1)
    [45649]=>
    int(1)
    [45099]=>
    int(1)
    [43147]=>
    int(1)
    [39122]=>
    int(1)
    [37309]=>
    int(1)
    [29849]=>
    int(1)
    [28732]=>
    int(1)
    [23916]=>
    int(1)
    [23644]=>
    int(1)
    [23351]=>
    int(1)
    [21351]=>
    int(1)
    [16970]=>
    int(1)
    [16781]=>
    int(1)
    [16763]=>
    int(1)
  }
  [6]=>
  array(1) {
    [63854]=>
    int(1)
  }
  [4]=>
  array(7) {
    [62921]=>
    int(1)
    [58863]=>
    int(1)
    [50981]=>
    int(1)
    [49118]=>
    int(1)
    [36078]=>
    int(1)
    [27718]=>
    int(1)
    [21813]=>
    int(1)
  }
  [21]=>
  array(1) {
    [38328]=>
    int(1)
  }
}

How can I loop through this array to get the digits [2], [6], and [4]? I want to print out those digits. Anyone who can help me with this? I can't figure it out how to do an foreach to do this.

4
  • You should use foreach. Commented Oct 23, 2014 at 13:35
  • @DavidePastore: Yes, I know.. Commented Oct 23, 2014 at 13:36
  • @Arif_suhail_123: Yes, But I want to print out the number 2 6 and 4 Commented Oct 23, 2014 at 13:37
  • RTFM foreach ($array as $KEY => $value) Commented Oct 23, 2014 at 13:41

4 Answers 4

2

Since they are the keys of your array elements, array_keys() should do:

$keys = array_keys($array);

To print them, comma seperated:

echo implode(', ', array_keys($array));
Sign up to request clarification or add additional context in comments.

Comments

0

Georges answer si the best but if you wanted a foreach its

foreach ($array as $key => $value) {
    echo $key;
}

Comments

0

Use foreach:

$result = array();
foreach ($array as $key => $value){
    array_push($result, $key)
}

Comments

0
foreach($array as $key=>$val) {
   if($key == 2 || $key == 4 || $key == 6)
      print_r($array[$key]);
}

Hope this helps :D

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.