Php in_array() 未按预期工作
Php in_array() not working as expected
我目前正在处理多个 select 下拉列表。我需要在编辑表单上显示 selected 值。
in_array()
没有像我预期的那样工作,我的逻辑有错误吗?
显示数据库中 selected 值的代码是:
<div class="form-group">
<label class="col-md-3 control-label" for="selectbasic">Update Artist Selection</label>
<div class="col-md-6">
<select id="artists1" multiple="multiple" name="id_artist_fk[]" class="form-control ">
<?php
foreach ($artist_list as $key => $value) {
if (in_array($value['id_artist'], $current_artist_list, true)) {
$selected = "selected='selected'";
}
// print_r($value['id_artist']. "==". $current_artist_list);
echo "<option value=\"{$value['id_artist']}\" {$selected}>{$value['name']} {$value['surname']}</option>";
}
?>
</select>
</div>
</div>
$artist_list
通过以下方式获取:
public function get_artist_list() {
$sql = "SELECT * FROM tbl_v_artist WHERE status != 0;";
$result = $this->database->doSelectQuery($sql);
$artists = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()) {
$artist = array(
'id_artist' => $row['id_artist'],
'name' => $row['name'],
'surname' => $row['surname'],
'status' => $row['status']
);
array_push($artists, $artist);
}
}
return $artists;
}
$current_artist_list
通过以下方式获得:
$current_artist_list = $vid->get_artistsID_for_video($_POST['id_video']);
get_artistsID_for_video
是:
public function get_artistsID_for_video($video_id) {
try {
$sql = "SELECT
tbl_video_artist.id_artist_fk
FROM tbl_video_artist
left join tbl_v_artist
ON tbl_v_artist.id_artist = tbl_video_artist.id_artists
WHERE tbl_video_artist.id_video_fk = {$video_id};";
//echo $sql;
$result = $this->database->doSelectQuery($sql);
$artists = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()) {
$artist = array(
'id_artist_fk' => $row['id_artist_fk']
);
array_push($artists, $artist);
}
}
return $artists;
} catch (Exception $ex) {
$ex->getMessage();
$ex->getFile();
}
}
请帮我指明正确的方向。
我编辑了 get_artistsID_for_video 如下:
public function get_artistsID_for_video($video_id) {
try {
$sql = "SELECT
tbl_video_artist.id_artist_fk
FROM tbl_video_artist
left join tbl_v_artist
ON tbl_v_artist.id_artist = tbl_video_artist.id_artists
WHERE tbl_video_artist.id_video_fk = {$video_id};";
//echo $sql;
$result = $this->database->doSelectQuery($sql);
// $artists = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()) {
$artist [] = $row['id_artist_fk'];
// array_push($artists, $artist);
}
return $artist;
}
} catch (Exception $ex) {
$ex->getMessage();
$ex->getFile();
}
}
请检查 $current_artist_list 必须是单个数组。即 $current_artist_list[0] = 1;
$current_artist_list[1] = 2;
这会将您的 ID 与条件相匹配。现在看起来你的 $current_artist_list 是具有键的关联数组值。尝试按我上面提到的那样仅推送值,或者如下更改代码。
$artists = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()) {
$artist = array(
'id_artist_fk' => $row['id_artist_fk']
);
array_push($artists, $row['id_artist_fk']);
}
}
查看in_array
手册:
in_array — Checks if a value exists in an array
那么,是什么检查了您的 in_array($value['id_artist'], $current_artist_list, true)
?
它检查 $current_artist_list
中是否存在 $value['id_artist']
的值。例如,如果 $value['id_artist']
是 20,in_array
检查值 20 是否在您的数组中。
但值 20 在您的 $current_artist_list
数组中 NOT。
因为 $current_artist_list
中每个元素的格式都是 array('id_artist_fk' => $row['id_artist_fk'])
。
因此,您正在搜索 20,但您存储的值是 ('id_artist_fk' => 20)
.
20
不等于 array
.
修复在 get_artistsID_for_video()
:
while ($row = $result->fetch_array()) {
$artists[] = $row['id_artist_fk'];
}
现在您在数组中搜索 20
,其中每个元素也是一个数字 。
使您的搜索更快(仍在get_artistsID_for_video
):
while ($row = $result->fetch_array()) {
// create array key with value of artist id
$artists[$row['id_artist_fk']] = 1;
}
并将in_array
替换为:
// check for existence of a key, not value.
if (!empty($current_artist_list[$value['id_artist']])) {
$selected = "selected='selected'";
}
我目前正在处理多个 select 下拉列表。我需要在编辑表单上显示 selected 值。
in_array()
没有像我预期的那样工作,我的逻辑有错误吗?
显示数据库中 selected 值的代码是:
<div class="form-group">
<label class="col-md-3 control-label" for="selectbasic">Update Artist Selection</label>
<div class="col-md-6">
<select id="artists1" multiple="multiple" name="id_artist_fk[]" class="form-control ">
<?php
foreach ($artist_list as $key => $value) {
if (in_array($value['id_artist'], $current_artist_list, true)) {
$selected = "selected='selected'";
}
// print_r($value['id_artist']. "==". $current_artist_list);
echo "<option value=\"{$value['id_artist']}\" {$selected}>{$value['name']} {$value['surname']}</option>";
}
?>
</select>
</div>
</div>
$artist_list
通过以下方式获取:
public function get_artist_list() {
$sql = "SELECT * FROM tbl_v_artist WHERE status != 0;";
$result = $this->database->doSelectQuery($sql);
$artists = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()) {
$artist = array(
'id_artist' => $row['id_artist'],
'name' => $row['name'],
'surname' => $row['surname'],
'status' => $row['status']
);
array_push($artists, $artist);
}
}
return $artists;
}
$current_artist_list
通过以下方式获得:
$current_artist_list = $vid->get_artistsID_for_video($_POST['id_video']);
get_artistsID_for_video
是:
public function get_artistsID_for_video($video_id) {
try {
$sql = "SELECT
tbl_video_artist.id_artist_fk
FROM tbl_video_artist
left join tbl_v_artist
ON tbl_v_artist.id_artist = tbl_video_artist.id_artists
WHERE tbl_video_artist.id_video_fk = {$video_id};";
//echo $sql;
$result = $this->database->doSelectQuery($sql);
$artists = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()) {
$artist = array(
'id_artist_fk' => $row['id_artist_fk']
);
array_push($artists, $artist);
}
}
return $artists;
} catch (Exception $ex) {
$ex->getMessage();
$ex->getFile();
}
}
请帮我指明正确的方向。
我编辑了 get_artistsID_for_video 如下:
public function get_artistsID_for_video($video_id) {
try {
$sql = "SELECT
tbl_video_artist.id_artist_fk
FROM tbl_video_artist
left join tbl_v_artist
ON tbl_v_artist.id_artist = tbl_video_artist.id_artists
WHERE tbl_video_artist.id_video_fk = {$video_id};";
//echo $sql;
$result = $this->database->doSelectQuery($sql);
// $artists = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()) {
$artist [] = $row['id_artist_fk'];
// array_push($artists, $artist);
}
return $artist;
}
} catch (Exception $ex) {
$ex->getMessage();
$ex->getFile();
}
}
请检查 $current_artist_list 必须是单个数组。即 $current_artist_list[0] = 1; $current_artist_list[1] = 2;
这会将您的 ID 与条件相匹配。现在看起来你的 $current_artist_list 是具有键的关联数组值。尝试按我上面提到的那样仅推送值,或者如下更改代码。
$artists = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()) {
$artist = array(
'id_artist_fk' => $row['id_artist_fk']
);
array_push($artists, $row['id_artist_fk']);
}
}
查看in_array
手册:
in_array — Checks if a value exists in an array
那么,是什么检查了您的 in_array($value['id_artist'], $current_artist_list, true)
?
它检查 $current_artist_list
中是否存在 $value['id_artist']
的值。例如,如果 $value['id_artist']
是 20,in_array
检查值 20 是否在您的数组中。
但值 20 在您的 $current_artist_list
数组中 NOT。
因为 $current_artist_list
中每个元素的格式都是 array('id_artist_fk' => $row['id_artist_fk'])
。
因此,您正在搜索 20,但您存储的值是 ('id_artist_fk' => 20)
.
20
不等于 array
.
修复在 get_artistsID_for_video()
:
while ($row = $result->fetch_array()) {
$artists[] = $row['id_artist_fk'];
}
现在您在数组中搜索 20
,其中每个元素也是一个数字 。
使您的搜索更快(仍在get_artistsID_for_video
):
while ($row = $result->fetch_array()) {
// create array key with value of artist id
$artists[$row['id_artist_fk']] = 1;
}
并将in_array
替换为:
// check for existence of a key, not value.
if (!empty($current_artist_list[$value['id_artist']])) {
$selected = "selected='selected'";
}