3

I have an array which contains a great deal of data, which has been generated from a JSON file. Most of the data is used to populateinput elements, but some of the keys contain hidden, default values to be used for calculations later.

The array looks something like this:

[name] => 'My App'
[default_E17] => 0.009
[default_H17] => 0.0236
[default_K17] => 50
[default_P17] => 0.0118
[default_E19] => 0.03

I want to loop over all default_* keys, and output them using HTML. Essentially, I want a foreach loop, but only for the keys whose format matches default_*. Does anyone know if this is possible?

Please note that the values in the array before [default_*] keys is variable length, so I cannot easily use an array_splice().

1
  • you have to iterate over the whole array and check the key for a matching pattern Commented Oct 3, 2012 at 12:56

5 Answers 5

7

You use strpos($key, "default_") === 0 to show that it start with default_ and its not in the middle or end

$array = array();
$array['name'] = 'My App';
$array['default_E17'] = "0.009";
$array['default_H17'] = "0.0236";
$array['default_K17'] = "50";
$array['default_P17'] = "0.0118";
$array['default_E19'] = "0.03";
$array['E19_default_test'] = "1.03";


echo "<pre>";

* You can use foreach *

$list = array();
foreach ( $array as $key => $value ) {
    if (strpos($key, "default_") === 0) {
        $list[$key] = $value;
    }
}

var_dump($list);

You can also use array_flip with array_filter

$array = array_flip(array_filter(array_flip($array),function($var) { return (strpos($var, "default_") === 0);}));
var_dump($array);

You can also use FilterIterator

class RemoveDefaultIterator extends FilterIterator {
    public function accept() {
        return (strpos($this->key(), "default_") === 0);
    }
}

$list = new RemoveDefaultIterator(new ArrayIterator($array));
var_dump(iterator_to_array($list));

They would all Output

array
  'default_E17' => string '0.009' (length=5)
  'default_H17' => string '0.0236' (length=6)
  'default_K17' => string '50' (length=2)
  'default_P17' => string '0.0118' (length=6)
  'default_E19' => string '0.03' (length=4)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help. I'd originally thought about this, but wondered if there was a more efficient way than looping through the entire array. It'd appear not!
Please see updated code with the use of === instead because == can return 0 when a boolean response is received
a speed test on each method could be helpful ;)
3
 foreach( $arr as $k => $v ) { //iterate the array
  if( strpos($k, 'default_') !== FALSE  ) //search if the key contains 'default_' 
   $default_values[] = $v;   // if so, store the values for the 'default_' keys   
 }

2 Comments

Ran your code out of curiosity and it does not work see codepad.viper-7.com/9DjEKz . It returns My App which should not be in the list
Edited the conditional check.
2

Just iterate over your array

Foreach( $inputArray AS $key=>$val ) {
  // check if key is the one we need
  if( ... ) {
     // it is - deal with it
  }
}

depending of the keys you use if() can be simple substr() or regexp matching.

Comments

2

You can use a FilterIterator for this.

Its essetially the same as looping over the whole array, because that's the only way really, but it strips you from doing this in your productive loop, thus generating less noise.

Here's how:

class Default_FilterIterator extends FilterIterator
{
    public function accept()
    {
      if (strpos($this->key(), 'default') === 0) {
        return true;
      }
      return false;
    }
}


$yourArray = array('default_stuff' => 'foo', 'otherstuff' => 'bar');

$filterdArray = new Default_FilterIterator(new ArrayIterator($yourArray));

foreach ($filteredArray as $key => $value) {
  //deal only with default values here
}

Comments

1
   foreach ($your_array as $key => $value) {
     // check if the $key starst with 'default_'
     if (substr($key, 0, 8) == "default_") {
      //do your thing...
      echo "<input type='text' value='" . $value . "'>";
     }
   }

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.