0

So i have the following nested array:

  'МУЖСКАЯ ОДЕЖДА LIFE IS GOOD' => 
array(
  'Российские размеры' => 
  array(
    '42', '44', '46', '48', '50', '52', '54'
    ), 
    'РАЗМЕРЫ SML' => 
    array(
      'XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL'
      ), 
      'Рост (для джоггеров)' => 
      array(
    '-', '-', '182/194', '182/194', '182/194', '182/194', '182/194'
    ), 
    'Обхват груди' => 
    array(
    'измеряется горизонтально по выступающим точкам груди вокруг тела' => 
    array(
    'Трикотажные изделия (худи, джоггеры, футболки)' => 
    array(
      '90-93', '94-97', '98-101', '102-105', '106-110'
      ), 
      'Куртка, жилет утепленные, куртка трикотажная' => 
    array(
    '84-86', '86-91', '91-97', '97-102', '102-107', '107-114', '114-122'
    )
    )
    ), 
    'Обхват талии' => 
    array(
    'измеряется не затягивая вокруг талии' => 
    array(
      'Трикотажные изделия (худи, джоггеры, футболки)' => 
      array(
        '78-81','82-85','86-89','90-93','94-98'
        ), 
        'Куртка, жилет утепленные, куртка трикотажная' => 
        array(
        '66-71','71-76','76-81','81-86','86-91','91-99','99-107'
        )
      )
    ),
      'Обхват бедер' => 
    array(
    'измерительная лента проодит по самым выступающим местам ягодиц' => 
    array(
      'Трикотажные изделия (худи, джоггеры, футболки)' => 
      array(
        '96-100','101-103','104-106','107-109','110-114'
        ), 
        'Куртка, жилет утепленные, куртка трикотажная' => 
        array(
        '84-86','86-91','91-97','97-102','102-107','107-114','114-122'
        )
      )
    )
  ),
  'ЖЕНСКАЯ ОДЕЖДА LIFE IS GOOD' => array(
  'Российские размеры' => 
  array(
    '42', '44', '46', '48', '50', '52', '54'
    ), 
    'РАЗМЕРЫ SML' => 
    array(
      'XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL'
      ), 
      'Рост (для джоггеров)' => 
      array(
    '-', '-', '182/194', '182/194', '182/194', '182/194', '182/194'
    ), 
    'Обхват груди' => 
    array(
    'измеряется горизонтально по выступающим точкам груди вокруг тела' => 
    array(
    'Трикотажные изделия (худи, джоггеры, футболки)' => 
    array(
      '90-93', '94-97', '98-101', '102-105', '106-110'
      ), 
      'Куртка, жилет утепленные, куртка трикотажная' => 
    array(
    '84-86', '86-91', '91-97', '97-102', '102-107', '107-114', '114-122'
    )
    )
    ), 
    'Обхват талии' => 
    array(
    'измеряется не затягивая вокруг талии' => 
    array(
      'Трикотажные изделия (худи, джоггеры, футболки)' => 
      array(
        '78-81','82-85','86-89','90-93','94-98'
        ), 
        'Куртка, жилет утепленные, куртка трикотажная' => 
        array(
        '66-71','71-76','76-81','81-86','86-91','91-99','99-107'
        )
      )
    ),
      'Обхват бедер' => 
    array(
    'измерительная лента проодит по самым выступающим местам ягодиц' => 
    array(
      'Трикотажные изделия (худи, джоггеры, футболки)' => 
      array(
        '96-100','101-103','104-106','107-109','110-114'
        ), 
        'Куртка, жилет утепленные, куртка трикотажная' => 
        array(
        '84-86','86-91','91-97','97-102','102-107','107-114','114-122'
        )
      )
    )
  )));

I wanted to convert it into an html table that would look like this enter image description here

I have tried to recursively loop through:

<html>
<body>
<table border=1>
  function printNestedArray($a,$table_row = true) {
  if($table_row){
    foreach ($a as $key => $value) {
      echo '<tr><td>' . htmlspecialchars("$key: ") . '</td></tr>';
      if (is_array($value)) {
        printNestedArray($value,false);
      } else {
        echo '<tr><td>' . htmlspecialchars($value) . '</td></tr>';
      }
    }
  }else{
        foreach ($a as $key => $value) {
      echo '<td>' . htmlspecialchars("$key: ") . '</td>';
      if (is_array($value)) {
        printNestedArray($value);
      } else {
        echo '<td>' . htmlspecialchars($value) . '</td>';
      }
    }
  }
}
</table>
</body>
</html>

Can someone , somehow get the result done? maybe some ideas of how? I would be very grateful , this is one of the tasks i have to get done until tomorrow but i have an intuition that this is gonna last long

4
  • Where is the question? What do you need to do? Commented Oct 5, 2020 at 15:50
  • Convert your example input data (array) to JSON format, so it will be more compact and easier to read it here. Commented Oct 5, 2020 at 15:52
  • Трикотажные изделия (худи, джоггеры, футболки)' => array( '90-93', '94-97', '98-101', '102-105', '106-110'), this part is impossible to render. There is no information that we need to skip 1 cell, then print, then skip last cell in a table. Commented Oct 5, 2020 at 15:54
  • Your sample array produces errors (and is unreadable with the current formatting). Please ensure you provide code we can easily read and test. Commented Oct 5, 2020 at 16:14

1 Answer 1

1

Your array is irregular - no recursive option. Worse - second part is a copy of first, values should be different. My code output is formatted as in your picture. $array it is your array (I hope, with correct values)

function noodavai($arr){
    $space0 = $space1 = '<td></td><td></td>';
    $simple = array_merge(range(0,3),range(10,13));
    $i = 0;
    foreach($arr as $thkey=>$thval){
        echo"<table><tr><th colspan='10'>$thkey</th></tr>\r\n";
        foreach($thval as $key=>$value){
            $i++;
            if(in_array($i,$simple)){   echo"<tr><td>$key</td>$space0"; foreach($value as $val){    echo"<td>$val</td>";}   echo"</tr>\r\n";}
            else{
                echo "<tr><td rowspan='2'>$key</td>";
                foreach($value as $ke=>$val){   echo "<td rowspan='2' class='grey'>$ke</td>";
                    foreach($val as $k=>$v){    echo ($i%2 == 0) ? "<td>$k</td>$space1" : "<td class='grey'>$k</td>";
                        foreach($v as $x){  echo"<td>$x</td>";} if($i%2 == 0){  echo $space2; $i++;}    echo"</tr>\r\n";}}}}
    echo"</table>\r\n";
    $i++;
    $space1 = $space2 = '<td></td>';}}

noodavai($array);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.