如何从这个数组中获取元素

时间:2014-10-26 13:50:27

标签: php arrays

我想从数组中获取数据。我在该数组中有两个子数组。一个位于[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] =>bisal@gjjil.com      
                [2]=> 845678956
            )

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

    )

2 个答案:

答案 0 :(得分:0)

您可以像这样访问关联数组:

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

答案 1 :(得分: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