Mostrando entradas con la etiqueta Arreglos en php. Mostrar todas las entradas
Mostrando entradas con la etiqueta Arreglos en php. Mostrar todas las entradas

jueves, 15 de marzo de 2012

Buscar valor en un arreglo

<?php

$meals = array('Walnut Bun' => 1,

               'Cashew Nuts and White Mushrooms' => 4.95,

               'Dried Mulberries' => 3.00,

               'Eggplant with Chili Sauce' => 6.50,

               'Shrimp Puffs' => 0);



$dish = array_search(6.50, $meals);

if ($dish) {

    print "$dish costs \$6.50";

}

?>

Checar un elemento con particular Value

<?php

$meals = array('Walnut Bun' => 1,

               'Cashew Nuts and White Mushrooms' => 4.95,

               'Dried Mulberries' => 3.00,

               'Eggplant with Chili Sauce' => 6.50,

               'Shrimp Puffs' => 0);

$books = array("The Eater's Guide to Chinese Characters",

               'How to Cook and Eat in Chinese');



// This is true: key Dried Mulberries has value 3.00

if (in_array(3, $meals)) {

  print 'There is a $3 item.';

}

// This is true

if (in_array('How to Cook and Eat in Chinese', $books)) {

  print "We have How to Cook and Eat in Chinese";

}

// This is false: in_array( ) is case-sensitive

if (in_array("the eater's guide to chinese characters", $books)) {

  print "We have the Eater's Guide to Chinese Characters.";

}

?>

Checar un elemento con particular Key

<?php

$meals = array('Walnut Bun' => 1,

               'Cashew Nuts and White Mushrooms' => 4.95,

               'Dried Mulberries' => 3.00,

               'Eggplant with Chili Sauce' => 6.50,

               'Shrimp Puffs' => 0); // Shrimp Puffs are free!

$books = array("The Eater's Guide to Chinese Characters",

               'How to Cook and Eat in Chinese');



// This is true

if (array_key_exists('Shrimp Puffs',$meals)) {

    print "Yes, we have Shrimp Puffs";

}

// This is false

if (array_key_exists('Steak Sandwich',$meals)) {

    print "We have a Steak Sandwich";

}

// This is true

if (array_key_exists(1, $books)) {

    print "Element 1 is How to Cook in Eat in Chinese";

}

?>

Alternando colores en tablas

<?php
$row_color = array('red','green');
$color_index = 0;

$meal = array('breakfast' => 'Walnut Bun',

              'lunch' => 'Cashew Nuts and White Mushrooms',

              'snack' => 'Dried Mulberries',

              'dinner' => 'Eggplant with Chili Sauce');

print "<table>\n";

foreach ($meal as $key => $value) {

    print '<tr bgcolor="' . $row_color[$color_index] . '">';

    print "<td>$key</td><td>$value</td></tr>\n";

    // This switches $color_index between 0 and 1

    $color_index = 1 - $color_index;

}

print '</table>';

?>

Recorrido de arreglo con each()

Cada vez que no este seguro de los contenidos y coherencia de los arrays  esta obligado a utilizar each();

Es también una muy buena herramienta para estar seguro de que no esta accediendo  a arrays fuera de sus limites

<?php

$my_array = array (0 =>"elemento1", 3=>"elemento2", 4=>"elemento3", 10=>"elemento4");


while(list($key, $value)=each ($my_array))
 print("Key : $key , Value: $value<br>");

?>

Resultado:

Key : 0 , Value: elemento1
Key : 3 , Value: elemento2
Key : 4 , Value: elemento3
Key : 10 , Value: elemento4

viernes, 2 de marzo de 2012

Imprimir arreglo recursivamente

<?php
  function imprimirarreglo($a) {
    echo '<blockquote>';
    foreach ($a as $key => $value) {
      echo htmlspecialchars("$key: ");
      if (is_array($value)) {
        imprimirarreglo($value);
      } else {
        echo htmlspecialchars($value) . '<br />';
      }
    }
    echo '</blockquote>';
  }

  $arr = array(
    'Roman' =>
      array('one' => 'I', 'two' => 'II', 'three' =>
        'III', 'four' => 'IV'),
    'Arabic' =>
      array('one' => '1', 'two' => '2', 'three' =>
        '3', 'four' => '4')
  );

  imprimirarreglo($arr);
?>


Resultado:

Roman:
one: I
two: II
three: III
four: IV
Arabic: one: 1
two: 2
three: 3
four: 4