dru's avatar
Level 3

flatting array not working

I have a string from the database that usually is something like this: 0,10,20,50,100,1000,30,55,90.

I do this:

$perfiles = collect(explode(',' , $Menu->perfil)); 
dump($perfiles);
$perfiles2 = $perfiles->flatten();
 dump($perfiles2);

the output is this:

Illuminate\Support\Collection {#332 ▼
 0 => "0"
1 => "10"
2 => "20"
3 => "50"
4 => "100"
5 => "1000"
6 => "30"
7 => "55"
8 => "90"
9 => "80"
10 => "85"
11 => "110"
12 => "88"
13 => ""
 ]}


Illuminate\Support\Collection {#419 ▼
  #items: array:14 [▼
    0 => "0"
    1 => "10"
    2 => "20"
    3 => "50"
    4 => "100"
    5 => "1000"
    6 => "30"
    7 => "55"
    8 => "90"
    9 => "80"
    10 => "85"
    11 => "110"
    12 => "88"
    13 => ""
      ]}

why isn't $perfiles2 not equal to [ 0,10, 20, 50, 100, and so on... ]

0 likes
15 replies
dru's avatar
Level 3

@nakov

the thing is that when I only use explode, this code doesn't work well.

if( in_array($rol->rol_nivel, $perfiles) ){
    $menus[ $Menu->posicion_id ][] = $Menu;
}

And I think is because it is a multidimensional array.

Nakov's avatar

@dru

and have you checked if the dd($rol->rol_nivel) is contained in the $perfiles array?

dru's avatar
Level 3

@nakov

when I dd($rol->rol_nivel) the output is 0 as in INT, which is right.

but there are some menues that have only:

#items: array:3 [▼
    0 => "100"
    1 => "1000"
    2 => ""
  ]

so in that it case it shouldn't be true, but after testing it a lot, all return true.

Could it be that the 2 is taken as a 0?

Nakov's avatar

Might be, try using the strict flag on the in_array function:

in_array($rol->rol_nivel, $perfiles, true) 
dru's avatar
Level 3

in that case it allways return a false

bugsysha's avatar

So $rol->rol_nivel is always returning integer and $perfiles is always a string which is exploded by comma?

dru's avatar
Level 3

I'm trying a new approach, as I understand, I could make a new array with arraymap.

this is my code:

$perfiles = collect(explode(',' , $Menu->perfil));
                    
                    $flattened = $perfiles->flatMap(function ($values) {
                        if($values !== '' ){
                            dump($values);
                            return $values;
                        } 
                    });
                   dd($flattened->all());

this is the output:

"0"
"10"
"20"
"50"
"100"
"1000"
"30"
"55"
"90"
"80"
"85"
"110"
"88"
[] //> dd($flattened->all()); why is this empty?? 

@bugsysha I even tried using INT with an if statement and it didn't work me well..

Nakov's avatar

@dru

then filter the array to exclude empty values:

$perfiles = array_filter(explode(',' , $Menu->perfil));

if( in_array($rol->rol_nivel, $perfiles) ){
    $menus[ $Menu->posicion_id ][] = $Menu;
}

and keep your in_array usage without the strict flag.

bugsysha's avatar
var_dump(array_filter(['', '0'])); // this will be empty array
dru's avatar
Level 3

Thanks for the suggestion, just tried it:

foreach ($menusRecorrido as $Menu){
                   
                    $perfiles = array_filter(explode(',' , $Menu->perfil));
                   
                   
                   dump(in_array($rol->rol_nivel, $perfiles)); // always false
                    if( in_array($rol->rol_nivel, $perfiles) ){
                        
                        $menus[ $Menu->posicion_id ][] = $Menu;
                    }
                    // */ 
                }
bugsysha's avatar
bugsysha
Best Answer
Level 61
$perfiles = explode(',' , $Menu->perfil);

if (in_array((string)$rol->rol_nivel, $perfiles, true)) {
    // logic here
}
1 like
dru's avatar
Level 3

YES! this finally worked!

thanks!

also, I was wondering. why flatMap wasn't working, am I using it incorrectly?

  $perfiles = collect(explode(',' , $Menu->perfil));


                    //dd($perfiles);
                    
                    $flattened = $perfiles->flatMap(function ($values) {
                        if($values !== '' ){
                            dump($values); // shows a value
                            return $values;
                        } 
                        
                    });
                    dd($flattened->all()); //is empty.
bugsysha's avatar

also, I was wondering. why flatMap wasn't working, am I using it incorrectly?

Cause you always have to return some value from map, flatMap methods.

Replace this

$flattened = $perfiles->flatMap(function ($values) {
                        if($values !== '' ){
                            dump($values); // shows a value
                            return $values;
                        } 
                        
                    });
                    dd($flattened->all());
$flattened = $perfiles->flatMap(function ($values) {
    if($values !== '' ){
        return $values;
    } 
    return $values; // but do something here so if and else are not the same
});
1 like

Please or to participate in this conversation.