如何从关联数组中获取值

时间:2014-10-06 07:35:42

标签: php associative-array

这是我的阵列:

Array
    (
        [0] => Array
        (
            [0] => S No.
            [1] => Contact Message
            [2] => Name
            [3] => Contact Number
            [4] => Email ID
        )

        [1] => Array
        (
            [0] => 1
            [1] => I am interested in your property. Please get in touch with me.
            [2] =>  lopa  <br/>(Individual)
            [3] => 1234567890
            [4] => <a href="mailto:loperea.ray@Gmail.com">loperea.ray@Gmail.com</a>
        )

        [2] => Array
        (
            [0] => 2
            [1] => This user is looking for 3 BHK Multistorey Apartment for Sale in Sohna, Gurgaon and has viewed your contact details.
            [2] =>  shiva  <br/>(Individual)
            [3] => 2135467890
            [4] => <a href="mailto:sauron82@yahoo.co.in">sauron82@yahoo.co.in</a>
        )
)

如何检索所有数据元素?

4 个答案:

答案 0 :(得分:1)

您可以在the official PHP doc page

上获取有关PHP中数组的信息

您可以使用围绕您要选择的键的方括号来访问数组[key]

所以$array[1]会给yoo:

Array
(
    [0] => 1
    [1] => I am interested in your property. Please get in touch with me.
    [2] =>  lopa  <br/>(Individual)
    [3] => 1234567890
    [4] => <a href="mailto:loperea.ray@Gmail.com">loperea.ray@Gmail.com</a>
)

$array[1][2]会给你:

lopa  <br/>(Individual)

或者您可以使用foreachfor循环等循环遍历数组元素。

// perfect for assoc arrays
foreach($array as $key => $element) {
    var_dump($key, $element);
}

// alternative for arrays with seamless numeric keys
$elementsCount = count($array);

for($i = 0; $i < $elementsCount; ++$i) {
    var_dump($array[$i]);
}

答案 1 :(得分:0)

您在多维数组中有整数索引元素。要从数组中访问单个元素,请使用数组名称及其索引$myArray[1]。要获取前一个选定数组的内部元素,请使用第二组[index] - $myArray[1][5],依此类推。

要动态获取数组中的所有元素,请使用嵌套的foreach循环:

foreach ($myArray as $key => $values) {
   foreach ($values as $innerKey => $value) {
       echo $value;
       // OR
       echo $myArray[$key][$innerKey];
   }
}

答案 2 :(得分:0)

解决方案是使用array_reduce

$header = array_map(
  function() { return []; }, 
  array_flip( array_shift( $array ) ) 
); // headers

array_reduce( $array , function ($carry, $item) {
  $i = 0;
  foreach( $carry as $k => $v ) {
    $carry[$k][] = $item[$i++];
  }
  return $carry;
}, $header );

首先,我们从输入数组的第一个元素中获取标题。然后我们映射 - 减少输入。

这给出了:

$array = [['A', 'B', 'C'], ['a1', 'b1', 'c1'], ['a2', 'b2', 'c2'], ['a3', 'b3', 'c3']];
/*
  array(3) {
    'A' =>
    array(3) {
        [0] =>
        string(2) "a1"
        [1] =>
        string(2) "a2"
        [2] =>
        string(2) "a3"
      }
    'B' =>
    array(3) {
        [0] =>
        string(2) "b1"
        [1] =>
        string(2) "b2"
        [2] =>
        string(2) "b3"
      }
    'C' =>
    array(3) {
        [0] =>
        string(2) "c1"
        [1] =>
        string(2) "c2"
        [2] =>
        string(2) "c3"
      }
  }
*/

答案 3 :(得分:0)

我认为这就是你要找的东西

$array = Array
(
        0=> Array
        (
                0 => 'S No.',
                1 => 'Contact Message',
                2 => 'Name',
                3 => 'Contact Number',
                4 => 'Email ID'
        ),

        1 => Array
        (
                0 => 1,
                1 => 'I am interested in your property. Please get in touch with me.',
                2 =>  'lopa  <br/>(Individual)',
                3 => '1234567890',
                4 => '<a href="mailto:loperea.ray@Gmail.com">loperea.ray@Gmail.com</a>',
        ),

        2 => Array
        (
                0 => 2,
                1 => 'This user is looking for 3 BHK Multistorey Apartment for Sale in Sohna, Gurgaon and has viewed your contact details.',
                2 =>  'shiva  <br/>(Individual)',
                3 => '2135467890',
                4 => '<a href="mailto:sauron82@yahoo.co.in">sauron82@yahoo.co.in</a>',
        )
);

$result_array = array();
array_shift($array);
reset($array);

foreach($array as $x=>$array2){
    foreach($array2 as $i => $arr){     
        if($i == 1){
            $result_array[$x]['Contact Message'] = $arr;
        }elseif($i == 2){           
            $result_array[$x]['Name'] = $arr;
        }elseif($i == 3){           
            $result_array[$x]['Contact Number']  =$arr;
        }elseif($i == 4){           
            $result_array[$x]['Email ID'] = $arr;
        }
    }   
}

print_r($result_array);