mysql table into multidimensional array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • antonydoyle
    New Member
    • Jul 2012
    • 1

    mysql table into multidimensional array

    Hi

    Hope someone can help me with this!

    I'm trying to get a table of data into a multidimensiona l array.

    The table fields are as follows;

    Code:
    headline
    text
    startDate
    type
    media
    caption
    credit
    Only one row of data will have a value for field "type". There will eventually be dozens of other rows.

    I need to place the data into an array with the following structure;

    Code:
    $array = array(
    
    	"timeline" => array(
    		"headline" => "value",
    		"text" => "value",
    		"startDate" => "value",
    		"type" => "default",
    		"asset" => array(
    				"media" => "value,
    				"caption" => "value",
    				"credit" => "value"
    				),
    		"date" => array(
    		     "1" => array (
    			"headline" => "value",
    			"text" => "value",
    			"startDate" => "value",
    			"type" => "",
    			"asset" => array(
    					"media" => "value,
    					"caption" => "value",
    					"credit" => "value"
    					)
    				    ),
    		     "2" => array (
    			"headline" => "value",
    			"text" => "value",
    			"startDate" => "value",
    			"type" => "",
    			"asset" => array(
    					"media" => "value,
    					"caption" => "value",
    					"credit" => "value"
    					)
    				    )
    		);
    All rows aside from the one where type="default" will be within "date".

    I'm completely stuck with this. I've been trying to build the arrays from multiple variables, but it's just not working. I'll add that I'm still pretty new to PHP.

    Any assistance you can offer with this would be MUCH appreciated!

    Thank you.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    How many/much records do you have in your table, (if you look at your example) ?

    I think 6, but if that is not correct, i did not understand your problem.... ;)

    The first part ('headline' - 'asset'):
    This can be constructud using the MySQL query:
    SELECT headline, text, startdate, type, group_concat(as set)
    from table
    where type='default'
    group by headline, text, startdate, type


    After thant you have to go one level deeper, to add the 'date' fields. this is basically a repitition of the above ;)

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      You may want to look at my response (best answer) in this link http://bytes.com/topic/php/answers/9...-fill-template

      php does not use multidimensiona l arrays. They use arrays within arrays. The biggest difference here is in how to access the structure of the array.

      Traditional multi arrays are referenced like this array[1,1]

      where as php multi arrays are referenced like this
      array[1][1];

      But if you are looking to do a cross-tab report, you may want to download the code example I give in the answer.

      Comment

      Working...