Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将imgsrc转换为字符串,将其放入数组并使用json编码?_Php_Javascript_Sql - Fatal编程技术网

Php 如何将imgsrc转换为字符串,将其放入数组并使用json编码?

Php 如何将imgsrc转换为字符串,将其放入数组并使用json编码?,php,javascript,sql,Php,Javascript,Sql,嗨,我正在使用函数json_encode通过关联数组从数据库检索数据。然而,我想问,如何将img标记转换为字符串并将其放入关联数组中,然后让json对其进行编码并将其吐出,同时将img标记保持为字符串 while($row=$result->FetchRow()) { $id= (float)$row['id']; $name = $row['name']; $color1 = $row['color']; $type1 = $row['type']; $to= (f

嗨,我正在使用函数json_encode通过关联数组从数据库检索数据。然而,我想问,如何将img标记转换为字符串并将其放入关联数组中,然后让json对其进行编码并将其吐出,同时将img标记保持为字符串

 while($row=$result->FetchRow())
{
  $id= (float)$row['id'];
  $name = $row['name'];
  $color1 = $row['color'];
  $type1 = $row['type'];
  $to= (float)$row['to']; 
  $thumb =$row['thumb']; //image path

  $array = array(
  "adjacencies" => array( array(
  "nodeTo" => "$to",
  "nodeFrom" => "$id",
  "data" => array() )),
  "data" => array(
   "$"."color" => $color1,
   "$"."type" => $type1 ),
  "id" => $id,
  "name" => "<img src='$thumb' height='30' width='30' alt='root'/><label>$name</label> ");
}
$json = json_encode($array);
print "$json";
return $json;
while($row=$result->FetchRow())
{
$id=(浮动)$row['id'];
$name=$row['name'];
$color1=$row['color'];
$type1=$row['type'];
$to=(浮动)$row['to'];
$thumb=$row['thumb'];//图像路径
$array=array(
“邻接”=>数组(数组(
“nodeTo”=>“$to”,
“nodeFrom”=>“$id”,
“数据”=>array()),
“数据”=>数组(
“$”“color”=>$color1,
“$”“类型”=>$type1),
“id”=>$id,
“名称”=>“$name”);
}
$json=json_encode($array);
打印“$json”;
返回$json;
试试这个:

$array=array();
while($row=$result->FetchRow())
{
    $id= (float)$row['id'];
    $name = $row['name'];
    $color1 = $row['color'];
    $type1 = $row['type'];
    $to= (float)$row['to']; 
    $thumb =$row['thumb']; //image path

    $array = array(
        "adjacencies" => array( array(
            "nodeTo" => "$to",
            "nodeFrom" => "$id",
            "data" => array() 
        )),
        "data" => array(
            "$"."color" => $color1,
            "$"."type" => $type1 ),
        "id" => $id,
        "name" =>htmlspecialchars("<img src='".$thumb."' height='30' width='30' alt='root'/><label>".$name."</label>")
    );
}
$json = json_encode($array);
echo $json;
return $json;
$array=array();
而($row=$result->FetchRow())
{
$id=(浮动)$row['id'];
$name=$row['name'];
$color1=$row['color'];
$type1=$row['type'];
$to=(浮动)$row['to'];
$thumb=$row['thumb'];//图像路径
$array=array(
“邻接”=>数组(数组(
“nodeTo”=>“$to”,
“nodeFrom”=>“$id”,
“数据”=>数组()
)),
“数据”=>数组(
“$”“color”=>$color1,
“$”“类型”=>$type1),
“id”=>$id,
“name”=>htmlspecialchars(“$name.”)
);
}
$json=json_encode($array);
echo$json;
返回$json;

阅读

首先,每次通过循环时都会覆盖$array。考虑固定以下行< /P>
...
$thumb =$row['thumb']; //image path

$array = array(
"adjacencies" => array( array(
"nodeTo" => "$to",
...

除此之外,只需使用一些默认变量来测试代码

$id = $to = 1;
$color1 = 'green';
$type1 = 'type1';
$thumb = 'image.jpg';
$name = 'image name';
$array = array(
    "adjacencies" => array(
        array(
            "nodeTo" => $to,
            "nodeFrom" => $id,
            "data" => array()
        )
    ),
    "data" => array(
        "$"."color" => $color1,
        "$"."type" => $type1
    ),
    "id" => $id,
    "name" => "<img src='$thumb' height='30' width='30' alt='root'/><label>$name</label> "
);
echo json_encode($array);
$id=$to=1;
$color1=‘绿色’;
$type1='type1';
$thumb='image.jpg';
$name='image name';
$array=array(
“邻接”=>数组(
排列(
“nodeTo”=>$to,
“nodeFrom”=>$id,
“数据”=>数组()
)
),
“数据”=>数组(
“$”“color”=>$color1,
“$”“类型”=>$type1
),
“id”=>$id,
“名称”=>“$name”
);
echo json_编码($array);
返回给我这个JSON,这似乎是您正在寻找的

{
    "adjacencies": [
        {
            "nodeTo": 1,
            "nodeFrom": 1,
            "data": []
        }
    ],
    "data": {
        "$color": "green",
        "$type": "type1"
    },
    "id": 1,
    "name": "<img src='image.jpg' height='30' width='30' alt='root'/><label>image name</label> "
}
{
“邻接”:[
{
"点头":一,,
“nodeFrom”:1,
“数据”:[]
}
],
“数据”:{
“$color”:“绿色”,
$type:“类型1”
},
“id”:1,
“名称”:“图像名称”
}

使用htmlspecialchars确保任何有问题的字符都被转义()。除此之外,在将数据插入DOM之前,它只是字符串数据

我假设在循环的每次迭代中,您都打算向数组$array中添加另一个元素。您希望在循环之前初始化$array变量

$array = array()
而不是

$array = array(
...
)
在循环中,执行以下操作

$array[$key] = array(
...
)

其中,$key是要用于索引数组的键。我假设你想用$id来做这个。您还可以增加一个整数并使用它。

您正在做的事情不起作用?否浏览器返回null作为名称键对中的值查看您的代码,这应该起作用吗?还有,为什么要将$row['id']作为浮动呢?此外,每次循环时都会覆盖$array值。json编码将我的int作为一个sting,因此我必须手动将其强制转换。我应该将该$array放入数组中吗?像$array=array[],$array=array(迭代);{“邻接”:[{“nodeTo”:“2”,“nodeFrom”:“1”,“data”:[]}],“data”:{“$color”:“#83548B”,“$type”:“circle”},“id”:1,“name”:“Blue Jay”}
$array[$key] = array(
...
)