1

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 :)

6
  • $case-ins in that case is your choice. That is just the variable name, not the key itself. In fact, it wouldn't even be the key, it would be the value. (foreach($arr as $key => $value)) Commented Mar 5, 2016 at 19:12
  • Besides: $a = array('foo-bar' => 'b'); echo $a['foo-bar']; will output b. I think you are confusing yourself here. The $case-ins is the variable name. I might've understood it incorrectly, of course Commented Mar 5, 2016 at 19:14
  • @Anant I cannot use count($par) because I need to get array size that is inside this array... For example I have this array $par that is filled with arrays $i, or $case-ins... and so on. I am sorry if you are stating something obvious, but I am new to php and don't quite understand. Commented Mar 7, 2016 at 20:44
  • 1
    @Hello_World what I meant was that you can't have a variable named $case-ins, it throws syntax error, unexpected '-'. $a-b would at the very least mean that you want the subtract b from a. When you type a foreach, you define the variable name, it doesn't matter the key nor the value. See the error. See it working. Commented Mar 7, 2016 at 21:13
  • 1
    You said: if I used => $value the type of $value would have to be string or integer. If you are referring to the foreach, then no. $value could be anything. Even an object. Take a look at this multi level array example: https://3v4l.org/3pSbA Commented Mar 7, 2016 at 21:23

1 Answer 1

1
$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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.