2

I have an array like that contain key with same name but with number at the end

array example:

Array
(
    [field_name0] => name 
    [field_name1] => sku_package_height 
    [field_name2] => sku_package_width
    [custom_field] => 13
    [attribute] => 'test'
    [field_name3] => sku_package_length 
    [field_name4] => sku_package_weight
)

from the example above i want to count how many record that has array key that contain field_name, so the result I want will be 5

1
  • can you update your question with the actual php array? Commented Dec 26, 2016 at 10:11

4 Answers 4

2

you can do this :

$count = 0;
foreach($array as $key => $value){

   if(strpos($key,"field_name") > -1){
      $count++;
   }

}

$count will have number of keys.

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

3 Comments

the result is 0
let me test it!
try this strpos($key,"field_name") > -1 . updated the answer
1

You can do it like below:-

<?php
$original_array = Array
(
    'field_name0' => 'name',
    'field_name1' => 'sku_package_height',
    'field_name2' => 'sku_package_width',
    'custom_field' => 13,
    'attribute' => 'test',
    'field_name3' => 'sku_package_length',
    'field_name4' => 'sku_package_weight'
);
$search = "field_name";
$counter = 0;
foreach($original_array as $key=> $value){
    if(strstr($key,$search)){
       $counter = $counter+1;
    }
}

echo $counter;

Output:-https://eval.in/704506

Or

<?php
$original_array = Array
(
    'field_name0' => 'name',
    'field_name1' => 'sku_package_height',
    'field_name2' => 'sku_package_width',
    'custom_field' => 13,
    'attribute' => 'test',
    'field_name3' => 'sku_package_length',
    'field_name4' => 'sku_package_weight',
);
$search = "field_name";
$counter = 0;
foreach($original_array as $key=> $value){
    if(is_numeric(strpos($key,$search))){
       $counter = $counter+1;
    }
}

echo $counter;

Output:-https://eval.in/704518

1 Comment

I think strpos should be used like this : strpos($keys_arr,$search)! am I right?
1

Check the isnumeric of the string position "field_name" in key

$i= 0;
foreach($arrayfields as $keys => $values){

   if (is_numeric(strpos($keys,"field_name"))){
      $i++;
   }

}
echo $i;

Comments

0
<?php
$array=array("field_name0"=>"name","field_name1"=>"sku_package_height ","field_name2"=>"sku_package_width","custom_field"=>"13","attribute"=>"test","field_name3"=>"sku_package_length", "field_name4"=>"sku_package_weight");

echo  $arraykey= count(preg_grep("/^field_name(\d)+$/",array_keys($array)));
?>

3 Comments

the result is 0
you have to replay $array with your variable here -> array_keys($array)
checkout updated version you should idea what i'm trying to say

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.