简体   繁体   English

如何从关联数组中获取价值

[英]how to get value from associative array

This is my 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>
        )
)

How can I retrieve all data element wise? 如何明智地检索所有数据元素?

You can get information about arrays in PHP on the official PHP doc page 您可以在官方PHP doc页面上获取有关PHP中数组的信息。

You can access arrays using square braces surrounding the key you like to select [key] . 您可以使用方括号括住要选择[key]的键来访问数组。

So $array[1] will give yoo: 因此$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>
)

And $array[1][2] will give you: $array[1][2]将为您提供:

lopa  <br/>(Individual)

Or you can walkt through the elements of an array using loops like the foreach or the for loop. 或者,您可以使用诸如foreachfor循环之类的循环foreach数组的元素。

// 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]);
}

You have integer indexed elements in multidimensional array. 您在多维数组中具有整数索引元素。 To access single element from array, use array name and it's index $myArray[1] . 要从数组访问单个元素,请使用数组名称及其索引$myArray[1] To get inner element of that previous selected array, use second set of [index] - $myArray[1][5] and so on. 要获取先前选择的数组的内部元素,请使用第二组[index] - $myArray[1][5] ,依此类推。

To dynamically get all elements from array, use nested foreach loop: 要从数组中动态获取所有元素,请使用嵌套的foreach循环:

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

The solution is to use array_reduce : 解决方案是使用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 );

First of all we get the header from the very first element of input array. 首先,我们从输入数组的第一个元素获取标头。 Then we map-reduce the input. 然后我们映射减少输入。

That gives: 这给出了:

$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"
      }
  }
*/

I think this is what you are looking for 我想这就是你要找的

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM