2

How can i convert mysql results (from mysql_fetch_array) into such a form?

$some = array(
     "comments" => array(
         array( "text" => "hi", "id" => "1" ),
         array( "text" => "hi", "id" => "2" ),
         array( "text" => "hi", "id" => "3" ),
         array( "text" => "hi", "id" => "4" )
    )
);

while the db looks like:

comments

id  text
1   blabla bla
2   bla bla

i've tried to fetch the values with foreach/while and insert it into two arrays but no success...

$some = array(
    "comments" => array()
);

$q = $mysql->sf('*', TBL_QST);
foreach($mysql->fetch($q) as $row) {
    $some[] = $row;
    // $some["comments"][] = $row;
}
1
  • Can you show your code that didn't work? Commented Jun 6, 2010 at 11:32

6 Answers 6

5

My print_r gives:

Array
(
    [comments] => Array
        (
            [0] => Array
                (
                    [text] => lorem ipsum
                    [id] => 0
                )
            [1] => Array
                (
                    [text] => lorem ipsum
                    [id] => 1
                )
            [2] => Array
                (
                    [text] => lorem ipsum
                    [id] => 2
                )
        )
)

And the php I did for it is:

$comments = array(
    'comments' => array()
    );

/* To simualate the loop */
for ($i = 0; $i < 3; $i++) { 
    array_push($comments['comments'], array(
        'text' => 'lorem ipsum',
        'id' => $i
        )
    );
}

I hope this will be to any help and that I didn't misunderstood your question.

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

Comments

2

Use mysql_fetch_assoc() instead, or use MYSQL_ASSOC as second argument of mysql_fetch_array().

Comments

2

Here's what I would do:

$dbc = mysql_connect(SERVER, USERNAME, PASSWORD);
mysql_select_db($dbc, DATABASE);

$query = "SELECT `id`, `text` FROM `comments`";
$result = mysql_query($dbc, $query);

$some = Array("comments" => Array());
while($row = mysql_fetch_array($result)) {
    array_push($some['comments'], Array($row['id'], $row['text']));
}

2 Comments

you forgot to close the bracket...and there is no "comments" row in the array..so it doesn't work...i have already tried with push and adding the "comments" into array...still doesn't work
Oh, oops. Misread where you wanted the data. Let me edit my question accordingly...
0
$some['comments'][] = ...

inside a loop

2 Comments

what is "doesn't work"? show actual results given with this line (var_dump($some))
if i commented this (line), that means it doesn't work...i already checked this with var_dump and as i said it didn't give me the results i wanted...however the problem is solved so thanks...
0

What you want is an associative array, and for this reason a special function has been already developed in PHP which is called mysql_fetch_assoc().

Updated
I am sorry, I understood you wrong. In this case you need this block of code below.

$sql="select id,text from comments";
$res=mysql_query($sql);
$arr=array();
while($row=mysql_fetch_assoc($res)){
     $arr[]=array('id'=>$row['id'],'text'=>$row['text']);
}
$some=array('comments'=>$arr);

2 Comments

$qst = array( "questions" => array() ); $q = $mysql->sf('*', TBL_QST); foreach(mysql_fetch_assoc($q) as $row) { $qst["questions"] = $row; // or $qst["questions"][] = $row; } doesn't work...
See the updated section of my answer. I tested that block of code and it works perfectly. Final result is the array $some.
0

Make an array with the same structure as the comments table:

$results = mysql_query("SELECT `id`, `text` FROM `comments`");
while ($row = mysql_fetch_array($results, MYSQL_NUM)) {$comments[intval($row[0])] = $row[1];}

The comments array is now structured:

comments[int `id`]=>`comment`

Then if you need comments in the "some" array:

$some['comments'] = $comments;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.