Assign single value to multiple keys?

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

    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
    <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

    #2
    I dont think you can do that?
    I would do
    PHP Code:
    $val 'one';
    $arr = array(
      
    '1' => $val,
      
    '2' => $val
    ); 
    Mobile chat, iphone chat, android chat, chat, rooms http://www.aiochat.com

    Comment


      #3
      But thats the thing which i would like to avoid if is possible.
      <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

      Comment


        #4
        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')); 
        Advertise your mobile site for FREE with AdTwirl

        Comment


          #5
          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.

          Comment


            #6
            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',
            ); 
            <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

            Comment


              #7
              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

              Comment


                #8
                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
                <?php
                include ('Ghost');
                if ($Post == true) {
                echo '

                sigpic
                alt='coding-talk.com!!' />';
                echo 'Sharing Is Caring!';
                } else {
                echo '

                alt='the username GHOST has been comprimised!' />';
                echo 'OMG SOMEBODY HELP ME!!';
                }
                ?>

                Comment


                  #9
                  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'
                      
                  ); 
                  <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

                  Comment


                    #10
                    your welcome buddy, code looks slicker too ;)
                    <?php
                    include ('Ghost');
                    if ($Post == true) {
                    echo '

                    sigpic
                    alt='coding-talk.com!!' />';
                    echo 'Sharing Is Caring!';
                    } else {
                    echo '

                    alt='the username GHOST has been comprimised!' />';
                    echo 'OMG SOMEBODY HELP ME!!';
                    }
                    ?>

                    Comment


                      #11
                      Yep. Or it can be written like this also.

                      PHP Code:
                          $array array_fill_keys(range(15), 'one')
                          + array(
                              
                      => 'two',
                              
                      => 'three',
                              
                      => 'four'
                          
                      ); 
                      <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

                      Comment

                      Working...
                      X