0

I currently have a function which creates an array from a html table the array output is

array:1 [
  0 => array:3 [
    0 => "Test"
    1 => "Test"
    2 => "Test"
  ]
]

Method for creating array from HTML

let convertedIntoArray = [];
        $("table#campaign_csv_table tr").each(function () {
            let rowDataArray = [];
            let actualData = $(this).find('textarea');
            if (actualData.length > 0) {
                actualData.each(function () {
                    rowDataArray.push($(this).text());
                });
                convertedIntoArray.push(rowDataArray);
            }
        });

How would I modify this array so it looks like this:

array:1 [
  0 => array:3 [
    "first_name" => "Test"
    "last_name" => "Test"
    "email" => "Test"
  ]

HTML table

<table class="display">
    <thead>
    <tr class="header">
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
    </tr>
    </thead>
    <tbody>
 
            <td id="table-cells"
              ><textarea rows="1" cols="50" style="resize: none" required
                                                                                  
                                                                                  ></textarea>
            </td>
            <td id="table-cells"
              ><textarea rows="1" cols="50" style="resize: none" required
                                                                                  
                                                                                  ></textarea>
            </td>
       
       
                <td id="table-cells" class="text-danger"><textarea rows="1" cols="50" style="resize: none" required
                                                                                   class="text-danger w-100"> Invalid Email Address</textarea>
                </td>
        </tr>
    </tbody>
</table>

I've tried using array_values() within PHP but had no luck can anyone see where im going wrong?

3
  • It looks like you really want an Array of Objects rather than an Array of Arrays. So perhaps rowDataArray should be rowDataObject and rather than pushing $(this).text(), you'd set the object key and value instead. But hard to tell without your HTML and where first_name, etc are coming from. You should include your html as well so we can attempt to recreate this. Commented Jul 12, 2022 at 15:58
  • @user1599011 I've added the HTML of the table for you Commented Jul 12, 2022 at 16:01
  • I don't think we need the php; we don't have access to all your php variables. Please use the rendered html only. Commented Jul 12, 2022 at 16:07

1 Answer 1

0

You'll have to push an object instead of an array since you want string keys.

Since your input fields aren't named, I'm using my own array let name = ... to determine the key names based on the index number.

let convertedIntoArray = [];
$("table#campaign_csv_table tr").each(function () {
    let rowDataArray = {};
    let actualData = $(this).find('textarea');
    if (actualData.length > 0) {
        actualData.each(function (i) {
            let name = ['first_name', 'last_name', 'email'][i]
            rowDataArray[name] = $(this).text();
        });
        convertedIntoArray.push(rowDataArray);
    }
});

console.log(convertedIntoArray)
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.