Jump to content

Recommended Posts

Hi Guys

 

Clearly I still have lots to learn...

 

I am trying to insert (using mysqli) multi-dimensional array values into the database, but when i do the value that gets stored  ends up as "$Array[1][1]".

 

Here's the test query i am running:

 

$sql = "INSERT INTO teams (company) VALUES ('$myarray[1][1]')";
$mysqli->query($sql);

 

So do i need to break the data out of the multidimensional array for the insert? Or is there some syntax thing i've missed?

 

Or worse still do i actually need to do:

 

$value1 = $myarray[0][0]; 

 

 

 

Any help would be very much appreciated!

 

Drongo

That worked a treat! Thank you.

 

Err... mind explaining to me why you wrapped it in double quotes and then concatenated?

 

Is this some way of make it interpret the next level down in the array?

 

 

 

 

This should work..

 

   $sql = "INSERT INTO teams (company) VALUES ('".$myarray[1][1]."')";
   $mysqli->query($sql);

 

 

 

To be honest I don't know the actual reason. You can place a single postion into a "" string:

 

 

$a[] = 'ss';
echo <<<_END
$a[0]
_END;

 

 

But when you chuck in another dimension with the extra set of square brackets, php trips up.

 

 

So as your query in $sql is essentailly a string, by breaking the string and joining it to the array pos, then joining the rest of the query to the end the $sql... $sql then becomes the string you wanted it to be in the first place.

 

 

However i'm pretty sure there is a better explanation.. and I would actually like to know it too... i notice you have Adam reading this thread.. hopefully he will be able to give a better explanation to the one i gave :)

 

 

(you can actually write less code there by placing the query directly into the mysqli:

 

 

 

   $mysqli->query( "INSERT INTO teams (company) VALUES ('".$myarray[1][1]."')" );

 

 

Well that sounds like a pretty decent explanation to me :)

 

I was more concerned there was some fundamental syntax thing i had completely overlooked.

 

It's good to know how to make it work though so thank you very much!

 

 

 

 

 

 

To be honest I don't know the actual reason. You can place a single postion into a "" string:

 

 

$a[] = 'ss';
echo <<<_END
$a[0]
_END;

 

 

But when you chuck in another dimension with the extra set of square brackets, php trips up.

 

 

So as your query in $sql is essentailly a string, by breaking the string and joining it to the array pos, then joining the rest of the query to the end the $sql... $sql then becomes the string you wanted it to be in the first place.

 

 

However i'm pretty sure there is a better explanation.. and I would actually like to know it too... i notice you have Adam reading this thread.. hopefully he will be able to give a better explanation to the one i gave :)

 

 

(you can actually write less code there by placing the query directly into the mysqli:

 

 

 

   $mysqli->query( "INSERT INTO teams (company) VALUES ('".$myarray[1][1]."')" );

Err... mind explaining to me why you wrapped it in double quotes and then concatenated?

 

That is actually a very good question, I am tempted to say it has something to do with greedy token, but that wouldn't make any sense. If anyone knows please share!

 

But alternatively you can use braces like so (it's more readable in my opinion):

"INSERT INTO teams (company) VALUES ('{$myArray[0][3]}')"

 

Or you can just asign the array value before hand and insert a regular string like so:

$myValue = $myArray[0][2];
"INSERT INTO teams (company) VALUES ('$myValue')"

 

Ahhh....i was trying to use braces but i wasn't wrapping the whole variable.  haha that makes me wonder about something else...

 

Do curly braces tell php to interpret that part first?

 

 

 

Err... mind explaining to me why you wrapped it in double quotes and then concatenated?

 

That is actually a very good question, I am tempted to say it has something to do with greedy token, but that wouldn't make any sense. If anyone knows please share!

 

But alternatively you can use braces like so (it's more readable in my opinion):

"INSERT INTO 2012hitting (hr) VALUES ('{$myArray[0][3]}')"

 

Or you can just asign the array value before hand and insert a regular string like so:

$myValue = $myArray[0][2];
$sql = "INSERT INTO 2012hitting (hr) VALUES ('$myValue')";

When you're dealing with a multidimensional array, and need to use one of the indices deeper than the first dimension in a quoted string (like a query string), it seems that you need to use complex notation, with the curly braces, or concatenate it into the string. Otherwise, you can only reference elements in the first dimension of the array. I took a quick look in the manual, but couldn't find any explanation, but that doesn't mean it isn't there somewhere.

 

Oh yea :) you can use curly's too... I don't know which is preffered by the guru's... and also i don't know which is more optimum.. I just prefer to break and join as I find it easier to read, loads of curlies makes it look like some old english text from a dickens novel in my opinion haha

well i feel like i've learned something today :)

 

I think i'm down with the curly braces club freelancer...seems easier to read to me. So you're out there on concatenation island on your own i'm afraid hehe.

 

Thanks for all that chaps. Has helped a lot!

Do curly braces tell php to interpret that part first?

 

Lol, I guess some one will just have to call up Rasmus Lerdorf for a full explanation. But also be aware of the use of curly braces in variables in order to separate it for from the rest of the text. For instance:

 

$dona = "connec";
$dona = "educa";
$dona = "presenta";
$dona = "evolu"

// normally you would do:
$myVar = 'The word' . $dona . 'tion could have a different beginning but always ends the same way.';

// but alternatively you can do (greedy token parsing):
$myVar = "The word {dona}tion could have a different beginning but always ends the same way.";

So in order to separate $dona from "tion" you use the curly braces, thus avoiding php from reading the string as $donation. Figured I would point this out for informational purposes considering it is probably closely related to the use of concatenation and curly braces in the array.

Cheers marc that;s good to know.  I can see keeping curly braces in mind can probably come in very handy!

 

 

Do curly braces tell php to interpret that part first?

 

Lol, I guess some one will just have to call up Rasmus Lerdorf for a full explanation. But also be aware of the use of curly braces in variables in order to separate it for from the rest of the text. For instance:

 

$begin = "connec";
$dona = "educa";
$dona = "presenta";
$dona = "evolu"

// normally you would do:
$myVar = 'The word' . $dona . 'tion could have a different beginning but always ends the same way.';

// but alternatively you can do (greedy token parsing):
$myVar = "The word {dona}tion could have a different beginning but always ends the same way.";

So in order to separate $dona from "tion" you use the curly braces, thus avoiding php from reading the string as $donation. Figured I would point this out for informational purposes considering it is probably closely related to the use of concatenation and curly braces in the array.

String Parsing

If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name.

 

[ and ] are not considered valid variable name characters, so PHP stop at that point.  It make a special case though where if the next character is a [, it will properly interpret it.  This only works for one level though.  For multiple levels, you have to explicitly tell PHP what the variable name is using the complex notation.

 

This is also why PHP cannot handle quoted keys (such as $arr['hi']) in a string without the complex notation.  After the [ it scans for more valid variable characters and stop at first invalid one.  ' or " are not valid so you end up requesting the variable $arr[ which causes PHP to error.

 

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.