Like traversing an array inside another array

0

I try to get the values of the "brother" array, the key is dinamic and try with for but it does not recognize me

for($l=0;$l<count($pru);$l++) {
              for ($h=0; $h <count($pru[$l]) ; $h++) { 
                dd($pru[$l][$h]);
              }
}

try with foreach and only show me the first value

$pru=array($request->hermano);

for($l=0;$l<count($pru);$l++) {
      foreach ($pru[$l] as  $values) {
       dd($values);
      }

}

    
asked by Lioni_Lee 16.01.2018 в 09:41
source

1 answer

0

Given the information you provide the way to go through the values would be through a loop foreach :

/* $datos = $request->all(); */
$datos = [
  '_token' => 'bGJVDLHkRw8jMdsoBvC4ouyBEvgyBs5VLHD8LlDp',
  'oculto' => 'Leonela Burgos ',
  'id_alumno' => '',
  'alumno' => '',
  'cedula_alumno_pres' => '',
  'dir_alumno_pres' => '',
  'telf_alumno_pres' => '',
  'fch_nac_alumno_pres' => '',
  'sex_alum_pres' => '',
  'op_desc_alum' => 'dsc_herm',
  'dato_buscar' => '0101030435',
  'selector' => '2',
  'hermano' => [
    39 => '39',
    40 => '40',
  ],
  'alumno_fin' => '',
  'cedula_alumno' => '',
  'edc_alumno' => '',
];
foreach ($datos['hermano'] as $clave => $valor) {
  echo '<p>', htmlspecialchars($clave . ' => ' . $valor), '</p>', PHP_EOL;
}

You can not traverse values using count() because this function calculates the number of elements in a matrix. Your loop would start with 0 and end with 1, preventing you from reaching the keys where the data is stored (39 and 40).

On the other hand, foreach iterates for each element and allows extracting the index and content of each element.

    
answered by 16.01.2018 / 11:03
source