0

I have this array that contains a message for a user. This is contained in variable $arr which is an array.

$arr looks like this when using var_dump()

var_dump($arr)

What I want to do is to merge the tables in [0] and [1] and have a result like this.

aim result

Please mention the things that I missed or you want to know. Any idea/s would be really appreciated!

11
  • So you basically want to de-duplicate the text? Commented Nov 27, 2015 at 3:24
  • De-duplicate, meaning merging the content? I apologize for not getting what you mean. Commented Nov 27, 2015 at 3:26
  • 1
    Oh I see. It might be good to explain exactly what you want to accomplish in the body of your question. I had a hard time figuring out what you wanted to achieve. Commented Nov 27, 2015 at 3:27
  • @yowza can you post your php code how you are generating this HTML. It is quite hard to understand what you want to accomplish ..What I got is $message is a string that is having the table which will display the array data in a tabular format .bu want to see the array, can you var_dump the array outside the HTML table ? Commented Dec 2, 2015 at 10:09
  • @PrafullaKumarSahu I apologize for the late response, I have updated my question. Please tell me what I'm still missing. Commented Dec 3, 2015 at 1:42

3 Answers 3

4
+50

To concatenate some rows in the same table, you have just to extract them from their original tables and them to a table which is concatenated with the common text parts. For that, take this example :

I'll put my texts in an array :

$data = [
    'Dear User2,<br> <br> Sample text <table style="border-collapse:collapse; width:100%" border="solid"> <tr> <th>TH1</th> <th>TH2</th> <th>TH3</th> </tr> <tr> <td style="width:10%"><center>1</center></td> <td style="width:20%"><center>foo1</center></td> <td style="width:20%"><center>foo1</center></td> </tr> </table> <br> Thank you, <br> Admin',
    'Dear User2,<br> <br> Sample text <table style="border-collapse:collapse; width:100%" border="solid"> <tr> <th>TH1</th> <th>TH2</th> <th>TH3</th> </tr> <tr> <td style="width:10%"><center>2</center></td> <td style="width:20%"><center>foo2</center></td> <td style="width:20%"><center>foo2</center></td> </tr> </table> <br> Thank you, <br> Admin',
    'Dear User2,<br> <br> Sample text <table style="border-collapse:collapse; width:100%" border="solid"> <tr> <th>TH1</th> <th>TH2</th> <th>TH3</th> </tr> <tr> <td style="width:10%"><center>3</center></td> <td style="width:20%"><center>foo3</center></td> <td style="width:20%"><center>foo3</center></td> </tr> </table> <br> Thank you, <br> Admin'
];

Common parts are :

Dear User2,<br> <br> Sample text <table style="border-collapse:collapse; width:100%" border="solid"> <tr> <th>TH1</th> <th>TH2</th> <th>TH3</th> </tr>

and

</table> <br> Thank you, <br> Admin

So if the common parts are always the same and with fixed lengths, you can use some substr() to extract what you need like this, for example :

// get common parts
$common_part_1 = substr($data[0], 0, 151);
$common_part_2 = substr($data[0], -36);

$content = '';

// concatenate all content rows
for($i = 0; $i < count($data); $i++){
    $content .= get_content($data[$i]);
}

where get_content() is the function that will extract the row containing the targeted text :

function get_content($data){    
    return substr($data, 151 , strlen($data) - 36 - 151);
}

then

echo $common_part_1 . $content . $common_part_2;

Of course this manner is really very limited, I don't recommend you to use it but I tried to give an idea how you can do ...


The second manner, is to extract your text using regex with preg_match() function :

// extract the 1st content row  
$content = get_content($data[0]);

// extract the common parts
$common_parts = str_replace($content, '##content##', $data[0]);

// concatenate all content rows
for($i = 1; $i < count($data); $i++){
    $content .= get_content($data[$i]);
}

and

function get_content($data){
    $pattern = '/\<\/th\>\s\<\/tr\>(.*)\<\/table\>/';
    preg_match($pattern, $data, $matches);
    return $matches[1];
}

then

echo str_replace('##content##', $content, $common_parts);

which will give you for both manners something like this :

Hope that can help.

Sign up to request clarification or add additional context in comments.

1 Comment

very nice 2nd option :)
1

You can split the $message into multiple part.

$message = '
     Dear User {APPROVER_SL},<br>
     <br>
     Sample text
     <table style="border-collapse:collapse; width:100%" border="solid">
     <tr>
     <th>TH1</th>
     <th>TH2</th>
     <th>TH3</th>
     </tr>';

$max = 10 // Your number of lines
for($i =0; $i < $max ; $i++){
    $message .= '
        <tr>
           <td style="width:10%"><center>'.$i.'</center></td>
           <td style="width:20%"><center>foo'.$i.'</center></td>
           <td style="width:20%"><center>foo'.$i.'</center></td>
        </tr>';
}

$message .='
</table>
<br>
Thank you, <br>
{SYSTEM_NAME}';

Try to integrate CSS into the code too so that your code don't look so messy and easier to manage.

3 Comments

Thanks for pointing out the messy part, I'll integrate CSS into my code.
is the code working for you? Basically you should adjust the second part of the code to generate your data and input using a loop.
It works fine but I need to adjust the second part, just like what you said.
0

Please try this code this is the sample code but you try this logic in that it will definitely help you thanx.....

<!DOCTYPE html>
<html>
<head>
    <title>Array</title>
</head>
<body>

<?php
    $arr = array
  (
  array("Upendrasinh",100,100),
  array("Ankit dave",200,200),
  array("Krunal Lathiya",300,300),
  array("Arjun Kanani",400,400),
  );

  $arrCount = count($arr);


  // $arrsubcount = count($arr[0]);
  // echo $arrsubcount;
  // echo "<br>";

  $arraycell=count($arr[0][0]);
  $arraycell=$arraycell;

echo '<table border="5px" width="100%">';
        echo "<tr>";
            echo "<th>Name</th>";
            echo "<th>Score</th>";
            echo "<th>Score2</th>";
        echo "</tr>";
  for($arrayrow=0;$arrayrow<=$arrCount-1;$arrayrow++){
            echo "<tr>"."<td>".$arr[$arrayrow][0]. "</td>"; 
                for($arraycolumn=0;$arraycolumn<$arraycell;$arraycolumn++){
                    echo "<td>".$arr[$arrayrow][$arraycell]."</td>";
                    echo "<td>".$arr[$arrayrow][$arraycell+1]."</td>"."</tr>";
                }
    }
  echo '</table>';

?>

</body>
</html>

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.