Asked  7 Months ago    Answers:  5   Viewed   54 times

Given the following array $mm

Array
(
    [147] => Array
        (
            [pts_m] => 
            [pts_mreg] => 1
            [pts_cg] => 1
        )    
    [158] => Array
        (
            [pts_m] => 
            [pts_mreg] => 
            [pts_cg] => 0
        )

    [159] => Array
        (
            [pts_m] => 
            [pts_mreg] => 1
            [pts_cg] => 1
        )

)

When I run count(array_filter($mm)) I get 3 as result since it is not recursive.

count(array_filter($mm), COUNT_RECURSIVE) also will not do because I actually need to run the array_filter recursively, and then count its result.

So my question is: how do I recursively run array_filter($mm) in this case? My expected result here would be 4.

Please note that I am not using any callback so I can exclude false, null and empty.

 Answers

60

Should work

$count = array_sum(array_map(function ($item) {
  return ((int) !is_null($item['pts_m'])
       + ((int) !is_null($item['pts_mreg'])
       + ((int) !is_null($item['pts_cg']);
}, $array);

or maybe

$count = array_sum(array_map(function ($item) {
  return array_sum(array_map('is_int', $item));
}, $array);

There are definitely many more possible solutions. If you want to use array_filter() (without callback) remember, that it treats 0 as false too and therefore it will remove any 0-value from the array.

If you are using PHP in a pre-5.3 version, I would use a foreach-loop

$count = 0;
foreach ($array as $item) {
  $count += ((int) !is_null($item['pts_m'])
          + ((int) !is_null($item['pts_mreg'])
          + ((int) !is_null($item['pts_cg']);
}

Update

Regarding the comment below:

Thx @kc I actually want the method to remove false, 0, empty etc

When this is really only, what you want, the solution is very simple too. But now I don't know, how to interpret

My expected result here would be 5.

Anyway, its short now :)

$result = array_map('array_filter', $array);
$count = array_map('count', $result);
$countSum = array_sum($count);

The resulting array looks like

Array
(
[147] => Array
    (
        [pts_mreg] => 1
        [pts_cg] => 1
    )    
[158] => Array
    (
    )

[159] => Array
    (
        [pts_mreg] => 1
        [pts_cg] => 1
    )

)
Wednesday, March 31, 2021
 
danjah
answered 7 Months ago
10

You can do this by unserializing the data (using unserialize()) and then iterating through it:

$fonts = array();

$contents = file_get_contents('http://phat-reaction.com/googlefonts.php?format=php');
$arr = unserialize($contents);

foreach($arr as $font)
{
    $fonts[$font['css-name']] = $font['font-name'];
}

Depending on what you're using this for, it may be a good idea to cache the results so you're not fetching external data each time the script runs.

Wednesday, March 31, 2021
 
Anand
answered 7 Months ago
93

It looks to me like you're looping the orders, but you're not looping the categories you want to be ordering them.

I've not run this code, but the pattern should do what you want I think.

// Group items into Categories - If you have a custom order you wish, pre-populate this array, or you can sort after
$categories = [];
foreach ($order->get_items() as $item) {
    $product_id = $item['product_id'];
    $terms = get_the_terms($product_id, 'product_cat');
    $cat_id = $terms[0]->term_id;
    $categories[$cat_id][] = $item;
}

// Loop Categories
foreach($categories as $category => $items){
    echo sprintf("<h1>%s</h1>", $category);
    //Loop Items in Category
    foreach ($items as $item) {
        print_r($item['name']);
    }
}
Saturday, May 29, 2021
 
PLPeeters
answered 5 Months ago
99

You have to use recursive function for this. hope this might help you

<?php
 $rs = unsetValues($arr);

 function unsetValues($a)
 {
    foreach($a as $k=>$v)
    {
       if(is_array($v))
       {
          $arr2[$k] = unsetValues($v);
       } else {
          if($v!="")
          $arr2[$k] = $v;
      }
  }
  return $arr2;
}
?>
Saturday, May 29, 2021
 
Valdas
answered 5 Months ago
20

if the chararray contains stringend or do not end with you can use these code, because strstr will brake on these ones:

#include <stdio.h>
int main()
{
    char c_to_search[5] = "asdf";

    char text[68] = "hello my name is  there is some other string behind it n asdf";

    int pos_search = 0;
    int pos_text = 0;
    int len_search = 4;
    int len_text = 67;
    for (pos_text = 0; pos_text < len_text - len_search;++pos_text)
    {
        if(text[pos_text] == c_to_search[pos_search])
        {
            ++pos_search;
            if(pos_search == len_search)
            {
                // match
                printf("match from %d to %dn",pos_text-len_search,pos_text);
                return;
            }
        }
        else
        {
           pos_text -=pos_search;
           pos_search = 0;
        }
    }
    // no match
    printf("no matchn");
   return 0;
}

http://ideone.com/2In3mr

Monday, September 13, 2021
 
Mike
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :