PHP多维数组,以表格形式显示数据

When I echo the array , result is :

$Items = Array( [147] => Array([ItemName] => Array([0]=>White Snack [1]=>Dark Cookie) [ItemCode] => Array([0]=>IT-WS [1]=>IT-DC ) )  [256] => Array([ItemName] => Array([0]=>Soft Sandwiches [1]=>Hard Drinks) [ItemCode] => Array([0]=>IT-SS [1]=>IT-HD )  )) 

Now I need to display the following result in a tabular form , the indexes 147 and 256 , I would pass them from a separate foreach loop so as to get the correct index value.

-----------147---------------

Name ----------------------------------- Code

White Snack --------------------------- WS

Dark Cookie --------------------------- DC

-----------256---------------

Name ----------------------------------- Code

Soft Sandwiches ---------------------- SS

Hard Drinks ---------------------------- HD

Am not able to achieve this. Please help.

    $ItemIndexes = array(147,256);

    foreach($ItemIndexes as $ItemMainIndex)

    {
         //further code here , 
        //here I would send the $ItemMainIndex to the $Items array 
       //and get the corresponding index value and display it accordingly.
    }

The structure of your array didn't go well with the structure of your display, so I made a new array with a different structure

$prods = array();
foreach ($Items as $key => $group){
    foreach ($group as $attr => $values){
        foreach ($values as $key2 => $value){
            $prods[$key][$key2][$attr] = $value;
        }
    }
}
var_dump($prods);

Output:

Array(
    [147] => Array(
            [0] => Array(
                    [ItemName] => White Snack
                    [ItemCode] => IT-WS
                )
            [1] => Array(
                    [ItemName] => Dark Cookie
                    [ItemCode] => IT-DC
                )
        )
    [256] => Array
            [0] => Array(
                    [ItemName] => Soft Sandwiches
                    [ItemCode] => IT-SS
                )
            [1] => Array(
                    [ItemName] => Hard Drinks
                    [ItemCode] => IT-HD
                )
        )
)

With that, I looped through to make a table:

$table = '<table>';
foreach ($prods as $key => $group){
    $table .= '<tr><td colspan="2">'.$key.'</td></tr>';
    $table .= '<tr><td>Name</td><td>Code</td></tr>';
    foreach ($group as $key2 => $values){
        $table .= '<tr>';
        foreach ($values as $attr => $value){
            if ($attr == 'ItemCode'){
                $parts = explode('-', $value);
                $table .= '<td>'.$parts[1].'</td>';
            } else {
                $table .= '<td>'.$value.'</td>';
            }
            $prods[$key][$key2][$attr] = $value;
        }
        $table .= '</tr>';
    }
}
$table .= '</table>';

With some styling, it can display more or less like you showed. In the array that you posted, there was a parenthesis out of place, so the structure of the original array that I used was:

$Items = array(147 => array('ItemName' => array(0=>'White Snack',
                                                        1=>'Dark Cookie'),
                                    'ItemCode' => array(0=>'IT-WS',
                                                        1=>'IT-DC')),
            256 => array('ItemName' => array(0=>'Soft Sandwiches',
                                                        1=>'Hard Drinks'),
                                    'ItemCode' => array(0=>'IT-SS',
                                                        1=>'IT-HD')));

You could create a function like this:

function echoItemTable($num, &$items){
    $val = $items[$num];
    echo "<h2>".$num."</h2>";
    echo "<table><thead><tr><th>Name</th><th>Code</th></tr></thead>";
    echo "<tbody>";
    foreach ($x = 0, $num = count($val["ItemName"); $x < $num; $x++){
        echo "<tr><td>".$val["ItemName"][$x]."</td>";
        echo "<td>".str_replace("IT-", "", $val["ItemCode"][$x])."</td></tr>";
    }
    echo "</tbody>";
    echo "</table>";
}

and call it in your code like

$ItemIndexes = array(147,256);

foreach($ItemIndexes as $ItemMainIndex)
{
    echoItemTable($ItemMainIndex, $items);
}

Here's an example of how you can iterate over the data structure.

$ItemIndexes = array(147,256);
foreach($ItemIndexes as $ItemMainIndex) {
  $Item = $Items[$ItemMainIndex];

  echo "<h1>-----------$ItemMainIndex---------------</h1>";
  echo "<h2>Name ----------------------------------- Code</h2>";

  $ItemNames = $Item['ItemName'];
  $ItemCodes = $Item['ItemCode'];

  for($i = 0; $i < count($ItemNames); $i++) {
    $Name = $ItemNames[$i];
    $Code = $ItemCodes[$i];   

    $Name = str_pad($Name . ' ', 40, '-');
    echo "$Name $Code<br/>";
  }  
}

This is not particularly good markup - we should probably be using a table of some sort - but it gives you something to start with.

Also, this is assuming that the 256 and 147 entries are at the same level in the $Items array, i.e. the array looks like this:

$Items = Array( 
  147 => Array(
    'ItemName' => Array(0=>'White Snack', 1=>'Dark Cookie' ),
    'ItemCode' => Array(0=>'IT-WS', 1=>'IT-DC' )
  ),
  256 => Array(
    'ItemName' => Array(0=>'Soft Sandwiches', 1=>'Hard Drinks'),
    'ItemCode' => Array(0=>'IT-SS', 1=>'IT-HD' )
  )
);

That's not the structure shown in your question though. If you really do need to support that exact structure, then the code would be a little more complicated.

Try this :

$Items       = array( 147 => array("ItemName" => array("White Snack", "Dark Cookie"), "ItemCode" => array("IT-WS", "IT-DC" )),  
                256 => array("ItemName" => array("Soft Sandwiches", "Hard Drinks"), "ItemCode" => array("IT-SS", "IT-HD" ))
              ); 

$result      = array();           
foreach($Items as $key => $Item){
    $result[$key]  = array_combine($Item["ItemName"], $Item["ItemCode"]); 
}

echo "<pre>";
print_r($result);

Output :

Array
(
    [147] => Array
        (
            [White Snack] => IT-WS
            [Dark Cookie] => IT-DC
        )

    [256] => Array
        (
            [Soft Sandwiches] => IT-SS
            [Hard Drinks] => IT-HD
        )

)