2011-03-02 33 views
2

这里是我的数组vardump:引用在数组的索引元素 - PHP

array(4) { 
     [3]=> array(1) 
     { 
      ["match"]=> int(33) 
     } 
     [2]=> array(1) 
     { 
      ["match"]=> int(32) 
     } 
     [1]=> array(1) 
     { 
      ["match"]=> int(16) 
     } 
     [4]=> array(1) 
     { 
      ["match"]=> int(3) 
     } 
} 

我需要返回索引3,2,1,和4用在查询。我不知道如何做到这一点。我需要在foreach语句运行查询:

foreach($arrayName as $key){ 
    //NEED TO RETURN INDEX HERE  
} 

我试着使用key($key)但返回这是一个级别的下面,我需要在那里索引字“匹配”。

任何帮助将不胜感激。

+0

实际上,您正在使用错误语义的foreach。它应该像** foreach($ arrayName为$ value)**如果您使用两个操作数。 – vbence 2011-03-02 15:25:38

+0

ahh semantics shmemantics;) – 2011-03-02 15:37:01

回答

6

轻松一)

$keys = array_keys($arrayName); 
+0

避免在循环中运行查询 - 您应该能够使用类似IN('。implode(',',$ keys)')的方式为您制定一个查询。 – 2011-03-02 15:19:01

+0

嗯,我认为这会比这更复杂。这是我第一次进入大型阵列,为帮助而欢呼 – 2011-03-02 15:19:21

7
foreach($arrayName as $key => $value){ 
    echo($key);  
} 
3

只需将密钥添加到在foreach:

foreach($arrayName as $key => $value){ 
     echo $key; //$key is well... the key and $value is the value of the current element in the array :) 
} 
4
foreach($arrayName as $key) 

$key实际上是在阵列中的值。尝试:

foreach($arrayName as $key=>$value)