Viewed   89 times

I have the following array:

Array
(
    [0] => Array
        (
            [0] => 87
            [1] => 58
            [2] => 85
            [3] => 86
        )

    [1] => Array
        (
            [0] => 58
            [1] => 84
        )

    [2] => Array
        (
            [0] => 58
        )

)

This array above is an example. The actual array is of variable size, but structured like this. Basically, I'd like to run array_intersect on each second level array and grab the value (number) that is common between them. In this case, it would be 58.

I'm not quite sure where to start on this. Any advice?

 Answers

1

This works for me:

function multi_intersect($arr) {
   $return = array();
   foreach ($arr as $a) {
       foreach ($arr as $b) {
           if ($a === $b) continue;
           $return = array_merge($return, array_intersect($a, $b));
       }
   }
   return array_unique($return);
}

Should get you:

Array
(
    [0] => 58
)

The above will work if you have a common number in at least two of the sub-arrays.

After your edit:

You can simply use call_user_func_array on array_intersect, if you want to find numbers that are contained in all sub-arrays:

$intersect = call_user_func_array('array_intersect', $arr);
Monday, October 17, 2022
 
4
$max = 0;
foreach($array as $obj)
{
    if($obj->dnum > $max)
    {
        $max = $obj->dnum;
    }
}

That function would work correctly if your highest number is not negative (negatives, empty arrays, and 0s will return the max as 0).

Because you are using an object, which can have custom properties/structures, I don't believe there are really any 'predefined' functions you can use to get it. Might as well just use a foreach loop.

You really can't get away from a foreach loop, as even internal functions use a foreach loop, it is just behind the scenes.

Another solution is

$numbers = array();
foreach($array as $obj)
{
    $numbers[] = $obj->dnum;
}
$max = max($numbers);
Saturday, October 8, 2022
2

In theory this should work:

function findChild(&$array){
     foreach($array as &$arr){
            if(isset($arr['children'])){
                  $arr['leaf'] = 0; //there are children
                  findChild($arr['children']);
            }
            else {
                  $arr['leaf'] = 1; //there are no children
            }
     }
}

Here is a working demo: http://codepad.org/AnYiRpES

Thursday, October 13, 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

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 :