hi,
hi how to check the condition that
array key contains words having only punctuation+alphabetic characters .

ex:
$arr1=array("kis-kuma"=>2,"off-set"=>4,"hello"=>1,123=>5,"fine");
from the above array i have to store in differnt array as keys
which contains both punctuation+alphabetic characters only:
my excepted output is:
$arr1=array("kis-kumar"=>2,"off-set"=>4)

    sounds like a nice task for me to solve, I'll just give you the solution :/

    <?php
    $word_array = array("smeg" => 7, "meaning_of_life" => 44, "handlebars" => 42,"some-key" => 911);
    $result_array = array();
    while($word = current($word_array)){
      if(strpbrk(key($word_array),'-_'))
        $result_array[key($word_array)] = $word;
      next($word_array);
    }
    print_r($result_array);
    ?>
    

    There you go _

    And in function form with comments:

    <?php
    //-----get_punct_keys
    //array get_punct_keys(array $array_in);
    //
    function get_punct_keys($array_in){
      //define the output array
      $result_array = array();
      //loop through each position in array_in
      while($word = current($array_in)){
        //if it has any punctuation in it
        if(strpbrk(key($array_in),'-_\'"\\/.,!£$€%^&*()+=}{[]@~#:;?/><')) // there is of course more possible punctuation
          //then apply it to the output with it's value
          $array_out[key($array_in)] = $word;
        //move the array pointer up one
        next($array_in);
      }
      //return the outcome
      return $array_out;
    }
    
    //make a sample input array
    $word_array = array("smeg" => 7, "meaning_of_life" => 44, "handlebars" => 42,"some-key" => 911);
    //run the test, and print the output array in a readable way
    print_r(get_punct_keys($word_array));
    ?>
    

      I am afraid your solution has a bug, Bozebo: it will accept elements with keys that consist entirely of punctuation. A secondary bug is that the range of punctuation characters is rather narrow, but then kishore2k9 probably should have defined precisely what was meant by punctuation.

      But honestly, instead of fiddling around with current(), key() and next(), Horizon88's suggestion of array_keys() would make things much simpler.

        laserlight;10884221 wrote:

        I am afraid your solution has a bug, Bozebo: it will accept elements with keys that consist entirely of punctuation. A secondary bug is that the range of punctuation characters is rather narrow, but then kishore2k9 probably should have defined precisely what was meant by punctuation.

        But honestly, instead of fiddling around with current(), key() and next(), Horizon88's suggestion of array_keys() would make things much simpler.

        They aren't actually bugs I think you will find, but problems with the initial specification. The thing is, php would not accept the key in the first place if it only consisted of -s and/or _s, also, they are the only characters other than numbers and letters allowed in a php variable name. Although, I couldn't find any specification on php.net of valid keys, so I presumed they are the same as the variables, apart from dashes/hyphens.

        edit: oh, I think a key can actually be any valid string, I hadn't thought of that. As a result I have made a small fix to my function in my previous post.

          The thing is, php would not accept the key in the first place if it only consisted of -s and/or s


          Of course it will: '-' and '
          ' are perfectly valid strings.

          they are the only characters other than numbers and letters allowed in a php variable name. Although, I couldn't find any specification on php.net of valid keys, so I presumed they are the same as the variables.

          An associative array index does not have to be a string that matches the rules for identifiers. It just has to be a string or an integer.

            Oh right "Punctuation + alphabetic characters" I can see now why this "is a rather odd request". Well that would require regular expressions and I am very bad at them. Though, I don't understand why intval would be of any use. And using array_keys would require them to be linked back to the original array to form a proper output.

            edit: (don't want to make another reply just to say this)

            laserlight;10884230 wrote:

            No, one can create a new array if necessary.

            Of course they could make a new array, I never said they couldn't. Just that you would have to link back to the original - ie search the keys to make one with just the keys/values that met what they originally wanted:

            kishore2k9 wrote:

            $arr1=array("kis-kuma"=>2,"off-set"=>4,"hello"=>1,123=>5,"fine");
            from the above array i have to store in differnt array as keys
            which contains both punctuation+alphabetic characters only:
            my excepted output is:
            $arr1=array("kis-kumar"=>2,"off-set"=>4)

            another edit/update:
            I wasn't meaning link as in, somehow join the keys with the old values (without making a new array?). I should have said refer.

              And using array_keys would require them to be linked back to the original array to form a proper output.

              No, one can create a new array if necessary.

                Just that you would have to link back to the original - ie search the keys to make one with just the keys/values that met what they originally wanted:

                That is precisely what the code is supposed to do. In fact, your own code does a linear search of the array keys.

                EDIT:
                Okay, I don't really want to spoonfeed the OP here, but this is what I had in mind:

                <?php
                $array = array(
                    'a' => 1,
                    'ab' => 2,
                    '-a' => 3,
                    '-a_' => 4,
                    'a-' => 5,
                    '-ab' => 6,
                    '_ab-' => 7,
                    'ab_' => 8,
                    'abc123' => 9,
                    'abc123_' => 10,
                    '_-' => 11
                );
                
                $result = array();
                foreach (array_keys($array) as $key) {
                    if (preg_match('/^[a-z_-]*([a-z][_-]|[_-][a-z])[a-z_-]*$/i', $key)) {
                        $result[$key] = $array[$key];
                    }
                }
                
                print_r($result);
                ?>

                I regard this solution as straightforward: it is easy to see that the keys are being processed.

                  Oh, wow, I completely didn't think about just using $array[$key] >_< I had it in my head that using array_keys would separate you from the original array or... something.

                    Write a Reply...