Assign single value to multiple keys?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • arnage
    replied
    Yep. Or it can be written like this also.

    PHP Code:
        $array array_fill_keys(range(15), 'one')
        + array(
            
    => 'two',
            
    => 'three',
            
    => 'four'
        
    ); 

    Leave a comment:


  • Ghost
    replied
    your welcome buddy, code looks slicker too ;)

    Leave a comment:


  • arnage
    replied
    Thanks guys.
    First i toke Gum's function just instead array_merge i use + because it messed keys, now its like this.

    PHP Code:
        $array array_fill_keys(range(15), 'one');
        
    $array += array(
            
    => 'two',
            
    => 'three',
            
    => 'four'
        
    ); 

    Leave a comment:


  • Ghost
    replied
    array_fill_keys() is pretty much the same also.

    PHP Code:
    <?php
    $keys 
    = array(4567);
    $a array_fill_keys($keys'banana');
    print_r($a);
    ?>
    ///
    ///
    ///
    The above example will output:
    Array
    (
    [4] => banana
    [5] => banana
    [6] => banana
    [7] => banana
    )

    PHP: array_fill_keys - Manual

    Leave a comment:


  • DRX
    replied
    Originally posted by GumSlone View Post
    make a custom function
    like:
    PHP Code:
    function custom_arr($start$end$value)
    {
      for(
    $i=$start;$i<=$end;$i++){$a[$i]=$value;}
      return 
    $a;
    }
    $arr = array( 
            
    => 'two'
            
    => 'three'
            
    ); 
    $arr array_merge($arrcustom_arr(15'one')); 
    your custom function custom_arr() is similar to default array_fill(startkey, num, value)
    PHP: array_fill - Manual

    Leave a comment:


  • arnage
    replied
    Originally posted by GumSlone View Post
    make a custom function
    like:
    PHP Code:
    function custom_arr($start$end$value)
    {
      for(
    $i=$start;$i<=$end;$i++){$a[$i]=$value;}
      return 
    $a;
    }
    $arr = array( 
            
    => 'two'
            
    => 'three'
            
    ); 
    $arr array_merge($arrcustom_arr(15'one')); 
    Thanks, good idea.

    Originally posted by CreativityKills View Post
    I don't know what you are trying to achieve but I've always said this, if it feels like you're forcing codes, then you're probably doing something wrong.
    Just to try to avoid listing each key for the same value. Like when assigning multiple values for one key only the opposite,

    PHP Code:
    $set = array(
        
    'var' => array(56789),
        
    'showshout' => 1,
        
    'csses' => array(
            array(
    'wapforum' => 'Theme Black'), 
            array(
    'wapforums' => 'Theme White'), 
            array(
    'basicblack' => 'Basic Black'), 
            array(
    'basicwhite' => 'Basic White')
        ),
        
    'default_css' => 'wapforum',
    ); 

    Leave a comment:


  • CreativityKills
    replied
    I don't know what you are trying to achieve but I've always said this, if it feels like you're forcing codes, then you're probably doing something wrong.

    Leave a comment:


  • GumSlone
    replied
    make a custom function
    like:
    PHP Code:
    function custom_arr($start$end$value)
    {
      for(
    $i=$start;$i<=$end;$i++){$a[$i]=$value;}
      return 
    $a;
    }
    $arr = array( 
            
    => 'two'
            
    => 'three'
            
    ); 
    $arr array_merge($arrcustom_arr(15'one')); 

    Leave a comment:


  • arnage
    replied
    But thats the thing which i would like to avoid if is possible.

    Leave a comment:


  • kevk3v
    replied
    I dont think you can do that?
    I would do
    PHP Code:
    $val 'one';
    $arr = array(
      
    '1' => $val,
      
    '2' => $val
    ); 

    Leave a comment:


  • arnage
    started a topic Assign single value to multiple keys?

    Assign single value to multiple keys?

    How to assign single value to multiple linear keys?

    For example, this is perfectly wrong:

    PHP Code:
        $array = array(
            array(
    range(15) => 'one'), // also: range(1, 5) => 'one',
            
    => 'two',
            
    => 'three',
            ); 
    Im getting the PHP Warning: Illegal offset type
Working...
X