-1

Ok so i have a while loop and I am trying to make an array out of it but my array is not coming out correctly.

My loop looks like this:

while ($rows = mysqli_fetch_assoc($headerresults)){
    $arr[$rows['COLUMN_NAME']] = array("Comments" => array($rows['COLUMN_COMMENT']));
 }

print_r($arr);

and my array looks like this:

[First_Name] => Array
        (
            [Comments] => Array
                (
                    [0] => 'Label' => 'First Name', 'Required' => 'Yes'
                )

        )...

What I want it to look like is this:

[First_Name] => Array
            (
                [Comments] => Array
                    (
                        [Label] => 'First Name'
                        [Required] => 'Yes'
                    )

            )...

Any advice would be helpful. Thank you. I know I have to add another block of code to my while loop but ive tried everything and can't get it to work. Thanks.

user982853
  • 2,470
  • 14
  • 55
  • 82
  • `COLUMN_COMMENT` contains a string like `"'Label' => 'First Name', 'Required' => 'Yes'"`…? – deceze May 09 '17 at 00:15
  • Yes, I realize that so my question is how to i get my code to treat or convert it to an array as if it was not a string but a array($key => $value) pair? Thank you. – user982853 May 09 '17 at 00:17
  • 1
    Why do you have this kind of string in the first place? It is *not* trivially unserialisable; depending on how exactly you're creating those strings and whether or not you've thought through all of the syntax edge cases it may not be unserialisable at all in edge cases. Why aren't you using a standardised format like JSON, or a normalised database? – deceze May 09 '17 at 00:19
  • BTW, variables do not work like copy and pasting source code. `array($foo)` creates an array with one value in its first index, regardless of what `$foo` contains. If `$foo` is `"'bar' => 'baz'"` that does not mean that you've just created the array `array('bar' => 'baz')`. – deceze May 09 '17 at 00:27
  • Thank you DECEZE... the JSON suggestion worked. Thank you. I answered your other question below. – user982853 May 09 '17 at 03:14

2 Answers2

0

$rows['COLUMN_COMMENT']) appears to be of type String, which is why array($rows['COLUMN_COMMENT'])) simply assigns it to index 0. There are a number of ways to convert it to an array, one of them has been discussed here:

Explode a string to associative array

Community
  • 1
  • 1
Pyromonk
  • 684
  • 1
  • 12
  • 27
0

Ok I figured it out. Thank you all for the comments. To answer above comment, I am storing some field attributes in the column comments section so that I can fetch attributes when loading a form dynamically. Below is my code for anyone trying to do the same thing. Likewise, if there are reasons experienced developers advise against this, please comment as I would like to know the drawbacks. I just didn't want to create a related table to store the field attributes if I could just throw them in the comments and fetch them on load. Again my page generates the forms dynamically by reading mysql rows and I wanted to add some attributes to the input fields so that i can do things like masking. So when the form loads it fetchs the field name and the attributes from the comments.

while ($rows = mysqli_fetch_assoc($headerresults)){
    $arr[$rows['COLUMN_NAME']] = array("Comments" => array($rows['COLUMN_COMMENT']));
    $j[$rows['COLUMN_NAME']] = (array) json_decode($arr[$rows['COLUMN_NAME']]['Comments'][0]);
} 

and that returns:

Array
(
    [First_Name] => Array
        (
            [Label] => First Name
            [Required] => Required
        )

    [Middle_Name] => Array
        (
            [Label] => MI
            [Required] => Required
        )

    [Last_Name] => Array
        (
            [Label] => Last Name
            [Required] => Required
        )    
)

Thank you for the comments and help. The JSON suggestion worked perfectly.

user982853
  • 2,470
  • 14
  • 55
  • 82