0

我正在从数据库中检索保存多维数组的数组值..我想添加

|

在数组的两个成员值之间..我尝试了基本的内爆函数,最后提到了我尝试了什么..

我在下面提到了循环

$sql = "SELECT `categories` FROM `jos_js_res_record` WHERE `categories` LIKE '%Autumn Market Session%' OR `categories` LIKE '%Farmer Market Profiles%' OR `categories` LIKE '%Spring Market Session%' OR `categories` LIKE '%Summer Market Session%' OR `categories` LIKE '%Winter Market Session%' ORDER BY `user_id`";

$db->setQuery( $sql );

$unserial = $db->loadObjectList();

foreach ($unserial as $dataElement) {
  $categories = unserialize($dataElement->categories);

if (is_array($categories)) {

foreach ($categories as $unserKey => $unserailData) { 

print $unserailData;
    }

}

echo '<br />';

}

我已经在每个循环的第二行尝试了下面的代码行,但显示错误

$comma_separated = implode(",", $unserailData);

请指导我如何将第二个数组$unserailData中的值内爆?

$unserailData 中的数据如下所示

夏季市场会议

夏季市场会议

夏季市场会议秋季市场会议

夏季市场会议秋季市场会议

4

2 回答 2

0

如果我正确阅读了您的请求,则以下内容应该有效:

$sql = "SELECT `categories` FROM `jos_js_res_record` WHERE `categories` LIKE '%Autumn Market Session%' OR `categories` LIKE '%Farmer Market Profiles%' OR `categories` LIKE '%Spring Market Session%' OR `categories` LIKE '%Summer Market Session%' OR `categories` LIKE '%Winter Market Session%' ORDER BY `user_id`"; 

$db->setQuery( $sql ); 

$unserial = $db->loadObjectList(); 

foreach ($unserial as $dataElement) { 
    $categories = unserialize($dataElement->categories); 
    if (is_array($categories)) {
         echo implode('|', $categories);
    } elseif (is_string($categories)) {
         echo $categories;
    }  
    echo '<br />'; 
}
于 2012-06-21T03:57:43.103 回答
0

我做了下面的技巧,但@Robbie 提到的代码更干净,所以我用他的代码替换了我的代码。

我做了这个把戏

foreach ($unserial as $dataElement) {
  $categories = unserialize($dataElement->categories);
if (is_array($categories))
{ 
foreach ($categories as $unserKey => $unserailData) {
$arr2[] = $unserailData;
}
$imploded = implode(" | ", $arr2);
print_r($imploded);
$arr2 = array();
}

else{
echo "Category not stored correctly";
}
echo '<br />';
}
于 2012-06-21T04:39:02.773 回答