Jump to content

PHP Arrays


AdamFerguson
Go to solution Solved by AdamFerguson,

Recommended Posts

Hi,

 

Can someone please help. I have an array such as:

 

 

$this->cpc[$key1][$key2]['shiptype'] = $value;

 

 

But when I try to print the value the array is empty:

 

 

echo $this->cpc[$key1][$key2]['shiptype'];

 

 

But, if I don't use variable names such as $key1 and $key2 and instead use the values of $key1 and $key2, it works. Such as: (these two lines DO set the value in the array and will print it to the screen)

 

 

$this->cpc[202-3454-234234][9685766]['shiptype'] = $value;

echo $this->cpc[202-3454-234234][9685766]['shiptype'];

 

 

The $key1 and $key2 DEFINITELY hold the correct values so what is going on here please? Does anyone know?

 

 

Best Regards, Adam

 

Link to comment
Share on other sites

Hi,

 

The code:

		$key1 = $shipping['orderid'][0];
		$key2 = $dbitem['ProductCode'];
		
		// store the shipping details for the order
		$this->cpc[$key1][$key2]['shiptype'] 			= $shipping['shiptype'][0];
		$this->cpc[$key1][$key2]['marketplace'] 		= $shipping['marketplace'][0];
		$this->cpc[$key1][$key2]['ship_name'] 			= $shipping['ship_name'][0];
		$this->cpc[$key1][$key2]['ship_address1'] 		= $shipping['ship_address1'][0];
		$this->cpc[$key1][$key2]['ship_address2'] 		= $shipping['ship_address2'][0];
		$this->cpc[$key1][$key2]['ship_city'] 			= $shipping['ship_city'][0];
		$this->cpc[$key1][$key2]['ship_state'] 			= $shipping['ship_state'][0];
		$this->cpc[$key1][$key2]['ship_postcode'] 		= $shipping['ship_postcode'][0];
		$this->cpc[$key1][$key2]['ship_countrycode'] 	= $shipping['ship_countrycode'][0];
		$this->cpc[$key1][$key2]['ship_phone'] 			= $shipping['ship_phone'][0];

if you print any of these individually (except any that being with $this->cpc) the value is printed to the screen so I know that everything has a value. Why then, would $this->cpc array not hold any of these values?

 

$key1, $key2, all the $shipping variables all have values when printed. But none are assigned to the $this->cpc array.

Link to comment
Share on other sites

  • Solution

Hi,

 

I figured it out, the array was treating $key1 and $key2 as integers and therefore $key1 (which was 202-7465746-2875675) was not being treated as a string as I had hoped. Just wrapped them like this in double quotes:

 

$this->cpc["{$key1}"]["{$key2}"]['ship_phone']

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.