2011-02-02 83 views
0

我有一个看起来像下面的数组。检查尴尬的阵列

$ => Array (2) 
(
| ['0'] => Array (2) 
| (
| | ['0'] = String(1) "2" 
| | ['1'] = String(1) "2" 
| ) 
| ['1'] => Array (2) 
| (
| | ['0'] = String(1) "2" 
| | ['1'] = String(1) "1" 
| ) 
) 

但也可以更大或更小只有一个数组。

每个数组表示从数据库返回的一行结果。

第一个字段[0] [0]是将要被需要的ID号码。

[0] [1]是我需要检查的值。

我需要知道它是否存在,说我是否得到2或1或者我没有。

如果我没有,那么我需要发送ID([0] [0])到另一个功能。

有时我可能会得到更多或更少的结果。所以这需要使用循环来完成,但我正在努力做到这一点,每次我想我有一些代码可以工作,但它不会。

任何人都可以帮忙吗?

编辑:

这是我走到这一步......

$tweet_sentiment = array(); 
$analyzer = array(); 
foreach($get_sentiment as $sentiment) { 
    $tweet_id = $sentiment[0]; 
    $analyzer[] = $sentiment[1]; 
    $tweet_sentiment[$tweet_id] = $analyzer; 
} 

这改变了这样的阵列考虑以下几点:

$ => Array (1) 
(
| ['2'] => Array (2) 
| (
| | ['0'] = String(1) "2" 
| | ['1'] = String(1) "1" 
| ) 
) 
+0

“我没有”的条件是什么?你没有? – Jonah 2011-02-02 00:48:14

+0

你可以发布你到目前为止的代码吗? – 2011-02-02 00:48:35

回答

0

我结束了改变一些内部的SQL语句,以便从数据库中我的结果是不同的。

我发现我使用的SQL语句和返回的数组过于复杂,无法按照我想要的方式进行处理 - 这主要是由于SQL查询结果的格式。

0

也许你可以做像这样检查:

array_key_exists(1, $x[1]) ? $x[1][1] : otherFunction($x[0][0])

其中$ x是您的数组。

1

这就是我理解你的问题。

第一个循环遍历主数组,并在其index => array2上工作。

第二个循环遍历第二个数组,并检查该数组的值是否为“value1”。如果是,则执行doWhenValueIsThere(),否则执行doWhenValueIsNotThere()

您必须根据您的需要创建两个功能。

foreach ($array1 as $index => $array2) 
    { 
    foreach (array_keys($array2) as $id) 
    { 
     if ($array2[$id] == "value1") doWhenValueIsThere(); 
     else doWhenValueIsNotThere(); 
    } 
    } 
0

试试这个

<?php 
$tweet_sentiment = array(); 
$analyzer = array(); 
foreach($get_sentiment as $sentiment) { 
    $tweet_id = $sentiment[0]; 
    if(isset($sentiment[1])){ 
     $analyzer[] = $sentiment[1]; 
     $tweet_sentiment[$tweet_id] = $analyzer; 
    } 
} 
?>