我尝试使用foreach循环更新数组中的值。然后使用更新的值打印数组。我在这里发布我当前的代码。请回复示例代码。感谢您。 这是我的阵列:
[associated_branch] => Array
(
[0] => Array
(
[store_id] => 0
[store_name] => Bazar Kolkata
[location] => Array
(
[location] => Sonarpur
)
[address_line1] => J L N Road
[address_line2] => Kolkata
[city] => 4
[state] => 24
)
[1] => Array
(
[store_id] => 16
[store_name] => KC DAS
[location] => Array
(
[location] => Garia Hat
)
[address_line1] => kolkata
[address_line2] =>
[city] => 4
[state] => 24
)
)
我希望:
[associated_branch] => Array
(
[0] => Array
(
[store_id] => 0
[store_name] => Bazar Kolkata
[location] => Sonarpur
[address_line1] => J L N Road
[address_line2] => Kolkata
[city] => 4
[state] => 24
)
[1] => Array
(
[store_id] => 16
[store_name] => KC DAS
[location] => Garia Hat
[address_line1] => kolkata
[address_line2] =>
[city] => 4
[state] => 24
)
)
我的Sql:
$assoBrSql="SELECT store_id, store_name, location, address_line1, address_line2, city, state FROM ".DB_MY_PREFIX."my_web_stores WHERE store_id IN($row[branch_ids]) AND cur_status=1 && is_more_branch =1 AND associated_branch IS NOT NULL";
$branches = $db->ExecuteQuery($assoBrSql);
$row['associated_branch']=$branches;
foreach ($row['associated_branch'] as $key => $value) {
$locsql="SELECT location FROM " .DB_MY_PREFIX. "my_localities WHERE location_id='".$value['location']."'";
$locrows=$db->ExecuteQuery($locsql);
$row['associated_branch'][$key]['location']=$locrows[0];
}
如何建立这个?
答案 0 :(得分:0)
你应该只需要改变:
$row['associated_branch'][$key]['location']=$locrows[0];
到
$row['associated_branch'][$key]['location']=$locrows[0]['location'];
您当前正在追加第二个查询的结果,该查询是一个包含单个元素“位置”的数组。
答案 1 :(得分:0)
如果分支可以有多个位置,那么您可以更改初始查询以使用带有groupconcat的子查询来获取所有位置。像这样的东西
$assoBrSql="SELECT store_id, store_name, location, address_line1, address_line2, city, state, (SELECT GROUP_CONCAT(a.location) as locations FROM " .DB_MY_PREFIX. "my_localities a WHERE a.location_id=location) as locations FROM ".DB_MY_PREFIX."my_web_stores WHERE store_id IN($row[branch_ids]) AND cur_status=1 && is_more_branch =1 AND associated_branch IS NOT NULL";
$branches = $db->ExecuteQuery($assoBrSql);
$row['associated_branch']=$branches;
代码未经编辑编写,希望没有拼写错误等;)