2016-07-13 7 views
-3
$arr_cont = array('type1' =>"Fruits",'f_div'=> 
        array(

         1 => "Apple", 
         2 => "Banana", 
         3 => "Mango", 
         4 => "Grapes", 
         ) 
         , 
      'type2' => "colors",'c_div' => 
        array (
          1 => "Red", 
          2 => "Green", 
         ) 
         , 
      'type3' => "Shapes",'s_div' => 
        array(
          1 => "Square", 
          2 => "Round", 
          ) 
         , 
      'type4' => "Flowers",'l_div' => 
        array(
          1 => "Rose", 
          2 => "Lily", 
         ) 

      ); 

i oben Array erwähnt zu drucken und i ausgegeben werden soll, wie unten, geben Sie mir bitte compelete Codierung der foreach-Schleife für dieseMultidimentional PHP-Array

heraus setzen:

type1 : Fruits : f_div 
1. Apple 
2. Banana 
3. Mango 
4. Grapes 
type2 : colors :c_div 
1. Red 
2. Green 
type3 : Shapes : s_div 
1. Square 
2. Round 
type4 : Flowers : l_div 
1. Rose 
2. Lily 

Dank im vorgerückten ... würde jede Antwort appricited sein ...

ich habe den Code, der unten erwähnt wird, aber seinen gebenden etwas Fehler versucht:

foreach($arr_cont as $val => $cont){ 
    print $val ." : " ; 
    foreach($cont as $val1 => $id){ 
     print $id ." : ".$val1; 
} 

Fehlermeldung

Warning: Invalid argument supplied for foreach() in 

aber sein Druck $ arr_cont Variable zusammen mit Fehlern Fehler in zweiten foreach Schleife

+3

Zeigen Sie uns, was Sie bisher angefangen haben? – claudios

+1

Wie wäre es mit nein? Wir sind kein kostenloser Codierdienst. – Epodax

+0

Zwei foreach .... überprüfen Sie Ihren Wert ist Array wenn true machen Sie eine andere Schleife sonst echo val –

Antwort

0
<?php 

$arr_cont = array(
    'type1' => "Fruits", 
    'f_div' => 
    array(
     1 => "Apple", 
     2 => "Banana", 
     3 => "Mango", 
     4 => "Grapes", 
    ) 
    , 
    'type2' => "colors", 
    'c_div' => 
    array(
     1 => "Red", 
     2 => "Green", 
    ) 
    , 
    'type3' => "Shapes", 
    's_div' => 
    array(
     1 => "Square", 
     2 => "Round", 
    ) 
    , 
    'type4' => "Flowers", 
    'l_div' => 
    array(
     1 => "Rose", 
     2 => "Lily", 
    ) 
); 
$output = ""; 
foreach ($arr_cont as $val => $cont) { 
    if (is_array($cont)) { 
     $output .= "$val<br />"; 
     foreach ($cont as $contIndex => $contValue) { 
      $output .= " $contIndex. $contValue<br />"; 
     } 
    } else { 
     $output .= "$val : $cont : "; 
    } 
} 
echo $output; 

output:

type1 : Fruits : f_div 
1. Apple 
2. Banana 
3. Mango 
4. Grapes 
type2 : colors : c_div 
1. Red 
2. Green 
type3 : Shapes : s_div 
1. Square 
2. Round 
type4 : Flowers : l_div 
1. Rose 
2. Lily 
+0

vielen Dank, es funktioniert –

0

$ output = null;

foreach ($ arr_cont wie $ val => $ cont) {

if (is_array($cont)) { 

    $output .= $val.'<br/>'; 
    foreach ($cont as $contIndex => $contValue) { 
     $output .= $contIndex.' '. $contValue.'<br/>'; 
    } 
} else { 
    $output .= $val .':'. $cont.':'; 
} 

} echo $ output;

+0

Thnaks viel. es funktioniert –