2017-08-24 73 views
0
$array = [ 
      [ 
      "UF" => "Goias", 
        [ 
        "Nome" => [ 
          "Avenor", 
          "KENNEDY", 
          "JOSE" 
          ], 
        "Fone" => [ 
          "+55 (91) 1111-1111", 
          "+55 (33) 2222-2222", 
          "+55 (93) 3333-3333" 
          ], 
        "Email" => [ 
          "[email protected]", 
          "[email protected]", 
          "[email protected]" 
          ] 
        ] 
      ], 
      [ 
      "UF" => "Amazonas", 
        [ 
        "Nome" => [ 
          "Kelsen", 
          "Joao", 
          "Kenia" 
          ], 
        "Fone" => [ 
          "+55 (91) 4444-4444", 
          "+55 (33) 5555-5555", 
          "+55 (93) 6666-6666" 
          ], 
        "Email" => [ 
          "[email protected]", 
          "[email protected]", 
          "[email protected]" 
          ] 
        ] 
      ] 
    ]; 

我想创建一个列表,通过搜索这个数组并填充列表itens。 例如:当用户选择“Goias”时,我必须在数组中搜索“Goias”,并返回一个包含“Nome”,“Fone”和“Email”的列表,但我找不到一种方法来循环这个数组并返回正确的值。如何从多维数组中搜索和检索值?

$selected = isset($_POST['selected']) ? $_POST['selected'] : 'nothing'; 
$key = array_search($selected,array_column($array, 'UF')); 

foreach ($array[$key] as $data){ 

     echo '<li><p>Nome: '.$data["Nome"].'</p><p>Fone: '.$data["Fone"].'</p><p>E-mail: '.$data["Email"].'</p></li>'; 
}; 

编辑:

我试着这样做:

echo 
     '<div class="representante-wrapper"> 
      <h3>'.$selected.'</h3>'; 
      foreach ($array[$key] as $data){ 
        foreach($data as $value){ 
        echo '<ul class="flex" id="dados" style="opacity: 1;">'; 
        var_dump($value); 
          foreach($value["Nome"] as $nome){ 
            echo '<li><p>Nome: '.$nome.'</p></li>'; 
          } 
          foreach($value["Fone"] as $fone){ 
            echo '<li><p>Fone: '.$fone.'</p></li>'; 
          } 
          foreach($value["Email"] as $email){ 
            echo '<li><p>E-mail: '.$email.'</p></li>'; 
          } 
        echo '</ul>'; 
        } 
      }; 
    echo '</div>'; 

但结果不是我所期望

Nome: Kelsen 

Nome: Joao 

Nome: Kenia 

Fone: +55 (91) 4444-4444 

Fone: +55 (33) 5555-5555 

Fone: +55 (93) 6666-6666 

E-mail: [email protected] 

E-mail: [email protected] 

E-mail: [email protected] 

我想这个如下,在每个阵列您的清单:

Nome: Kelsen 
Fone: +55 (91) 4444-4444 
E-mail: [email protected] 


Nome: Joao 
Fone: +55 (33) 5555-5555 
E-mail: [email protected] 

Nome: Kenia 
Fone: +55 (93) 6666-6666 
E-mail: [email protected] 

回答

0

你可以尝试通过键和值,像搜索:

foreach ($array as $key => $value) { 
    // $key is "Goias" 
    // $value is an array with "nome", "fone" and "Email" keys 
} 
0
$array = array("UF" => array("Goias"=> array("Nome" => array("Avenor","KENNEDY","JOSE"), 
       "Fone" => array("+55 (91) 1111-1111","+55 (33) 2222-2222","+55 (93) 3333-3333"), 
       "Email" => array("[email protected]","[email protected]","[email protected]") 
       ) 
     ), 
     array(
     "UF" => array("Amazonas"=> 
       array(
       "Nome" => array(
         "Kelsen", 
         "Joao", 
         "Kenia" 
         ), 
       "Fone" => array(
         "+55 (91) 4444-4444", 
         "+55 (33) 5555-5555", 
         "+55 (93) 6666-6666" 
         ), 
       "Email" => array(
         "[email protected]", 
         "[email protected]", 
         "[email protected]" 
         ) 
       ) 
     ) 
)); 
$selected = isset($_POST['selected']) ? $_POST['selected'] : 'nothing'; 
$i=0; 
    foreach($array as $key => $val){ 
    if(isset($array[$i]['UF'][$selected])){ 
     break; 
    } 
    $i++; 
    } 
foreach ($array[$i]['UF'] as $data){ 
$li = '<li>'; 
foreach($data["Nome"] as $nome){ 
$li .= '<p>Nome:'.$nome.'</p>'; 
} 
foreach($data["Fone"] as $nome){ 
$li .= '<p>Fone:'.$nome.'</p>'; 
} 
foreach($data["Email"] as $nome){ 
$li .= '<p>E-mail:'.$nome.'</p>'; 
} 
     $li .= '</li>'; 
echo $li; 
}; 
+0

它不工作,PHP的回报: “未定义指数:诺姆”; “提供给foreach()的参数无效” – kelsen

+0

检查上面编辑的代码 – Rajnish