匹配单独的数组值和&打印结果

时间:2013-01-29 20:54:40

标签: php arrays codeigniter

// Setup Query
$query = $this->db->query('SELECT * FROM here');

// Pull in the appropriate data for the model
$toolkitName = $this->toolkits_model->find_by('id', $id);

// Strip the commas from incoming array
$matchMeCommas = $toolkitName->toolkits_listitems; 
$matchMe = explode(',', $matchMeCommas);    

// Print ID for each item in toolkit parent list
foreach ($query->result_array() as $row) :
echo $row['id'];
endforeach;

// Match each item from toolkit list item
foreach ($matchMe as $row2) :
echo $row2;
endforeach;

我要做的是匹配这两个数组的值并打印父列表项的结果。目前发生的是我得到两个与“ID(2,3,4,5,等)”相关的“234567891011”字符串。

我应该补充一点,我希望匹配相似的值并为特定ID提取结果。所以,如果值为2& 4匹配我想从数据库中检索他们的信息并打印出来。

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:2)

仍然不确定你想要什么,但这可以让你开始:

foreach($query->result_array() as $row){
    if(in_array($row['id'], $matchMe)){
        //this is a match ... do something
    }
}

OR

您可以构建所有匹配的数组:

//get the ids from the query
$ids = array();
foreach($query->result_array() as $row){
    $ids[] = $row['id'];
}

$matches = array_intersect($matchMe, $ids);