How to compare elements of two arrays?

0

Good, my problem is that I could not compare an element between two arrays. What I'm looking for is to compare if the element in array2 is in array1 and print that element and if it is not printed it has no meaning.

It is a question that if the person's favorite color is the meaning in the color array, print the meaning otherwise print that no meaning was found.

I have tried several ways but it does not print anything to me .. I would be grateful for a quick response

This is the code:

<html>
<head>
<title>Unidad 2 -Ejercicio 4</title>
<meta http-equiv="Content-Type" 
      content="text/html; charset=ISO-8859-1" />

</head>
<body>   
<?php  

//se crea un array multidimensional
$personas=array('Juan gabriel'=>array(
  'direccion'=>'Calle 7# 8-25',
  'telefono'=>'31789456',
  'Fecha de cumpleaños'=>'14/09/1992',
  'color favorito'=>'verde'
),

                'Andrea martinez'=>array(
                  'direccion'=>'Calle 10# 9-75',
                  'telefono'=>'31789456',
                  'Fecha de cumpleaños'=>'28/06/1994',
                  'color favorito'=>'blanco'
                ),

                'Nancy peña'=>array('direccion'=>'Av 5 #23-58 Sur',
                                    'telefono'=>'31789456',
                                    'Fecha de cumpleaños'=>'2/02/1991',
                                    'color favorito'=>'azul'),

               );

$colores=array('blanco'=>'pureza',
              'verde'=>'esperanza',)


?>


<br>    <br>
<br>

    <!--    se crea una tabla donde mostraran los datos-->
<table widtch="60%" border="1" align="center">
  <tr>  
    <th bgcolor="#449742">Nombres</th>
    <th bgcolor="#449742">Direccion</th>
    <th bgcolor="#449742">Telefono</th>
    <th bgcolor="#449742">Fecha de cumpleaños</th>
    <th bgcolor="#449742">Color favorito</th>
    <th bgcolor="#449742">Significado</th>
  </tr>
  <?php 
  //se recorre el array 
  foreach($personas as $nombre=>$informacion){
    echo "<tr>";
    echo '<td>' . $nombre . '</td>';
    foreach($informacion as $info){
      echo '<td>' . $info . '</td>';

    }
      foreach($personas as $signicado){
        if (in_array($signicado, $colores)){
          echo '<td>' . $colores .'</td>';
        }
      }

    //          foreach ($personas as $cuales)
   //          {
  //            $igual=array_search($cuales, $colores);
 //            if($igual)
//            { 
//          echo '<td>' . $colores[$igual]. '</td>';
//            }
//          }

//          foreach($colores as $significado){
//            $significado=array_intersect_assoc($personas, $colores)
//              echo '<td>' . $significado . '</td>';
//            }
    echo "</tr>";
    }
  ?>
</table>
</body>
</html>
    
asked by Juanzu 24.11.2016 в 17:52
source

1 answer

1

The error is in the data that you are passing to in_array . You are passing it as aguja an associative matrix ( $significado ), when those supported for aguja are arreglo (plano) o una cadena .

Try to do it like this:

<table widtch="60%" border="1" align="center">
  <tr>  
    <th bgcolor="#449742">Nombres</th>
    <th bgcolor="#449742">Direccion</th>
    <th bgcolor="#449742">Telefono</th>
    <th bgcolor="#449742">Fecha de cumpleaños</th>
    <th bgcolor="#449742">Color favorito</th>
    <th bgcolor="#449742">Significado</th>
  </tr>
  <?php 
  //se recorre el array 
  foreach($personas as $nombre=>$informacion){
    echo "<tr>";
    echo '<td>' . $nombre . '</td>';
    foreach($informacion as $info){
      echo '<td>' . $info . '</td>';

    }
    // Si el color favorito esta en el arreglo $colores
    $significado = '&nbsp;'; //Entidad HTML del espacio ( )
    if(isset($colores[$informacion['color favorito']])) {
        $significado = $colores[$informacion['color favorito']];
    }
    echo '<td>' . $significado .'</td>';
    echo "</tr>";
  }
  ?>
</table>
    
answered by 24.11.2016 / 18:20
source