PHP数组 - 选择它的一部分

时间:2014-11-20 11:37:25

标签: php arrays foreach

这可能会被某人看到并且能够说...这一点使它工作/改变它然而它会起作用...

我有一个存储在数据库中的数组

item1title:::item1product:::item1code###iten2title:::item2product:::item2code etc

但是,我不能只用...获取阵列的一部分

  if(isset($itemConversations) && !empty($itemConversations)){
    $mAttributes = array();
    if(strpos($item['itemConversations'], "###") !== false){
        $mAttributes = explode("###", $item['itemConversations']);
    } else {
        $mAttributes[] = $item['itemConversations'];
    }
    foreach($mAttributes as &$mAttribute){
        if(strpos($mAttribute, ":::") !== false){
            $mAttribute = explode(":::", $mAttribute);
        } else {
            $mAttribute = array($mAttribute, '');
        }
$message .= '
    <td colspan="2">hello'.$mAttribute[0].$mAttribute[1].$mAttribute[2].'</td>
</tr>';

有人可以告诉我上面要更改的内容,以便我可以在$ message之后的方式中选择阵列吗?

1 个答案:

答案 0 :(得分:0)

您的代码已修复:

$message ='';
if(isset($itemConversations) && !empty($itemConversations)){
    $mAttributes = array();
    if(strpos($itemConversations, "###") !== false){
        $mAttributes = explode("###", $itemConversations);
    } else {
        $mAttributes[] = $itemConversations;
    }
    foreach($mAttributes as &$mAttribute){
        if(strpos($mAttribute, ":::") !== false){
            $mAttribute = explode(":::", $mAttribute);
        } else {
            $mAttribute = array($mAttribute, '');
        }
        $message .= '<tr>
                        <td colspan="2">hello'.$mAttribute[0].$mAttribute[1].$mAttribute[2].'</td>
                    </tr>';
    }
}
相关问题