1

I have an array with following format:

Array
(
    [0] => Array
        (
            [Push to Web] => Yes
            [attribute_set] => laminate_flooring
            [category] => Accessories/Underlay
            [name] => Under Pad Feather Light Foam
            [sku] => 123-1028
            [description] => Floor Underlayment Feather Light (Vapour Barrier) 200 Sqft/Roll
            [short_description] => Floor Underlayment Feather Light (Vapour Barrier) 200 Sqft/Roll
            [image] => 123-1028.jpg
            [gallery_Image] => 9095307460638-424-424.jpg
            [price] => 0.24
            [qty_ca] => 16
            [weight] => 3
            [meta_description] => 51-1001
            [meta_title] => Under Pad Feather Light Foam
            [status] => 1
            [flooring_coverage] => 200
            [is_flooring_product] => 1
            [web_price_comparative] => 0.31
        )

    [1] => Array
        (
            [Push to Web] => Yes
            [category] => Accessories
            [name] => Vent Maple Flush 4x10
            [sku] => 089-1000
            [description] => Vent Flush Mount Maple 4 x 10
            [short_description] => Vent Flush Mount Maple 4 x 10
            [image] => 089-1000.jpg
            [price] => 17.05
            [qty_ca] => 63
            [qty_us] => 41
            [meta_description] => 10-1023
            [meta_title] => Vent Maple Flush 4x10
            [status] => 1
            [flooring_coverage] => 1
            [min_order_qty] => 400
            [web_price_comparative] => 22.16
        )
)

I have to print the data in the table so that each key of array is printed as column header and the value as column data. That means Push to Web, attribute_set etc will be header and Yes, laminate_flooring will be data respectively.

I wrote the following but its not working.

$table  = '<table border="1" id="datatable">
<tr>';
foreach($data as $key=>$value){
    $table .= '<td>'.$key.'</td>';
}
$table .= '</tr>';
foreach($data as $value){
    $table .=   '<tr>';
    foreach($value as $innerkey=>$innervalue){
                $table .=   '<td>'.$innervalue.'</td>';
             }
             $table .= '</tr>';
        }
    }
 }
 $table .= '</table>';

 print_r($table);

Please help me to sort out the problem. Thanks in advance

4
  • 2
    I wrote the following but its not working - can you show an example of the result which shows where the problem is. Commented May 29, 2020 at 19:31
  • May be array_keys() and array_values() will help. php.net/manual/en/function.array-keys & php.net/manual/en/function.array-values.php Also use the alternate notation of foreach() when using inside HTML. It will look much cleaner. <?php foreach($array as $element): #your code endforeach; ?> Commented May 29, 2020 at 19:38
  • I am getting 0, 1,2,3 ........ the keys as the header rows. Commented May 29, 2020 at 21:32
  • Also arrays don't have keys or values are filled by next value. so no blank cell is here. But there should be some blank cells. Commented May 29, 2020 at 21:34

1 Answer 1

1

You say you get 0,1,2 in the header keys.. etc....

foreach($data as $key=>$value){
    $table .= '<td>'.$key.'</td>';
}

This is because your top level keys are:

0
1
2
3..

In the next level you have these keys:

Push to Web
Category
Name
etc..

In this code snippet you add the innervalue-data (push to web-value, category-value, name-value etc...)...

foreach($data as $value){
    $table .=   '<tr>';
    foreach($value as $innerkey=>$innervalue){
        $table .= '<td>'.$innervalue.'</td>';
    }
    $table .= '</tr>';
}

...these values are on the same level as the key you want. Therefore $innerkey is the key you want to add in the header:

foreach($data as $value){
    $table .= '<tr>';
    foreach($value as $innerkey=>$innervalue){
        $table .= '<td>'.$innerkey.'</td>';
    }
    $table .= '</tr>';
}

So the logic is basically something like this:

$table  = '<table border="1" id="datatable">';
foreach($data as $key=>$value){
    $table .= '<tr>';
    foreach($value as $innerkey=>$innervalue){
        $table .= '<td>'.$innerkey.'</td>';
    }
    $table .= '</tr>';
}
foreach($data as $value){
    $table .= '<tr>';
    foreach($value as $innerkey=>$innervalue){
        $table .= '<td>'.$innervalue.'</td>';
    }
    $table .= '</tr>';
}
$table .= '</table>';

Based on above logic - you can do like this to achieve what you want:

UPDATE

Now that I understand that all keys does not exists in all items. You could do like this:

//Because you have different keys in different item
//you could recreate each item in the array
//Based on the the $use_keys array here 
//(add all keys you want to use and $use_keys set the order of the keys in 
//each item!)
$use_keys = ['pushweb', 'attribute set', 'category', 'name'];

//Go through the whole array and recreate it with keys given
//in $use_keys
foreach($data as $key=>$value){ 

    //Create a new array with key values from array $use_keys
    $new_arr = [];
    foreach($use_keys as $apk_item) {
        //Add key/value to the new array. If value already exist
        //in the $data[key] then use that value, else just empty string
        $new_value = '';
        if (isset($data[$key][$apk_item])) {
            $new_value = $data[$key][$apk_item];
        }            
        $new_arr[$apk_item] = $new_value;                
    }
    //Recreate the data[key] part of the original data array
    $data[$key] = $new_arr;
}

//Then same code as before
$row = 0;
$table  = '<table border="1" id="datatable">';
foreach($data as $value){
    $table .= '<tr>';
    if ($row == 0) {
        foreach($value as $innerkey=>$innervalue){
            $table .= '<td>'.$innerkey.'</td>';
        }    
    }
    $table .= '</tr><tr>';
    foreach($value as $innerkey=>$innervalue){
        $table .= '<td>'.$innervalue.'</td>';
    }
    $table .= '</tr>';
    $row++;
}
$table .= '</table>';
Sign up to request clarification or add additional context in comments.

8 Comments

Hi, Thanks for the reply. header row is showing but not all columns. only columns having available in the first array. Secondly arrays don't have keys or values are filled by next value. so no blank cell is here. But there should be some blank cells.
I dont understand. Can you please update your question and tell what output you want from your example array?
I actually need the table like this ibb.co/zn9zNgR. But getting lik this ibb.co/LSy2sYb and this ibb.co/Tbg92jb
It seems weird you have a different set of keys for each item in the array? (Attribute-set is set in the first item but not in the second in your example).
Awesome. Love from me fro your lifesaver code. Thanks :)
|

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.