Creating a table from a multidimensional array in PHP -


I am receiving an error from an API that has a variable number of fields and I try to get it in the table I am doing

A sample object looks like this:

  array 'silver' => Array 'property' = & gt; Array 'paragraph' = & gt; Float2 'ROS_Medium_1' = & gt; Float 37704 'ROS_Medium_2' = & gt; Float 37711 'ROS_Medium_3' = & gt; Float 37546 'ROS_Leaderboard_Footer' => Float 37 9 41 'ROS_Leaderboard_Infinite Scroll' = & gt; Float 3636 'price' = & gt; Float 375 'discount' = & gt; Int 0 'banner_cpm' = & gt; Float 12.7 9 'banner_sov' = & gt; Float 0.0099 'content_cpm' = & gt; Float 150 'content_sov' => float 0.0028 'fixed_cpm' = & gt; Float 0 'blended CPM' => Float 26.26 'gold' => Array 'property' = & gt; Array 'paragraph' = & gt; Float2 'ros_layerboard' = & gt; Float 15212 'ros_ccf' = & gt; Float 1 'ROS_Halfpage' = & gt; Float 15212 'ROS_Medium_1' = & gt; Float 12459 'ROS_Medium_2' = & gt; Float 12476 'ROS_Medium_3' = & gt; Float 12476 'ROS_Leaderboard_Footer' = & gt; Float 12390 'ROS_Leaderboard_Infinite Scroll' = & gt; Float 1239 'price' = & gt; Int 500 'discount' = & gt; Int 0 'banner_cpm' = & gt; Float 18.84 'banner_sov' = & gt; Float 0.008 'content_cpm' = & gt; Float 150 'content_sov' => Float 0.0028 'fixed_cpm' = & gt; Float 0 'blended CPM' => Boat 29.8   

Number of columns, top level arrays (so, silver and gold here)

The number of high-level arrays may be different, the number is equal to And the property may be different from the sub-array array (note how the property has more properties in the "gold" array in the sample code). For each asset, one line and one other key must be in the array.

What would be the best way to bring all this information into a table-ready format? As far as I can tell, the first step is to create an array of all unique properties. But then without closing a group of clunkily foreach loops, I'm not sure where to go from here.

The desired output

You can do this in many ways , I have to do it like this:

Sample Value:

  $ Values_from_api = array ('silver' => array ('assets' = & gt; Array ('paragraph' => 2.0, 'ROS_ADI_1_1' => 37704.0, 'ROS_Medium_2' => 37711.0, 'ROS_Medium_3' = & gt 37546.0, 'ROS_Leaderboard_Footer' => 37 9 41.0, 'ROS_Leaderboard_Infinite Scroll '=> 3636.0,' value '=> 375,' discount '=> 0,' banner_cpm '=> 12.79,' banner_sov '=> 0.0099,' content_cpm '= & gt; ; 150.0, 'content_sov' => .0028, 'fixed_cpm' => 0.0, 'blended_cpm' => 26.26,),), 'gold' => Rani ('property' = & gt; array ('paragraph' => 2.0, 'ROS_Leaderboard' => 15212.0, 'ROS_CCF' => 1.0, 'ROS_Halfpage' => 15212.0, 'ROS_Medium_1' = & Gt; 12459.0, 'ROS_Medium_2' => 12476.0, 'ROS_Medium_3' = & gt; 12476.0, 'ROS_Leaderboard_Footer' => 12390.0, 'ROS_Leaderboard_Infinite Scroll' = & gt; 1239.0, 'value' = & gt; 500, 'Discount' = & gt; 0, 'banner_cpm' = & gt; 18.84, 'Banner_sov' = & gt; 0.008, 'content_cpm' = & gt; 150.0, 'content_sov' = & gt; 0.0028, 'fixed_cpm' = & gt; 0.0, 'blended_cpm' = & gt; 29.8,),)));   

First you should take all necessary columns:

  $ left_columns = array (); $ {$ Type ['property']; $ value] as {$ column = & gt; foreach {$ left_columns [] = $ column; Foreach (dollar type as $ type_key = & gt $ values_from_api)}} $ Left_columns = array_unique ($ left_columns); After you have collected the necessary columns, you can print them like this:  
  & lt; Table range = "1" cellpadding = "10" & gt; & Lt; Thead & gt; & Lt; Width = "200" & gt; & Lt; / V & gt; & Lt; Th & gt; Silver & lt; / Th & gt; & Lt; Th & gt; Gold & lt; / Th & gt; & Lt; / Thead & gt; & Lt; Tbody & gt; & Lt ;? php foreach ($ left_columns $ key = & gt; $ column_name):? & Gt; & Lt; TR & gt; & Lt; Td> & Lt ;? Php echo $ column_name; ? & Gt; & Lt; / TD & gt; & Lt ;? {Foreach ($ type $ column = & gt; $ value) {ifet ($ value [$ column_name])) {echo "& lt; td & rdquo; php foreach ($ values_from_api $ type_key = & gt; $ type) Gt; $ value [$ column] & lt; / td & gt; "; } Other {resonant "<">  ; }}}? & Gt; & Lt; / TR & gt; & Lt ;? php endforeach; ? & Gt; & Lt; / Tbody & gt; & Lt; / Table & gt;    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -