2013-10-29 101 views
0

我全功能PHP:如何獲取值的鍵在陣列中的陣列

function jsonData(){ 
      //$data = $this->DataSet->query("SELECT * FROM data_sets;"); 
      $data = $this->DataSet->find('all', array(
      'fields' => array('created','systolisch', 'diastolisch', 'gewicht', 'puls'), 
    )); 
      $columns = array(); 
      $values = array(); 

      $count = count($data); 

      for($i=0; $i < $count; $i++) 
       { 
       $keys[] = array_values($data[$i]); 
       $values [] = array_values(array_keys($data[$i])); 
       } 



      $array = array(
       $keys, 
       $values, 

       ); 

      return new CakeResponse(array('body' => json_encode($array), 'type' => 'json')); 
    } 

這裏的情況是:

for($i=0; $i < $count; $i++) 
       { 
       $keys[] = array_values($data[$i]); 
       $values [] = array_values(array_keys($data[$i])); 
       } 

這將返回:

[[[{"created":"2013-10-29 14:16:38","systolisch":"77","diastolisch":"83","gewicht":"77","puls":"77"}],[{"created":"2013-10-29 14:52:00","systolisch":"99","diastolisch":"88","gewicht":"80","puls":"100"}]],[["DataSet"],["DataSet"]]] 

我需要什麼是array_values(array(keys)lile diastolisch和systolisch結果的關鍵字,值也應該呈現爲普通值而不是對。 我怎麼能做到這一點?

回答

1

您可以使用array_keys。這是從PHP文檔:

<?php 
$array = array(0 => 100, "color" => "red"); 
print_r(array_keys($array)); 

$array = array("blue", "red", "green", "blue", "blue"); 
print_r(array_keys($array, "blue")); 

$array = array("color" => array("blue", "red", "green"), 
      "size" => array("small", "medium", "large")); 
print_r(array_keys($array)); 
?> 

這將輸出:

Array 
(
[0] => 0 
[1] => color 
) 

Array 
(
[0] => 0 
[1] => 3 
[2] => 4 
) 
Array 
(
[0] => color 
[1] => size 
) 
+0

,你可以即時通訊已經在使用這一看,如果沒有適當的結果 – thegrunt

+0

+1參與 – thegrunt