1

I have an array of categories getting values from database

<?php foreach ($category_1['children'] as $category_2) { ?>
  <li><a href="<?php echo $category_2['href']; ?>">
<?php echo $category_2['name']; ?></a>
<?php } ?>

It returns values like

8.5" x 14" (Tri-Fold) 8.5" x 2.75" 8.5" x 3.667" (1/3 Page) 8.5" x 5.5" 8.5" x 5.5" (1/2 Page) 80lb gloss book, 16 Page Wall Calendar 9" x 12" Envelope 9" x 14.5" Booklets/Catalogs Bookmarks Brochures Business Cards Calendars

The problem is that I want to remove the values which have numbers in it and display the values with names only. For eg.

Booklets/Catalogs Bookmarks Brochures Business Cards Calendars

So how can I remove the values with numbers from the array. Can it be done with the array_filter using wildcard.

6 Answers 6

3

The solution using array_filter and preg_match functions:

...

$category_1['children'] = array_filter($category_1['children'], function($v){
    return !preg_match("/\d/", $v["name"]);
});
// now the $category_1['children'] array contains only items with needed "names" 
Sign up to request clarification or add additional context in comments.

Comments

1

I have tested the following code and it works. It loops through the array with a for loop and when it finds a row with an href that's not empty it removes the element from array. You can see bellow that I have created the array $category_1['children'] :

<?php


$category_1['children'] = array(
  array('href' => 'j', 'name' => 'aa'),
  array('href' => '', 'name' => 'bb'),
  array('href' => 'j', 'name' => 'aa'),
  array('href' => 'l', 'name' => 'aaas'),
  array('href' => '', 'name' => 'aa'),
  array('href' => '', 'name' => 'cc')
);

Now time to sort it out :

$array = $category_1['children'];
for ($i=0; $i < count($category_1['children']); $i++) {
  if($category_1['children'][$i]['href'] != null) {
    unset($category_1['children'][$i]);
  }
}

Verify result :

print_r($category_1['children']);

Comments

0
<?php foreach ($category_1['children'] as $category_2) { 
        $str = $category_2['name'];
         preg_match('!\d+!', $str, $matches);
        if(empty($matches))){
        ?>
        <li><a href="<?php echo $category_2['href']; ?>"><?php echo $category_2['name']; ?></a>
    <?php } 
        }?>

preg match checks for numeric value in category name and if there is no numeric data, displays the list.

Comments

0

Here is the simple way:

<?php foreach ($category_1['children'] as $category_2) { ?>

<?php if(1 !== preg_match('~[0-9]~', $category_2['name'])){ ?>

  <li><a href="<?php echo $category_2['href']; ?>">

      <?php echo $category_2['name']; ?></a></li>

<?php } } ?>

Comments

0

You can simply use regex '/^[a-zA-Z ]*$/' and put a check on it: e.g.

<?php foreach ($category_1['children'] as $category_2) {

  if(preg_match('/^[a-zA-Z ]*$/', category_2['name'])) {?>

   <li><a href="<?php echo $category_2['href']; ?>"><?php echo $category_2['name']; ?></a></li>

  <?php }

} ?>

Comments

0

You are right, array_filter() is the solution.

$new_array = array_filder($category_1['children'], function ($element) {
    return !preg_match("~[0-9]~", $element['name'])
}

I didn't test it, but it should work.

Do this before the foreach() and check values with a die(var_dump($new_array));

// Edit The solution provided by @romanperekhrest is a better preg_match, anyway if you don't want to use regular expressions, an alternative would be

!strpbrk($element, '1234567890')

It's obviously intended inside the closure.

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.