Gafwebmaster
Gafwebmaster

Reputation: 41

Php array wrong output when try to add in html tag

I wander why my code produce a wrong output.

$array = require 'http://vilavaleaprahovei.ro/kimea/allMarks.php';
$output = array_keys($array);
$option = '';
foreach ($output as $val) {
    $option .= "<option>$val</option>";
}
print_r($option);

Expected result is:

<option>ALCAR STAHLRAD</option>
<option>ENZO</option>
<option>DEZENT</option>
...........

Thank you in advance!

Upvotes: 1

Views: 133

Answers (3)

simonecosci
simonecosci

Reputation: 1204

ensure you have allow_url_include = On in your php.ini

if allMarks.php is a php file where you declare and return an array you should open the php script tag <?php first and use the return keyword as follow:

<?php
return array (
  'ALCAR STAHLRAD' => 
  array ( ...

Upvotes: 2

Rahul Pawar
Rahul Pawar

Reputation: 1036

Try this I hope it will works :

$data = array('ALCAR STAHLRAD' => array('diametru' => array(0 => 15, 6 => 16, 9 => 14, 14 => 13, 20 => 17, 468 => 20,), 'latime' => array(0 => 6, 9 => 5, 10 => 4, 27 => 7, 501 => 3,), 'pcd' => array(0 => 15, 6 => 16, 9 => 14, 14 => 13, 20 => 17, 468 => 20,), 'pcd1' => array(0 => 15, 6 => 16, 9 => 14, 14 => 13, 20 => 17, 468 => 20,), 'et' => array(0 => 15, 6 => 16, 9 => 14, 14 => 13, 20 => 17, 468 => 20,), 'cb' => array(0 => 15, 6 => 16, 9 => 14, 14 => 13, 20 => 17, 468 => 20,),), 'ENZO' => array('diametru' => array(0 => 16, 2 => 17, 3 => 15, 10 => 14, 14 => 18,), 'latime' => array(0 => 6, 1 => 7, 10 => 5, 35 => 8,), 'pcd' => array(0 => 16, 2 => 17, 3 => 15, 10 => 14, 14 => 18,), 'pcd1' => array(0 => 16, 2 => 17, 3 => 15, 10 => 14, 14 => 18,), 'et' => array(0 => 16, 2 => 17, 3 => 15, 10 => 14, 14 => 18,), 'cb' => array(0 => 16, 2 => 17, 3 => 15, 10 => 14, 14 => 18,),), 'DEZENT' => array('diametru' => array(0 => 17, 1 => 16, 6 => 15, 24 => 18, 46 => 14, 80 => 19, 159 => 20,), 'latime' => array(0 => 7, 6 => 6, 20 => 8, 63 => 5, 120 => 4, 159 => 9,), 'pcd' => array(0 => 17, 1 => 16, 6 => 15, 24 => 18, 46 => 14, 80 => 19, 159 => 20,), 'pcd1' => array(0 => 17, 1 => 16, 6 => 15, 24 => 18, 46 => 14, 80 => 19, 159 => 20,), 'et' => array(0 => 17, 1 => 16, 6 => 15, 24 => 18, 46 => 14, 80 => 19, 159 => 20,), 'cb' => array(0 => 17, 1 => 16, 6 => 15, 24 => 18, 46 => 14, 80 => 19, 159 => 20,),), 'DOTZ' => array('diametru' => array(0 => 17, 1 => 15, 3 => 18, 5 => 16, 6 => 19, 65 => 20,), 'latime' => array(0 => 7, 1 => 6, 2 => 8, 71 => 9,), 'pcd' => array(0 => 17, 1 => 15, 3 => 18, 5 => 16, 6 => 19, 65 => 20,), 'pcd1' => array(0 => 17, 1 => 15, 3 => 18, 5 => 16, 6 => 19, 65 => 20,), 'et' => array(0 => 17, 1 => 15, 3 => 18, 5 => 16, 6 => 19, 65 => 20,), 'cb' => array(0 => 17, 1 => 15, 3 => 18, 5 => 16, 6 => 19, 65 => 20,),), 'AEZ' => array('diametru' => array(0 => 16, 1 => 17, 5 => 18, 10 => 19, 28 => 20, 106 => 21, 157 => 22,), 'latime' => array(0 => 7, 1 => 8, 16 => 9, 105 => 6, 106 => 11, 114 => 10,), 'pcd' => array(0 => 16, 1 => 17, 5 => 18, 10 => 19, 28 => 20, 106 => 21, 157 => 22,), 'pcd1' => array(0 => 16, 1 => 17, 5 => 18, 10 => 19, 28 => 20, 106 => 21, 157 => 22,), 'et' => array(0 => 16, 1 => 17, 5 => 18, 10 => 19, 28 => 20, 106 => 21, 157 => 22,), 'cb' => array(0 => 16, 1 => 17, 5 => 18, 10 => 19, 28 => 20, 106 => 21, 157 => 22,),),);
// Above array is your data
$option = '';
foreach ($data as $key => $val) {
    $option .= "<option>$key</option>";
}
print_r(htmlentities($option));

Upvotes: 1

Pritam Jinal
Pritam Jinal

Reputation: 151

the problem you are facing is that you want to print html without rendering it on html so you should be using

 foreach ($output as $val) {
    $option .= "&lt;option&gt;$val&lt;/option&gt; </br>";
}

instead of

foreach ($output as $val) {
    $option .= "<option>$val</option>";
}

Upvotes: -1

Related Questions