Foros del Web » Programando para Internet » PHP »

filtrar array de manera recursiva

Estas en el tema de filtrar array de manera recursiva en el foro de PHP en Foros del Web. Hola tengo un array con cierta estructura donde quiero filtrar solo los que contienen 'survey78399' , pero en algunos casos no logro hacerla funcionar. la ...
  #1 (permalink)  
Antiguo 07/07/2016, 14:34
 
Fecha de Ingreso: enero-2011
Ubicación: /root
Mensajes: 530
Antigüedad: 13 años, 3 meses
Puntos: 61
filtrar array de manera recursiva

Hola tengo un array con cierta estructura donde quiero filtrar solo los que contienen 'survey78399' , pero en algunos casos no logro hacerla funcionar.

la estructura del array:

Código PHP:
Array
(
    [
0] => Array
        (
            [
email] => john.doe@sendgrid.com
            
[timestamp] => 1337966815
            
[category] => survey78399
            
[event] => open
        
)

    [
2] => Array
        (
            [
email] => juanito@email.com
            
[timestamp] => 1337966855
            
[category] => Array
                (
                    [
0] => survey78399
                    
[1] => transactional
                
)

            [
event] => spam
        
)

    [
3] => Array
        (
            [
email] => jajjaa@test.com
            
[timestamp] => 1337966899
            
[category] => Array
                (
                    [
0] => survey78399
                    
[1] => transactional
                
)

            [
event] => open
        
)

    [
4] => Array
        (
            [
sg_event_id] => sendgrid_internal_event_id
            
[sg_message_id] => sendgrid_internal_message_id
            
[ip] => 255.255.255.255
            
[useragent] => Mozilla/5.0 (iPhoneCPU iPhone OS 7_1_2 like Mac OS XAppleWebKit/537.51.2 (KHTMLlike GeckoVersion/7.0 Mobile/11D257 Safari/9537.53
            
[event] => click
            
[email] => email@example.com
            
[timestamp] => 1249948800
            
[url] => http://yourdomain.com/blog/news.html
            
[url_offset] => Array
                (
                    [
index] => 0
                    
[type] => html
                
)

            [
unique_arg_key] => unique_arg_value
            
[category] => Array
                (
                    [
0] => category1
                    
[1] => category2
                
)

            [
newsletter] => Array
                (
                    [
newsletter_user_list_id] => 10557865
                    
[newsletter_id] => 1943530
                    
[newsletter_send_id] => 2308608
                
)

            [
asm_group_id] => 1
        
)



mi codigo :

Código PHP:

function debug($valor)
{
    echo 
"<pre>";
    
print_r($valor);
    echo 
"</pre>";
    exit;
}

$search 'survey78399';
         
        
$callback = function($item) use (&$callback, &$search) {
            if (
is_array($item)) {
                return 
array_filter($item$callback);
            }else {
                return 
$item == $search;
            }
        };

        
$filtered array_filter($arr$callback);


        
debug($filtered); 
Saludos
  #2 (permalink)  
Antiguo 07/07/2016, 16:47
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: filtrar array de manera recursiva

Solo cambia la condición a === entre $item y $search
Código PHP:
Ver original
  1. $array = [
  2.     0 =>
  3.         [
  4.             "email" => "[email protected]",
  5.             "timestamp" => "1337966810",
  6.             "category" =>
  7.                 [
  8.                     0 => "survey78399_",
  9.                     1 => "transactional",
  10.                 ],
  11.  
  12.             "event" => "delivered",
  13.         ],
  14.  
  15.     1 =>
  16.         [
  17.             "email" => "[email protected]",
  18.             "timestamp" => "1337966855",
  19.             "category" =>
  20.                 [
  21.                     0 => "survey78399",
  22.                     1 => "transactional",
  23.                 ],
  24.  
  25.             "event" => "spam",
  26.         ],
  27.  
  28.     2 =>
  29.         [
  30.             "email"=> "[email protected]",
  31.             "timestamp" => "1337966899",
  32.             "category" =>
  33.                 [
  34.                     0 => "survey8888",
  35.                     1 => "transactional",
  36.                 ],
  37.  
  38.             "event" => "open",
  39.         ],
  40.  
  41.     3 =>
  42.         [
  43.             "email" => "[email protected]",
  44.             "timestamp" => "1337966899",
  45.             "category" => "survey78399",
  46.             "event" => "open",
  47.         ],
  48.     4 =>
  49.         [
  50.             "sg_event_id" => "sendgrid_internal_event_id",
  51.             "sg_message_id" => "sendgrid_internal_message_id",
  52.             "ip" => "255.255.255.255",
  53.             "useragent" => "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53",
  54.             "event" => "click",
  55.             "email" => "[email protected]",
  56.             "timestamp" => 1249948800,
  57.             "url" => "http://yourdomain.com/blog/news.html",
  58.             "url_offset" =>
  59.                 [
  60.                     "index" => 0,
  61.                     "type" => "html",
  62.                 ],
  63.  
  64.             "unique_arg_key" => "unique_arg_value",
  65.             "category" =>
  66.                 [
  67.                     0 => "category1",
  68.                     1 => "category2"
  69.                 ],
  70.  
  71.             "newsletter" =>
  72.                 [
  73.                     "newsletter_user_list_id" => 10557865,
  74.                     "newsletter_id" => 1943530,
  75.                     "newsletter_send_id" => 2308608,
  76.                 ],
  77.  
  78.             "asm_group_id" => 1,  
  79.         ]
  80. ];
  81.  
  82. $search = 'survey78399';
  83.  
  84. $callback = function($item) use (&$callback, &$search) {
  85.     if (is_array($item)) {
  86.         return array_filter($item, $callback);
  87.     }else {
  88.         return $item === $search;
  89.     }
  90. };
  91.  
  92. $filtered = array_filter($array, $callback);
  93.  
  94. print_r($filtered); // imprime solo key 1 y 3 del arreglo original
  95. /*
  96. Array
  97. (
  98.     [1] => Array
  99.         (
  100.             [email] => [email protected]
  101.             [timestamp] => 1337966855
  102.             [category] => Array
  103.                 (
  104.                     [0] => survey78399
  105.                     [1] => transactional
  106.                 )
  107.  
  108.             [event] => spam
  109.         )
  110.  
  111.     [3] => Array
  112.         (
  113.             [email] => [email protected]
  114.             [timestamp] => 1337966899
  115.             [category] => survey78399
  116.             [event] => open
  117.         )
  118.  
  119. )
  120.  
  121. */
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.

Etiquetas: filtrar, html, manera, recursiva
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:56.