How do I get the number of elements in the second dimension of a two-dimensional array?

I have:

$link["url"][]="http://somewhere";
$link["text"][]="target";
$link["img"][]="./Images/pic.gif";
$link["url"][]="http://somewhereelse";
$link["text"][]="othertarget";
$link["img"][]="./Images/img.gif";

for ($i=0; $i<count($link["url"]); $i++) {
     imagelink($link["url"],$link["text"],$link["img"]);
}

When having error_reporting(E_ALL) I get this error:
Warning: Uninitialized string offset: 0 in /usr/local/etc/httpd/htdocs/project/displayfuncs.inc.php on line xy

Where line is the one with the count()-function in it.

Since php seems to dislike the count-function in this context, what should I use then?

    your idea is right, but your code is wrong. it should look like:

    for( $i=0; $i<count($link["url"]); $i++ ) 
    { 
    	imagelink( $link["url"][$i], $link["text"][$i], $link["img"][$i] ); 
    } 
    

    btw... why don't you store the values this way?

    $images[] = Array( 'url' => 'http://www.google.com', 'text' => 'Google', 'img' => 'google.gif' );
    $images[] = Array( 'url' => 'http://www.hotbot.com', 'text' => 'HotBot', 'img' => 'hotbot.gif' );
    foreach( $images as $image )
    {
    	imagelink( $image['url'], $image['text'], $image['img'] );
    }
    

    this way there's less danger that your index gets out of bounds.

      OK, that does both look a lot cleaner... THX for your hint

      But how do I find out how many of these url/text/img triples I actually have?
      I am using it to build some kind of menu, and with knowing the fixed imagesize of the menu-entries and their number I can calculate the space they are taking up...

      $distanceback=((4-count($link["url"]))*110)+20;
      

        The thing to be aware of, of course, is that there is no well-defined meaning to "the count of second dimension of a two-dimensional array", because PHP doesn't have such a beast. What it has is a one-dimensional array each element of which is itself a one-dimensional array.

        So just because one element of the first dimension happens to have a certain length, that doesn't automatically mean that every other element has the same length.

        On the other hand, if your implementation is such that you can guarantee that each element of the first dimension array is of the same length, then you only need to look at the length of any one of its elements (since we're assuming they're all the same length anyway it doesn't really matter).

        So (still assuming that the three arrays $link['url'], $link['text'] and $link['img'] are all the same length), then there are count($link['url']) triples.

        You should consider trexx's other suggestion, though, and have your triples along the second dimension. That is, instead of $link['url'][$i], have $link[$i]['url']. In other words, have an array of triples, not a triplet of arrays. Then the number of triples is trivial: count($link).

          Ok... I am testing that now... if it works, I'll have to change it at some dozen places, but still...

            Ok, I'm changing it now. Thanks.

              Write a Reply...