Reputation: 31
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 :)
Upvotes: 1
Views: 988
Reputation: 65
$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.
Upvotes: 1