使用键值读取PHP数组

时间:2013-12-26 12:30:24

标签: php arrays

我有一个我在PHP脚本中创建的数组,var_dump函数为我提供了这个值:var_dump($arrayOfValues);

array(3) { 
    [0]=> array(2) { 
        [0]=> string(12) "BusinessName" 
        [1]=> string(13) "ITCompany" 
    } 
    [1]=> array(2) {  
        [0]=> string(7) "Address"  
        [1]=> string(58) "29 xxxxxx,Canada"  
    }  
    [2]=> array(2) {  
        [0]=> string(20) "PrimaryBusinessPhone"  
        [1]=> string(14) "(444) 111-1111"  
    }  
    [3]=> array(2) {  
        [0]=> string(13) "BusinessEmail"  
        [1]=> string(24) "xx@example.com" 
    }
} 

我想使用键而不是索引来访问“BusinessName”的值,所以如果我放了:echo $arrayOfValue[0][1];它给了我的商业名称是ITCompany但是如果我把 :echo $arrayOfValue['BusinessName'][1];它什么都没有,所以我该如何解决呢?

数组初始化为$arrayOfValue = array();,然后在for循环中动态填充,如

$arrayOfValue[] = array($Label, $Value);

6 个答案:

答案 0 :(得分:2)

你的数组有这种数据

$arrayOfPostsValue[] = array("BusinessName","ITCompany");
$arrayOfPostsValue[] = array("Address","29 xxxxxx,Canada");
$arrayOfPostsValue[] = array("PrimaryBusinessPhone","(444) 111-1111");
$arrayOfPostsValue[] = array("BusinessEmail","xx@example.com");

数据中没有数组键所以,你必须重新创建你想要的数组

$newArrayOfPostsValue = array();

foreach ( $arrayOfPostsValue as $value ){
    $newArrayOfPostsValue[$value[0]] = $value[1];
}

print_r($newArrayOfPostsValue);

,这是输出

Array
(
    [BusinessName] => ITCompany
    [Address] => 29 xxxxxx,Canada
    [PrimaryBusinessPhone] => (444) 111-1111
    [BusinessEmail] => xx@example.com
)

答案 1 :(得分:1)

如评论中所述,更改数组的结构,将更容易处理

   $my_array = array(
      array('Business Name' => 'It Company'), 
      array('Address' => 'My address')
   ); 

查看数组的内容,我将其重组为

$my_improved_array = array(
  'BusinessName' => 'It Company', 
  'Address' => 'My Address',
); 

这是您可以访问的方式,

echo $my_array[0]['BusinessName'] //prints 'It Company'
echo $my_array[1]['Address'] //prints 'My Address'

echo $my_improved_array['BusinessName'] // prints 'It Company'

答案 2 :(得分:0)

尝试这样并将数组保存为键值对,然后访问键:

 foreach($arrayOfValues as $a)
 $arr[$a[0]] = $a[1];

 echo $arr['BusinessName'];

答案 3 :(得分:0)

创建该数组时,使用索引作为BusinessName

构建它
array ( 
    0 => array( 
        'BusinessName'=>  "ITCompany" 
    ) 
    1 => array(1) (  
        'Address'=> "29 xxxxxx,Canada"   
    )  
  )

等。

答案 4 :(得分:0)

PHP数组示例

$array = array();
$array['BusinessName'] = 'ITCompany';
$array['Address'] = '29 xxxxxx,Canada';

等等......现在你可以用

回显值了
echo $array['BusinessName'];

这也有效

$array = array('BusinessName' => 'ITCompany', 'Address' => '29 xxxxxx,Canada');

答案 5 :(得分:0)

首先,你是如何构建这个数组的?也许你可以直接制作一个可行的阵列。

无论如何,你可以这样做:

$oldArray = Array(
    Array("BusinessName", "ITCompany"),
    Array("Address", "29 xxxxxx,Canada"),
    Array("PrimaryBusinessPhone", "(444) 111-1111"), 
    Array("BusinessEmail", "xx@example.com")
);

$newArray = Array();
foreach($oldArray as value) {
    $newValue[$value[0]] = $value[1];
}

/* Now, it's equal to
$newArray = Array(
    "BusinessName"         => "ITCompany",
    "Address"              => "29 xxxxxx,Canada",
    "PrimaryBusinessPhone" => "(444) 111-1111",  
    "BusinessEmail"        => "xx@example.com"
);
*/