SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get the key for an associative array?

    I have an associative array which stores width/height info.

    I want to grab the key to be used as part of the filename for a thumbnailing.

    Code:
    $resize_array = array
    (
    	"portfolioPage" => array("w" => 100,"h" => 75),
    	"loungePage" 	=> array("w" => 200,"h" => 150),
    	"freehandPage" 	=> array("w" => 288,"h" => 216),
    );
    
    
    /*
    I want to grab the key for each array item
    I want to grab the width element for each array item
    I want to grab the height element for each array item
    I want to use the information to build an thumbnail name, ie:
    portfolioPage_w_h where w and h are replaced by the numbers
    */
    
    // outputting the array, but I can't seem to grab the key
    foreach($resize_array as $rs_field_array => $rs_field):
    	
    	print '<b>'.$rs_field_array . ' -- ' . $rs_field.'</b>';
    	
    	foreach($rs_field as $field=>$value):		
    		?>
    		<?=$field;?> -- <?=$value;?><br />
    		<?php
    	endforeach;
    
    endforeach;

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    foreach($resize_array as $key => $value)
    {
        
    $filename $key '_' $value['w'] . '_' $value['h'];
        echo 
    "$filename<br>\n";

    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    <?php
    $resize_array = array
    (
    	"portfolioPage" => array("w" => 100,"h" => 75),
    	"loungePage" 	=> array("w" => 200,"h" => 150),
    	"freehandPage" 	=> array("w" => 288,"h" => 216),
    );
     
    echo "<pre>";
    foreach ($resize_array AS $page => $dimensions) {
    	$width = $dimensions['w'];
    	$height = $dimensions['h'];
    	echo $page . "\t";
    	echo "W: $width" . "\t";
    	echo "H: $height" . "\n";
    }
    echo "</pre>";


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •