Viewed   328 times

I'm trying to delete sub array of my multidimensional array, if any of the value is empty, than delete entire sub array. I want a universal function for the same! Dont want to type specific keys. And than ReIndex the newly formed array.

My array is like

Array
(
    [0] => Array
        (
            [name] => Test
            [mobile] => 613594551
            [email] => [email protected]
        )
    [1] => Array
        (
            [name] => Test1
            [mobile] => 613594552
            [email] => [email protected]
        )
    [2] => Array
        (
            [name] => Test2
            [mobile] => 613594553
            [email] => [email protected]
        )
    [3] => Array
        (
            [name] => Test3
            [mobile] => 613594554
            [email] => [email protected]
        )
)

So if my array is

Array
(
    [0] => Array
        (
            [name] => 
            [mobile] => 613594551
            [email] => [email protected]
        )
    [1] => Array
        (
            [name] => Test1
            [mobile] => 
            [email] => [email protected]
        )
    [2] => Array
        (
            [name] => Test2
            [mobile] => 613594553
            [email] => 
        )
    [3] => Array
        (
            [name] => Test3
            [mobile] => 613594554
            [email] => [email protected]
        )
)

Than display

Array
(
    [0] => Array
        (
            [name] => Test3
            [mobile] => 613594554
            [email] => [email protected]
        )
)

 Answers

2

Elaborating on Martin's answer, you can use array_filter() for both the source array and the nested array:

$filtered_array = array_filter($array, function($item){
    return count($item) == count(array_filter($item));
});
sort($filtered_array); // to reindex

Working example: https://eval.in/521449

Tuesday, September 20, 2022
 
4

You need to iterate over your results, adding a new entry to the output when you encounter a new team, or updating the points value when you find the same team again. This is most easily done by initially indexing the output by the team name, and then using array_values to re-index the array numerically:

$teams = array();
foreach ($results as $result) {
    $team = $result['team'];
    if (!isset($teams[$team])) {
        $teams[$team] = array('team' => $team, 'points' => $result['punti']);
    }
    else {
        $teams[$team]['points'] += $result['punti'];
    }
}
$teams = array_values($teams);
print_r($teams);

Output (for your sample data):

Array
(
    [0] => Array
        (
            [team] => Red Bull Racing
            [points] => 418
        )
    [1] => Array
        (
            [team] => Scuderia Ferrari
            [points] => 353
        )
    [2] => Array
        (
            [team] => Mercedes-AMG
            [points] => 516
        )
    [3] => Array
        (
            [team] => Racing Point F1
            [points] => 147
        )
    [4] => Array
        (
            [team] => Haas F1
            [points] => 127
        )
)

Demo on 3v4l.org

Friday, August 12, 2022
 
3
$output = array_reduce(
    $input,
    function (array $carry, array $item) {
        // generate the key to identify the duplicates
        // Add $item['gross'] if needed
        $key = $item['vendor_number'].'/'.$item['document_number'];

        // If this is the first appearance of the key
        // then add the value to the partial list and return it
        if (! isset($carry[$key])) {
            $carry[$key] = $item;
            return $carry;
        }

        // A previous revision exists    
        // Check values in $item against those already existing in the list
        $old = $carry[$key];
        if ($old['revision_no'] < $item['revision_no']) {
            // This is a new revision, replace the old one
            $carry[$key] = $item;
        }

        // Return $carry (updated or not)
        return $carry;
    },
    array()
);

This code does not preserve the keys from the original array. A solution that preserves the keys can be implemented in a similar fashion using array_walk().

Tuesday, December 20, 2022
4

No there is no VBA-function to get a row or column. You can only write it yourself, or take a look here:
http://www.cpearson.com/excel/vbaarrays.htm

Monday, November 28, 2022
3

You can do it like this, do the calculation from the innermost of the array. Check the demo.

<?php
function f(&$array)
{
    foreach($array as $k => &$v)
    {
        if(is_array($v))
        {
            if(count($v) == count($v, 1))
            {
                unset($array[$k]);
                if($k == 'sum')
                {
                    $v =  array_sum($v);
                    $array[] = $v;

                }elseif($k == 'multiply'){
                    $v = array_product($v);
                    $array[] = $v;
                }else{

                    foreach($v as $vv)
                        $array[] = $vv;
                }
            }else
                f($v);
        }
    }
}

while(count($array) != count($array, 1))
{
    f($array);
}

print_r($array);

Note:

traverse array from outer to inner
traverse array from inner to outer

Monday, November 14, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :