Friday 15 April 2011

mysql - PHP Add to Multidimensional Array using mysql_fetch -


I have seen on the web but I can find out how to echo the data from arrays, but I have to add them to them. This array is multidimensional, so I want to add an array to an array all the time. How do I do this?

This code is:

  & lt ;? Php $ data = array (array (array ('id' = & gt; "1", 'Katgi' => "Lifestyle", 'Title' = & gt; "Some Cool Titles ", 'Url' = & gt;" http://example.com ",)))); $ Sql ​​= mysql_query (Select "SELECT *" where range = '$ cat'); while ($ line = mysql_fetch_array ($ sql)) {$ id = $ row ["id"]; $ Cat = $ row ["Category"]; $ headline = $ line ["title"]; $ Url = $ line ["url"]; // Add to array // array (// 'id' = & gt; "$ id" , // 'catagory' = & gt; "$ cat", // 'title' = & gt; "$ title", // 'url' = & gt; "$ url", //),} mysql_close ( ); Echo json_encode ($ data);? & Gt;    

just do that. .. ($ Row = mysql_fetch_array ($ sql)) {$ data ['contact'] [] = $ line;}

or < This ...

  while ($ row = mysql_fetch_array ($ sql)) {array_push ($ data ['contact' A ', $ line);}   

Then a print_r will show you the array ...

  print_r ($ data);    

No comments:

Post a Comment