简体   繁体   中英

Array key with the dash php 5

In my code in php 5 I need to access array key which have a dash in its name(case-ins). Is there any way to do that?

My code looks like this:

 $count = 0;
 foreach($par as $key){
      foreach($key as $case-ins)
           $count = $count+1;
      ....   
 }

Basically I need to get array size. I know I can probably use the count function, but right now the biggest problem I am dealing with is the dash. I have found on the internet something like ${case-ins}. but it didn't work. I can't change name of array key because it is actually argument from command line which I got using getopt.

Could you please help me with this? Or is there any other way around to count how many times was same argument used?

Thank you for all the answers :)

$par = array(
            array(
                '0'=> 'dark-blue',
                '1'=> 'yellow',
                '2'=> 'high-color'
                ),
            );

$count = 0;
foreach ($par as $key ) {   
    foreach ($key as ${'case-ins'} ) {
        if (preg_match('#-#', ${'case-ins'} )==true) {
            $count = $count+1;
        }       
    }
}
echo $count; // count is 2 ..

More info: in some strict PHP systems you better use #(.+)?-(.+)?# instead of #-# and instead of ${'case-ins'} you may use normal variables like $case_ins , not dash in PHP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM