SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How to tell if the keys is defined in an array?

    What I'm trying to do: Make a generic way to output <select> lists given an array.

    Say there are three arrays:

    PHP Code:
         $r1 = array('Hello','World');
         
    $r2 = array('Bar'=>'Foo','Hello'=>'World');
         
    $r3 = array(0=>'First',1=>'Second'); 
    What I want $r1 to do is, set the keys such that if it's not explicitly defined - to use it's value as the key. So that I can output:

    HTML Code:
        <select name='foo'>
            <option value='Hello'>Hello</option>
            <option value='World'>World</option>
        </select>
    While the others will set it's value attribute according to what's defined.

    Any ideas?

  2. #2
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $r1 array_flip($r1); 

  3. #3
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    What your asking for is impossible, as when you assign the array like this:
    Code:
    $r1 = array('Hello','World');
    PHP will assign "Hello" to the key 0 and "World" to the key 1.

    With other words they will have keys assigned to them.

    This means that as long as you allow integer keys to be counted as "assigned" keys, there is no way to differentiate if it was a key you set yourself, or if it was auto assigned.

    In the event that you do only count string keys as assigned ones, then you can easily just check the key to see if it is a string or integer and if its an integer to use the value for key.

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TheRedDevil View Post
    What your asking for is impossible, as when you assign the array like this:
    Code:
    $r1 = array('Hello','World');
    PHP will assign "Hello" to the key 0 and "World" to the key 1.

    With other words they will have keys assigned to them.

    This means that as long as you allow integer keys to be counted as "assigned" keys, there is no way to differentiate if it was a key you set yourself, or if it was auto assigned.

    In the event that you do only count string keys as assigned ones, then you can easily just check the key to see if it is a string or integer and if its an integer to use the value for key.
    yeah that's the conclusion I was at, was just wondering if there's some method PHP has, in the unlikely event that it in fact keeps track of who defined the integer index.

  5. #5
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    The easiest way to make this work, is most probably to assign the value as a key when you initiate the array if possible.

    I.e. that if you dont have a key set, you use the value as a key right there, instead of trying to find out later on.

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Why not just store an array of arrays?

    Code PHP:
    <?php
    $options = array(
        array('foo'),
        array(1, 'bar')
    );

    You then just have to count how many items are in the array.

    Code PHP:
    <?php
    $options = array(
    	array('foo'),
    	array(1, 'bar')
    );
     
     
    foreach($options as $option)
    {
    	if(1 === count($option))
    	{
    		$key = $value = $option[0];
    	}
    	else
    	{
    		list($key, $value) = $option;
    	}
    	echo "<option value=\"{$key}\">{$value}</option>", PHP_EOL;
    }
     
     
    /*
    	<option value="foo">foo</option>
    	<option value="1">bar</option>
    */

    Anthony.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    That option would be more resource demanding than to set them from the start, since that would also require you to know if the key exists or not.

    A more resource friendly option would be something like this.

    PHP Code:
    function prepareArray(&$options$value$key=null) {
        
    $options[(($key === null)?$value:$key)] = $value;
        }

    $options = array();

    //When you assign the values you have you just do, if you are uncertain that there is a key or not
    prepareArray($options$array_value, ((empty($array_key))?null:$array_key));

    //Or if your certain that the value has no key
    prepareArray($options$array_value);

    //Or if your certain it has a key
    prepareArray($options$array_value$array_key); 


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
  •