1

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);
2
  • Then u should rebuild the structure of your array Commented Dec 26, 2013 at 12:34
  • You are making the array wrong, show us you code for array creation Commented Dec 26, 2013 at 12:34

6 Answers 6

2

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]
)
1

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'
0

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'];
0

While creating that array build it with index as BusinessName

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

etc..

0

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');
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.