0

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.
    }
2
  • If you look at the structure of that $items array, it seems that the 256 entry is not at the same level as the 147 entry. Is that intentional? Commented Jul 17, 2013 at 17:16
  • @JamesHolderness : no no sir , it was a typo , i have corrected it now Commented Jul 18, 2013 at 4:39

4 Answers 4

1

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')));
Sign up to request clarification or add additional context in comments.

9 Comments

sir, loop works fine , but when I use this to display 500 rows then until all the rows are displayed on the page, the browser scrolling in that Tab remains stuck. why this hanging problem occurs??
@sqlchild - I don't know. That's a lot of rows.
could you please give it a check in your browser to see if this problem occurs ?
@sqlchild - Dis you fix your data? As I pointed out in my answer, your data wasn't propely formed, so if you tried to process 500 lines worth of poorly nested data, that might cause your browser to hang. Compare your data with the $Items array that I posted.
sir , I have fixed my data , I have an another array with lots of items , and am printing that one not this one. while printing the scrolling hangs and when the whole table is displayed then the browser scrolling gets enabled ( unhangs ) . for checking you can add 200 items the $Items array and then execute the code
|
1

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);
}

Comments

1

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.

Comments

0

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
        )

)

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.