0

I'm trying to create an HTML using PHP associate array. My function looks like:

function generateTable($associative_array){
echo '<table><thead><tr><th>';
echo implode('</th><th>', array_keys(current($associative_array)));
echo '</th></tr></thead><tbody>';
foreach ($associative_array as $row){
    array_map('htmlentities', $row);
    echo '<tr valign="top"><td>';
    echo implode('</td><td>', $row); 
    echo '</td></tr>';
}
echo '</tbody></table>';
}

AND my associative array generated is like this:

array(2) { 
  [0]=> array(3) { 
    ["Objective"]=> string(21) "Product Catalog Sales" 
    ["Targeting Group"]=> array(3) { 
      [0]=> string(31) "female Age 18-24 28.73% cheaper" 
      [1]=> string(31) "female Age 25-34 28.18% cheaper"  
      [2]=> string(30) "female Age 35-44 4.51% cheaper" 
    } 
    ["Placement"]=> array(3) { 
      [0]=> string(33) "mobile_feed iphone 30.62% cheaper" 
      [1]=> string(45) "mobile_feed android_smartphone 19.58% cheaper"  
      [2]=> string(33) "right_hand desktop 21.48% cheaper" 
    } 
  } 
  [1]=> array(3) { 
    ["Objective"]=> string(11) "Conversions" 
    ["Targeting Group"]=> array(6) { 
      [0]=> string(31) "female Age 18-24 28.73% cheaper" 
      [1]=> string(31) "female Age 25-34 28.18% cheaper" 
      [2]=> string(30) "female Age 35-44 4.51% cheaper" 
      [3]=> string(31) "female Age 18-24 14.41% cheaper" 
      [4]=> string(31) "female Age 25-34 14.75% cheaper" 
      [5]=> string(30) "female Age 35-44 6.14% cheaper" 
    } 
    ["Placement"]=> array(4) { 
      [0]=> string(33) "mobile_feed iphone 30.62% cheaper" 
      [1]=> string(45) "mobile_feed android_smartphone 19.58% cheaper"  
      [2]=> string(33) "right_hand desktop 21.48% cheaper" 
      [3]=> string(33) "right_hand desktop 58.77% cheaper" 
    } 
  }
}

The table structure is being generated, but it is unable to input the values, below is the error that is displayed: can someone please help how to modify the function.

Warning: htmlentities() expects parameter 1 to be string

Notice: Array to string conversion

9
  • Make sure all the values are strings and not arrays/objects Commented Jan 5, 2016 at 11:53
  • it would be difficult to collect all values as strings.I need to collect some of them as arrays Commented Jan 5, 2016 at 11:54
  • You could use serialize() or something similiar to display them as text. Or you could loop through the inner array and create a sub-row so to speak. I don't know any decent solutions for this to be honest, its always a bit odd Commented Jan 5, 2016 at 11:56
  • 2
    Not the source of your warning, but your code does not output or assign the result of array_map(), which does not operate on arrays by reference. So this line of code is redundant? Commented Jan 5, 2016 at 12:04
  • you should use array_walk_recursive . Commented Jan 5, 2016 at 12:05

1 Answer 1

1

Before applying any escaping or quoting functions to this complex construct, get yourself a idea of what the result should look alike. That's a two- to three-dimensional array. It has, on the second level, both strings and another array. Furthermore, it's a mixed flat and associated array.

So your "row" isn't a row at all, but a complex associative array itself.

I suggest to create objects from the second level on:

class AdsSpec {
  private $objective;
  private $targetGroup;
  private $placement;
   ...
  function getObjective()
  { 
    return $this->objective;
  }
}

etc. And then

$newArray = array();

foreach ($associative_array as $obj) {
   $ads = new AdSpec($obj['Objective'], $obj["Targeting Group"], $obj["Placement"]);
   $newArray[] = $ads;
}

furthermore:

foreach ( $newArray as $adsObject )
{
 ...
    echo htmlentities($adsObject->getObjective());

 ...

This would clear the array stuff and separate rendering from the object itself.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.