2014-10-26 90 views
-2

我想從數組中獲取數據。在該數組中有兩個子數組。一個位於[0],另一個位於['header_data']。最初我能夠提取[0]下標元素,但不知道如何獲得['header_data']元素。下面的代碼給出:如何從這個數組中獲取元素

foreach ($result as $index) { 
     $resultArr[] = explode('  ', $index[0][0]); 
    } 

    //$valuesArr = array(); 
    foreach ($resultArr as $value) { 

     $arrayVal = array("lead_source" => "ABC", 
      "name" => trim(strip_tags(str_replace(array('Name : ', '(Dealer)'), '', $value[0]))), 
      "emailId" => trim(strip_tags(str_replace(array('Email : ', 'Verified'), '', $value[1]))), 
      "contactNo" => trim(strip_tags(str_replace(array('Phone number : ', '  Verified'), '', $value[2]))), 
      "project_name" => " ", 
      "customer_query" => "", 
      "description" => "", 
      "locality" => "", 
      "budget" => "", 
      "date" => $index['header_data']['date'], 
      "subject" => $index['header_data']['subject'] 
     ); 

輸出是在這裏:

Array 
    (
     [0] => Array 
      (
       [0] => Kishor 
       [1] =>[email protected]  
       [2]=> 845678956 
      ) 

     [header_data] => Array 
      (
       [date] => Sun, 5 Oct 2014 15:10:17 +0580 
       [subject] => Advertisement Response for Dealer profile 
      ) 

    ) 
+2

嗯,'$ arr ['header_data'] ['date']'? – 2014-10-26 13:55:21

回答

0

您可以訪問關聯數組是這樣的:

$arr['header_data']['date'] 
$arr['header_data']['subject'] 
0

從一個關聯數組,你有一個名爲訪問數據鑰匙:

$array = array('peach' => 'melba', 'banana' => 'split'); 

echo $array['peach'] . PHP_EOL; 
echo $array['banana'] . PHP_EOL; 

輸出:

melba 
split 

要訪問一個嵌套關聯數組數據:

$nest = array('peach' => 'melba', 'chocolate' => array('chip' => 'cookies', 'milk' => 'shake')); 

echo $nest['chocolate']['chip'] . PHP_EOL; 
echo $nest['chocolate']['milk'] . PHP_EOL; 

輸出:

cookies 
shake 

迭代通過陣列:

foreach ($nest['chocolate'] as $k => $v) { 
    echo "key: $k, value: $v" . PHP_EOL; 
} 

輸出:

key: chip, value: cookies 
key: milk, value: shake 
相關問題