我遇到问题array_push
(或使用[]
- 这似乎无关紧要使用哪种方法)让我发疯。我正在使用一系列SQL查询构建一个json文件,其中包括运行SQL查询,然后将一个函数应用于结果以构建一系列节点:
function array_builder($result, $id_name, $column_name, $type, $color)
{
global $mysqli;
global $person_color;
global $multiple_author_color;
global $otherwork_other_color;
global $otherwork_lydgate_color;
global $work_color;
global $witness_color;
global $otherwork_color;
global $location_color;
global $city_color;
global $region_color;
global $country_color;
global $edition_color;
global $default_color;
global $proofed_color;
global $draft_color;
global $DNB_color;
while ($row = $result->fetch_array())
{
$type_construct = $type . $row[$id_name];
$name = UTF_clean($row[$column_name]);
switch ($type)
{
case "work":
person_checker($row);
$url_color = status_indicator($row,$type)[0];
$link = status_indicator($row,$type)[1];
$new_type = $type;
$new_color = $work_color;
break;
case "other_work":
person_checker($row);
$url_color = status_indicator($row,$type)[0];
$link = status_indicator($row,$type)[1];
$new_type = $type;
$new_color = $otherwork_other_color;
break;
case "other_Lydgate_work":
person_checker($row);
$url_color = status_indicator($row,$type)[0];
$link = status_indicator($row,$type)[1];
$new_type = $type;
$new_color = $otherwork_lydgate_color;
break;
case "multiple_author_work":
person_checker($row);
$url_color = status_indicator($row,$type)[0];
$link = status_indicator($row,$type)[1];
$new_type = $type;
$new_color = $multiple_author_color;
break;
case "person":
$url_color = status_indicator($row,$type)[0];
$link = status_indicator($row,$type)[1];
$new_type = $type;
$new_color = $person_color;
break;
default:
$url_color = 'aliceblue';
$link = NULL;
$new_color = $color;
$new_type = $type;
//debug_to_console("default");
}
$item_array= array(
'id' => $type_construct,
'name' => $name,
'type' => $new_type,
'color' => $new_color,
'url_color' => $url_color,
'link' => $link,
);
if (strpos($new_type, 'work') !== false) {
$to_check = $type . $row[$id_name];
$test = array_search_multidim($GLOBALS['array'],'id', $to_check);
if ($test != "") {
}
else
{
array_push($GLOBALS['array'], $item_array);
}
}
else {
if (!in_array_r($item_array, $GLOBALS['array'])) {
array_push($GLOBALS['array'], $item_array);
}
}
}
}
这可以按预期工作,生成一个数组,然后使用json_encode
:
[
{
"id": "person1",
"name": "John Lydgate",
"type": "person",
"color": "darkred",
"url_color": "cyan",
"link": "http://dx.doi.org/10.1093/ref:odnb/17238"
},
{
"id": "work55",
"name": "A Balade declaring that wemens chastite Doeth moche excel all treasure worldly",
"type": "work",
"color": "midnightblue",
"url_color": "aliceblue",
"link": ""
},
{
"id": "witness7",
"name": "R.3.19",
"type": "witness",
"color": "green",
"url_color": "aliceblue",
"link": null
},
{
"id": "location5",
"name": "Trinity College",
"type": "location",
"color": "gold",
"url_color": "aliceblue",
"link": null
}
]
当我尝试在while循环中多次调用array_builder
相同的SQL结果时出现问题:
$witness_result->data_seek(0);
while ($row = $witness_result->fetch_array())
{
$other_work_sql = "select Work.Work, Work.DIMEV_number, Work.id as Work_id, Witness_work_lookup.Witness_id, Witness_work_lookup.Beginning_URL,Witness_work_role_lookup.Person_id, People.Name as Person_name,Status_id from People inner join Witness_work_role_lookup on Witness_work_role_lookup.Person_id = People.id inner join Witness_work_lookup on Witness_work_lookup.id = Witness_work_role_lookup.Witness_work_id inner join Work on Witness_work_lookup.Work_id = Work.id where Witness_id = " . $row['Witness_id'] . " AND Person_id != 1 AND Work.id not in (" . $_GET['string'] . ")";
debug_to_console($other_work_sql);
$other_work_result = mysqli_query($mysqli, $other_work_sql);
array_builder($other_work_result, "Work_id","Work", "other_work", $otherwork_other_color);
link_builder($other_work_result, "other_work", "witness", "Work_id", "Witness_id");
}
在get字符串中调用带有多个id的while循环中的array_builder
,然后我得到:
{
"0": {
"id": "person1",
"name": "John Lydgate",
"type": "person",
"color": "darkred",
"url_color": "cyan",
"link": "http://dx.doi.org/10.1093/ref:odnb/17238"
},
"1": {
"id": "work14",
"name": "A Mumming at Eltham",
"type": "work",
"color": "midnightblue",
"url_color": "aliceblue",
"link": ""
},
"2": {
"id": "work55",
"name": "A Balade declaring that wemens chastite Doeth moche excel all treasure worldly",
"type": "work",
"color": "midnightblue",
"url_color": "aliceblue",
"link": ""
}
etc...
对我而言,显而易见的是,while循环导致数组在遍历项目时重新索引自身,但我不明白的是为什么或如何阻止它这样做。另一个名为link_builder
的函数没有这个问题。这里仅供参考。
function link_builder($result,$source_type,$target_type,$source_id,$target_id) {
$result->data_seek(0);
while ($row = $result->fetch_array()) {
$source_location = array_search_multidim($GLOBALS['array'],'id',$source_type . $row[$source_id]);
$target_location = array_search_multidim($GLOBALS['array'],'id',$target_type . $row[$target_id]);
$color = $GLOBALS['array'][$target_location]['color'];
$array = array(
'color' => $color,
'source' => $source_type . $row[$source_id] ,
'target' => $target_type . $row[$target_id],
'value' => 1);
if (in_array_r($array,$GLOBALS['link_array']))
{}
else
{
array_push($GLOBALS['link_array'],$array);
}
}
}
如果有人可以向我解释为什么array_builder
会显示这种行为(也许,为什么link_builder
不是这样)我会很感激。