OussamaLord
OussamaLord

Reputation: 1095

Reading PHP array using key value

I have an array that I'm creating inside my PHP script, the var_dump function gives me this value :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) "[email protected]" 
    }
} 

I would like to access to the value of the "BusinessName" using key and not index so if I put : echo $arrayOfValue[0][1]; it gives me the BusinessName that is ITCompany but if I put : echo $arrayOfValue['BusinessName'][1]; it gives nothing so how can I fix that please?

The array is initialized $arrayOfValue = array(); and then populated dynamically inside a for loop like that

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

Upvotes: 1

Views: 111

Answers (6)

Sebastien C.
Sebastien C.

Reputation: 4833

First of all, how are you building this array ? Maybe you can directly make a workable array.

Anyway, you can do something like this :

$oldArray = Array(
    Array("BusinessName", "ITCompany"),
    Array("Address", "29 xxxxxx,Canada"),
    Array("PrimaryBusinessPhone", "(444) 111-1111"), 
    Array("BusinessEmail", "[email protected]")
);

$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"        => "[email protected]"
);
*/

Upvotes: 0

Nono
Nono

Reputation: 7302

your array has this kind of data

$arrayOfPostsValue[] = array("BusinessName","ITCompany");
$arrayOfPostsValue[] = array("Address","29 xxxxxx,Canada");
$arrayOfPostsValue[] = array("PrimaryBusinessPhone","(444) 111-1111");
$arrayOfPostsValue[] = array("BusinessEmail","[email protected]");

there is no array key in data So, you have to recreate your desire array

$newArrayOfPostsValue = array();

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

print_r($newArrayOfPostsValue);

and here is output

Array
(
    [BusinessName] => ITCompany
    [Address] => 29 xxxxxx,Canada
    [PrimaryBusinessPhone] => (444) 111-1111
    [BusinessEmail] => [email protected]
)

Upvotes: 2

prabeen giri
prabeen giri

Reputation: 803

As mentioned in the comment, change the structure of the array, it will be much easier to handle

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

Looking at the content of your array, I will restructure it as

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

This is how you can access,

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

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

Upvotes: 1

Jompper
Jompper

Reputation: 1402

PHP Array example

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

And so on... Now you can echo values with

echo $array['BusinessName'];

Also this works

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

Upvotes: 0

KA.
KA.

Reputation: 4504

While creating that array build it with index as BusinessName

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

etc..

Upvotes: 0

Aditya
Aditya

Reputation: 4463

Try like this and save array as key value pairs and then access the key:

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

 echo $arr['BusinessName'];

Upvotes: 0

Related Questions