我正在开发一家披萨店的电子商务电子商务,现在我正试图获得规模(熟悉)和成分(Pernildol�,之前订购的披萨的Bac�,Emmental )。我想要获取的数据(本段中的斜体值)从数据库序列化:
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
# define nodes, node types, and color range
np.random.seed(10000)
nodes = list('abcdefghijkl')
nodeTypes = ['foo','bar','baz']
nodeColors = [0.0, 0.5, 1.0]
# assign each node a type and color via a dictionaries
nodeTypeDict = dict(zip(nodeTypes, [nodes[:4],nodes[4:8],nodes[8:]]))
nodeColorDict = dict(zip(nodeTypes, nodeColors))
nodePos = dict(zip(nodes,[(np.random.random(),np.random.random())
for i in range(len(nodes))]))
# generate the graph
g = nx.Graph()
g.add_nodes_from(nodes)
# create image canvas and axes
fig, ax = plt.subplots(1, figsize=(6,6))
# iterate each nodetype, changing colors and labels of the nodes
for nt in nodeTypes:
# choose nodes and color for each iteration
nlist = nodeTypeDict[nt]
ncolor = [nodeColorDict[nt]]*len(nlist)
# draw the graph
nx.draw_networkx_nodes(g,
pos=nodePos,
nodelist=nlist,
ax=ax,
cmap=plt.cm.brg,
vmin=0.0,
vmax=1.0,
node_color=ncolor,
label=nt) # the label for each iteration is
# the node type
# here is the problem. The legend does not inherit the colors.
ax.legend(scatterpoints=1)
plt.show()
我发现'反序列化' php方法,我试过这个:
a:4:{s:10:"attributes";a:2:{s:6:"Tamany";a:1:{i:3;s:8:"Familiar";}s:11:"Ingredients";a:3:{i:318;s:12:"Pernil dol�";i:270;s:5:"Bac�";i:294;s:8:"Emmental";}}s:9:"shippable";s:1:"0";s:4:"type";s:5:"pizza";s:6:"module";s:10:"uc_product";}array(4) { ["attributes"]=> array(2) { ["Tamany"]=> array(1) { [3]=> string(8) "Familiar" } ["Ingredients"]=> array(3) { [318]=> string(11) "Pernil dol�" [270]=> string(4) "Bac�" [294]=> string(8) "Emmental" } } ["shippable"]=> string(1) "0" ["type"]=> string(5) "pizza" ["module"]=> string(10) "uc_product" }
在我这样做之后,我得到了这个多维数组(更具人性化):
$attr = $row['data']; // data from database
$data = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $attr); // I did this because I get some errors...
下一步是尝试使用foreach循环循环结果数据,如下所示:
array(4) { ["attributes"]=> array(2) { ["Tamany"]=> array(1) { [3]=> string(8) "Familiar" } ["Ingredients"]=> array(3) { [318]=> string(11) "Pernil dol�" [270]=> string(4) "Bac�" [294]=> string(8) "Emmental" } } ["shippable"]=> string(1) "0" ["type"]=> string(5) "pizza" ["module"]=> string(10) "uc_product" }
我是一名php初学PHP开发人员,我无法弄清楚如何循环这个数组以获得我需要的值。我收到此错误:
foreach($data['attributes'] as $item)
{
print '<ul>';
foreach($item as $value)
{
print_r('<li>' . $value . '</li>');
}
print '</ul>';
}
有人能告诉我如何循环这个数组来获取数据吗? 任何帮助都将非常非常感激。
致以最诚挚的问候,
答案 0 :(得分:0)
我为你创建了这个例子。首先,我声明了一个数组,它相信模仿你必须解析的数组。然后我循环并输出数组的内容。
<?php
$array = array(
0 => array(
'0' => 'John Doe',
'1' => 'john@example.com'
),
1 => array(
'0' => 'Jane Doe',
'1' => 'jane@example.com'
),
);
foreach ($array as $key => $value) {
$thisArray = $array[$key];
print_r('<ul>');
foreach ($thisArray as $key2 => $value){
print_r('<li>'.$thisArray[$key2].'</li>');
}
print_r('</ul>');
}
?>
答案 1 :(得分:0)
最后,根据@Gregory Hart的答案,我达到了目标。这是在我的特定情况下可以实现的最终代码:
$data = $row['data'];
$attrib = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $data);
$attr = unserialize($attrib);
foreach ($attr as $key => $value)
{
print_r('<ul>');
$thisArray = $attr[$key];
foreach ($thisArray as $key2 => $value2)
{
print_r('<li>' . $key2 . ': ');
$thisArray2 = $attr[$key][$key2];
foreach ($thisArray2 as $key3 => $value3)
{
if ($key2 == 'Tamany')
print_r('<span class="label label-warning">' . utf8_encode($thisArray2[$key3]) . '</span> ');
if ($key2 == 'Ingredients')
print_r('<span class="label label-success">' . utf8_encode($thisArray2[$key3]) . '</span> ');
if ($key2 == 'Salsa')
print_r('<span class="label label-primary">' . utf8_encode($thisArray2[$key3]) . '</span> ');
}
print '</li>';
}
print_r('</ul>');
}
谢谢你的帮助!