3

I want to print this array to all indexes upto 21, but in this code this is printing only to array length, what i should i do the print whole array in for loop?

<?php
$array=array(0=>"hello",
             1=>"world", 
             2=>"this", 
             3=>"is", 
             4=>"an", 
             20=>"array",
             21=>"code" );

$length=count($array);

for($i=0;$i<$length;$i++){
         echo "$i=>".$array[$i]; 
         echo "<br />";

      }
?>
15
  • why not do $i<21 ? and update your output to check if the value exists? Commented Mar 5, 2013 at 22:00
  • Change this for($i=1;$i<=$length;$i++){ to for($i=0;$i<$length;$i++){ Commented Mar 5, 2013 at 22:01
  • It should be $i < $length, not $i <= $length. Commented Mar 5, 2013 at 22:01
  • then every time insert an element i will have to change the length, that's the problem. Commented Mar 5, 2013 at 22:02
  • 2
    I am seriously curious; why can't a foreach loop be used? Commented Mar 5, 2013 at 22:37

4 Answers 4

2

Your difficulty is the way you're defining your array:

$array=array(0=>"hello",
             1=>"world", 
             2=>"this", 
             3=>"is", 
             4=>"an", 
             20=>"array",
             21=>"code" );

Arrays in php are really hashmaps; when you call index 5 on the above array, it is undefined. No index item up to 20 will be defined, and these will Notice out:

PHP Notice:  Undefined offset:  5

Because you're using array length as your iterating variable, and calling exactly that variable, you will never get positions 20 and 21 in your code.

This is what your array looks like to the computer:

0 => "hello"
1 => "world"
2 => "this"
3 => "is"
4 => "an"
5 => NULL
6 => NULL
7 => NULL
... //elided for succinctness 
19 => NULL
20 => "array"
21 => "code"

When you call $array[7] it can't return anything. When you call $array[20] it will return "array".

What you really want is a foreach loop:

foreach($array as $key => $val) {
    //key will be one of { 0..4, 20..21}
    echo "$key is $value\n";
}

Resulting in:

$ php test.php 
0 is hello
1 is world
2 is this
3 is is
4 is an
20 is array
21 is code

If you must use a for loop:

$key_array = array_keys($array);
for($i=0;$i<count($key_array);$i++){
   $key = $key_array[$i];
   echo "$key => ".$array[$key]."\n";
}

Note this is not a clean solution.

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

4 Comments

I have asked particularly to use for loop.
Editted for an explicit for loop.
thanx, Nathaniel that will do it.
@NathanielFord +1 for the array_keys() solution, that slipped of my thought !
2

Solution with a for loop:

$array=array(0=>"hello",
             1=>"world", 
             2=>"this", 
             3=>"is", 
             4=>"an", 
             20=>"array",
             21=>"code" );

$max = max(array_flip($array)); // What if max array key is 10^5 ?
for($i=0;$i<=$max;$i++){
    if(isset($array[$i])){
        echo "$i=>".$array[$i]."<br>";
    }
}

5 Comments

Note that the poster here has a very salient point if your array is quite large; the performance impact could be meaningful. This solution checks every possible key between 0 and... whatever. Please look into where in a hash map the letter 'a' goes, for instance.
@NathanielFord Yes you're absolutely right, but it's the SO's wish to do it this way :p
There are cleaner ways to do this, though; you can get all the keys in an array, and just iterate through those rather than iterating through all possible keys: this sort of programming could quickly kill a web app if it has to search every number index before getting to an 'a' in the array.
@user2113060 you should go for Nathaniel Ford's solution, it's more efficient !
@HamZaDzCyberDeV, ok thnx, I would go for that!
1
foreach($array as $key=>$value){
    echo $key."=>".$value; 
    echo "<br />";
}

3 Comments

sorry, I mentioned in for loop.
ah sorry..why only a for loop and not a foreach though?
I want to know if there is a way to print in for loop?
-1

You want to start your loop with $i=0 as PHP uses zero indexing. also in your loop you want to cap your max value for interation at $i

  <?php
    $array=array(0=>"hello",
                 1=>"world", 
                 2=>"this", 
                 3=>"is", 
                 4=>"an", 
                 20=>"array",
                 21=>"code" );

    $length=count($array);

    for($i=0;$i<$length;$i++){
             echo "$i=>".$array[$i]; 
             echo "<br />";

          }
    ?>

5 Comments

but still it won't print index 20 and 21.
This code is non-functioning: you will get an array index out of bounds exception when it attempts to access '5', which does not exist in this array.
yeah that problem, Nathaniel, i want to print whole array without getting that.
Why has it to be a for loop? Foreach no possibility?
I have been asked particularly to use for loop.

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.