Using curly brackets in multidimensional array
I have an array of ID's used to execute mysqli queries in a foreach loop (the below code is just an example - the array will contain more than 50+ elements/ID's). For each array element/ID I query an array which I then include in a compData array. However, I am unsure of how to dynamically define the arrays in the foreach loop. I would very much appreciate if you can check whether I am using ${$val} correctly in the below or if something else is wrong. Thanks.
$idArray = array(
"List1",
"List2",
);
$compData = array();
foreach($idArray as $val) {
$sth = mysqli_query($conn, "SELECT * FROM {$tableName} WHERE ID = {$val}");
${$val} = array();
while($r = mysqli_fetch_array($sth)) {
For ($n = 1; $n <= $CI_NOYEARS; $n++){
${$val}['data'] = $r[$n];
}
}
$compData[{$val}] = ${$val}
}
foreach($compData['List1'] as $result) {
For ($n = 0; $n <= $CI_NOYEARS; $n++){
echo $result[$n];
}
}
The above code does not work and does not echo any data from the $compData array. Below is an example that works just fine where I execute the code using the ID's directly.
$sth = mysqli_query($conn, "SELECT * FROM {$tableName} WHERE ID = 'List1'");
$List1 = array();
while($r = mysqli_fetch_array($sth)) {
For ($n = 1; $n <= $CI_NOYEARS; $n++){
$List1['data'] = $r[$n];
}
}
$companyData = array();
$companyData['List1'] = $List1;
foreach($compData['List1'] as $result) {
For ($n = 0; $n <= $CI_NOYEARS; $n++){
echo $result[$n];
}
}
php arrays mysqli
|
show 4 more comments
I have an array of ID's used to execute mysqli queries in a foreach loop (the below code is just an example - the array will contain more than 50+ elements/ID's). For each array element/ID I query an array which I then include in a compData array. However, I am unsure of how to dynamically define the arrays in the foreach loop. I would very much appreciate if you can check whether I am using ${$val} correctly in the below or if something else is wrong. Thanks.
$idArray = array(
"List1",
"List2",
);
$compData = array();
foreach($idArray as $val) {
$sth = mysqli_query($conn, "SELECT * FROM {$tableName} WHERE ID = {$val}");
${$val} = array();
while($r = mysqli_fetch_array($sth)) {
For ($n = 1; $n <= $CI_NOYEARS; $n++){
${$val}['data'] = $r[$n];
}
}
$compData[{$val}] = ${$val}
}
foreach($compData['List1'] as $result) {
For ($n = 0; $n <= $CI_NOYEARS; $n++){
echo $result[$n];
}
}
The above code does not work and does not echo any data from the $compData array. Below is an example that works just fine where I execute the code using the ID's directly.
$sth = mysqli_query($conn, "SELECT * FROM {$tableName} WHERE ID = 'List1'");
$List1 = array();
while($r = mysqli_fetch_array($sth)) {
For ($n = 1; $n <= $CI_NOYEARS; $n++){
$List1['data'] = $r[$n];
}
}
$companyData = array();
$companyData['List1'] = $List1;
foreach($compData['List1'] as $result) {
For ($n = 0; $n <= $CI_NOYEARS; $n++){
echo $result[$n];
}
}
php arrays mysqli
There is rarely any use for variable variables that can't be solved with an associative array.
– miken32
Nov 15 '18 at 21:32
What exactly is the problem you're having?
– miken32
Nov 15 '18 at 21:33
My problem is that the data is not being added to the $compData array. The echo does not return anything. I am fairly sure that there is an error in the $compData[{$val}] = ${$val . "Array"} line.
– ChartProblems
Nov 15 '18 at 21:37
And I'm pretty sure yourecho
doesn't return anything because you're burying your data many levels deep. Your loops at the end there don't account for the fact that you've put things under thedata
element of the array, for example.
– miken32
Nov 15 '18 at 21:44
I have tried to add more explanation and included a piece of code that works as intended. Hopefully this will clarify what the issue is?
– ChartProblems
Nov 15 '18 at 21:46
|
show 4 more comments
I have an array of ID's used to execute mysqli queries in a foreach loop (the below code is just an example - the array will contain more than 50+ elements/ID's). For each array element/ID I query an array which I then include in a compData array. However, I am unsure of how to dynamically define the arrays in the foreach loop. I would very much appreciate if you can check whether I am using ${$val} correctly in the below or if something else is wrong. Thanks.
$idArray = array(
"List1",
"List2",
);
$compData = array();
foreach($idArray as $val) {
$sth = mysqli_query($conn, "SELECT * FROM {$tableName} WHERE ID = {$val}");
${$val} = array();
while($r = mysqli_fetch_array($sth)) {
For ($n = 1; $n <= $CI_NOYEARS; $n++){
${$val}['data'] = $r[$n];
}
}
$compData[{$val}] = ${$val}
}
foreach($compData['List1'] as $result) {
For ($n = 0; $n <= $CI_NOYEARS; $n++){
echo $result[$n];
}
}
The above code does not work and does not echo any data from the $compData array. Below is an example that works just fine where I execute the code using the ID's directly.
$sth = mysqli_query($conn, "SELECT * FROM {$tableName} WHERE ID = 'List1'");
$List1 = array();
while($r = mysqli_fetch_array($sth)) {
For ($n = 1; $n <= $CI_NOYEARS; $n++){
$List1['data'] = $r[$n];
}
}
$companyData = array();
$companyData['List1'] = $List1;
foreach($compData['List1'] as $result) {
For ($n = 0; $n <= $CI_NOYEARS; $n++){
echo $result[$n];
}
}
php arrays mysqli
I have an array of ID's used to execute mysqli queries in a foreach loop (the below code is just an example - the array will contain more than 50+ elements/ID's). For each array element/ID I query an array which I then include in a compData array. However, I am unsure of how to dynamically define the arrays in the foreach loop. I would very much appreciate if you can check whether I am using ${$val} correctly in the below or if something else is wrong. Thanks.
$idArray = array(
"List1",
"List2",
);
$compData = array();
foreach($idArray as $val) {
$sth = mysqli_query($conn, "SELECT * FROM {$tableName} WHERE ID = {$val}");
${$val} = array();
while($r = mysqli_fetch_array($sth)) {
For ($n = 1; $n <= $CI_NOYEARS; $n++){
${$val}['data'] = $r[$n];
}
}
$compData[{$val}] = ${$val}
}
foreach($compData['List1'] as $result) {
For ($n = 0; $n <= $CI_NOYEARS; $n++){
echo $result[$n];
}
}
The above code does not work and does not echo any data from the $compData array. Below is an example that works just fine where I execute the code using the ID's directly.
$sth = mysqli_query($conn, "SELECT * FROM {$tableName} WHERE ID = 'List1'");
$List1 = array();
while($r = mysqli_fetch_array($sth)) {
For ($n = 1; $n <= $CI_NOYEARS; $n++){
$List1['data'] = $r[$n];
}
}
$companyData = array();
$companyData['List1'] = $List1;
foreach($compData['List1'] as $result) {
For ($n = 0; $n <= $CI_NOYEARS; $n++){
echo $result[$n];
}
}
php arrays mysqli
php arrays mysqli
edited Nov 15 '18 at 22:06
ChartProblems
asked Nov 15 '18 at 20:50
ChartProblemsChartProblems
10510
10510
There is rarely any use for variable variables that can't be solved with an associative array.
– miken32
Nov 15 '18 at 21:32
What exactly is the problem you're having?
– miken32
Nov 15 '18 at 21:33
My problem is that the data is not being added to the $compData array. The echo does not return anything. I am fairly sure that there is an error in the $compData[{$val}] = ${$val . "Array"} line.
– ChartProblems
Nov 15 '18 at 21:37
And I'm pretty sure yourecho
doesn't return anything because you're burying your data many levels deep. Your loops at the end there don't account for the fact that you've put things under thedata
element of the array, for example.
– miken32
Nov 15 '18 at 21:44
I have tried to add more explanation and included a piece of code that works as intended. Hopefully this will clarify what the issue is?
– ChartProblems
Nov 15 '18 at 21:46
|
show 4 more comments
There is rarely any use for variable variables that can't be solved with an associative array.
– miken32
Nov 15 '18 at 21:32
What exactly is the problem you're having?
– miken32
Nov 15 '18 at 21:33
My problem is that the data is not being added to the $compData array. The echo does not return anything. I am fairly sure that there is an error in the $compData[{$val}] = ${$val . "Array"} line.
– ChartProblems
Nov 15 '18 at 21:37
And I'm pretty sure yourecho
doesn't return anything because you're burying your data many levels deep. Your loops at the end there don't account for the fact that you've put things under thedata
element of the array, for example.
– miken32
Nov 15 '18 at 21:44
I have tried to add more explanation and included a piece of code that works as intended. Hopefully this will clarify what the issue is?
– ChartProblems
Nov 15 '18 at 21:46
There is rarely any use for variable variables that can't be solved with an associative array.
– miken32
Nov 15 '18 at 21:32
There is rarely any use for variable variables that can't be solved with an associative array.
– miken32
Nov 15 '18 at 21:32
What exactly is the problem you're having?
– miken32
Nov 15 '18 at 21:33
What exactly is the problem you're having?
– miken32
Nov 15 '18 at 21:33
My problem is that the data is not being added to the $compData array. The echo does not return anything. I am fairly sure that there is an error in the $compData[{$val}] = ${$val . "Array"} line.
– ChartProblems
Nov 15 '18 at 21:37
My problem is that the data is not being added to the $compData array. The echo does not return anything. I am fairly sure that there is an error in the $compData[{$val}] = ${$val . "Array"} line.
– ChartProblems
Nov 15 '18 at 21:37
And I'm pretty sure your
echo
doesn't return anything because you're burying your data many levels deep. Your loops at the end there don't account for the fact that you've put things under the data
element of the array, for example.– miken32
Nov 15 '18 at 21:44
And I'm pretty sure your
echo
doesn't return anything because you're burying your data many levels deep. Your loops at the end there don't account for the fact that you've put things under the data
element of the array, for example.– miken32
Nov 15 '18 at 21:44
I have tried to add more explanation and included a piece of code that works as intended. Hopefully this will clarify what the issue is?
– ChartProblems
Nov 15 '18 at 21:46
I have tried to add more explanation and included a piece of code that works as intended. Hopefully this will clarify what the issue is?
– ChartProblems
Nov 15 '18 at 21:46
|
show 4 more comments
2 Answers
2
active
oldest
votes
It's a bit tough figuring out what your goal is based on the code you provided, but it seems far more complicated than it needs to be. I think what I've got here will replicate your desired output.
You should also be using prepared statements; they are safer, and take a lot of the overhead out of database queries when you're going to be repeating the same query multiple times. I've implemented them here.
$idArray = ["List1", "List2"];
$sth = $conn->prepare("SELECT * FROM `$tableName` WHERE ID = ?");
foreach($idArray as $val) {
$data = ;
$sth->bind_param("s", $val);
$sth->execute();
$result = $sth->get_result();
while ($r = $result->fetch_array()) {
$data = array_merge($data, array_splice($r, 1, $CI_NOYEARS));
}
$compData[$val]["data"] = $data;
}
foreach($compData["List1"]["data"] as $result) {
echo $result;
}
This works pretty much perfectly! Thank you very much. However, for some reason the echo includes the "List1" ID name as the first entry. For instance the echo currently is: "List1, 1, 2, 3..". Would you be able to tell me how I can prevent that?
– ChartProblems
Nov 15 '18 at 22:26
Unless it's in the database, my code isn't outputting that value anywhere. Tryprint_r($compData)
to see what's actually in the array.
– miken32
Nov 15 '18 at 22:28
add a comment |
I don't understand why you're using that curly-brace notation at all when you're just copying it over into a multidimensional compData array anyway? Why not just directly put the data into the array:
for ($n = 1; $n <= $CI_NOYEARS; $n++) {
$compdata[$val]['data'] = $r[$n];
}
(You'll also need to declare it as an array at the top level of the foreach
of course.)
Side note: I would suggest not interpolating data directly into your query string like that, and instead using bound parameters: http://php.net/manual/en/mysqli-stmt.bind-param.php It's much more secure.
Thank you for helping out. I've tried your solution but my echo of the results in $compData['List1'] for instance doesn't return anything. The code above works just fine when I do execute it for every ID but due to the large amounts of ID's I just wanted to do this dynamic by using an array and foreach statement instead? I hope this makes sense. I will have a look at the link you provided as well.
– ChartProblems
Nov 15 '18 at 21:06
Did you declare the value as an array before the code I gave you? If so, and it's still not working, I'd suggest printing the value of$val
inside that loop, so you can make sure there is, in fact, a "List1" in there. (I'd also suggest using either print_r or var_dump when outputting arrays, instead of echo, since those will give you the full values of the array entries.)
– IceMetalPunk
Nov 16 '18 at 18:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327714%2fusing-curly-brackets-in-multidimensional-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's a bit tough figuring out what your goal is based on the code you provided, but it seems far more complicated than it needs to be. I think what I've got here will replicate your desired output.
You should also be using prepared statements; they are safer, and take a lot of the overhead out of database queries when you're going to be repeating the same query multiple times. I've implemented them here.
$idArray = ["List1", "List2"];
$sth = $conn->prepare("SELECT * FROM `$tableName` WHERE ID = ?");
foreach($idArray as $val) {
$data = ;
$sth->bind_param("s", $val);
$sth->execute();
$result = $sth->get_result();
while ($r = $result->fetch_array()) {
$data = array_merge($data, array_splice($r, 1, $CI_NOYEARS));
}
$compData[$val]["data"] = $data;
}
foreach($compData["List1"]["data"] as $result) {
echo $result;
}
This works pretty much perfectly! Thank you very much. However, for some reason the echo includes the "List1" ID name as the first entry. For instance the echo currently is: "List1, 1, 2, 3..". Would you be able to tell me how I can prevent that?
– ChartProblems
Nov 15 '18 at 22:26
Unless it's in the database, my code isn't outputting that value anywhere. Tryprint_r($compData)
to see what's actually in the array.
– miken32
Nov 15 '18 at 22:28
add a comment |
It's a bit tough figuring out what your goal is based on the code you provided, but it seems far more complicated than it needs to be. I think what I've got here will replicate your desired output.
You should also be using prepared statements; they are safer, and take a lot of the overhead out of database queries when you're going to be repeating the same query multiple times. I've implemented them here.
$idArray = ["List1", "List2"];
$sth = $conn->prepare("SELECT * FROM `$tableName` WHERE ID = ?");
foreach($idArray as $val) {
$data = ;
$sth->bind_param("s", $val);
$sth->execute();
$result = $sth->get_result();
while ($r = $result->fetch_array()) {
$data = array_merge($data, array_splice($r, 1, $CI_NOYEARS));
}
$compData[$val]["data"] = $data;
}
foreach($compData["List1"]["data"] as $result) {
echo $result;
}
This works pretty much perfectly! Thank you very much. However, for some reason the echo includes the "List1" ID name as the first entry. For instance the echo currently is: "List1, 1, 2, 3..". Would you be able to tell me how I can prevent that?
– ChartProblems
Nov 15 '18 at 22:26
Unless it's in the database, my code isn't outputting that value anywhere. Tryprint_r($compData)
to see what's actually in the array.
– miken32
Nov 15 '18 at 22:28
add a comment |
It's a bit tough figuring out what your goal is based on the code you provided, but it seems far more complicated than it needs to be. I think what I've got here will replicate your desired output.
You should also be using prepared statements; they are safer, and take a lot of the overhead out of database queries when you're going to be repeating the same query multiple times. I've implemented them here.
$idArray = ["List1", "List2"];
$sth = $conn->prepare("SELECT * FROM `$tableName` WHERE ID = ?");
foreach($idArray as $val) {
$data = ;
$sth->bind_param("s", $val);
$sth->execute();
$result = $sth->get_result();
while ($r = $result->fetch_array()) {
$data = array_merge($data, array_splice($r, 1, $CI_NOYEARS));
}
$compData[$val]["data"] = $data;
}
foreach($compData["List1"]["data"] as $result) {
echo $result;
}
It's a bit tough figuring out what your goal is based on the code you provided, but it seems far more complicated than it needs to be. I think what I've got here will replicate your desired output.
You should also be using prepared statements; they are safer, and take a lot of the overhead out of database queries when you're going to be repeating the same query multiple times. I've implemented them here.
$idArray = ["List1", "List2"];
$sth = $conn->prepare("SELECT * FROM `$tableName` WHERE ID = ?");
foreach($idArray as $val) {
$data = ;
$sth->bind_param("s", $val);
$sth->execute();
$result = $sth->get_result();
while ($r = $result->fetch_array()) {
$data = array_merge($data, array_splice($r, 1, $CI_NOYEARS));
}
$compData[$val]["data"] = $data;
}
foreach($compData["List1"]["data"] as $result) {
echo $result;
}
edited Nov 15 '18 at 22:22
answered Nov 15 '18 at 22:11
miken32miken32
24.1k95073
24.1k95073
This works pretty much perfectly! Thank you very much. However, for some reason the echo includes the "List1" ID name as the first entry. For instance the echo currently is: "List1, 1, 2, 3..". Would you be able to tell me how I can prevent that?
– ChartProblems
Nov 15 '18 at 22:26
Unless it's in the database, my code isn't outputting that value anywhere. Tryprint_r($compData)
to see what's actually in the array.
– miken32
Nov 15 '18 at 22:28
add a comment |
This works pretty much perfectly! Thank you very much. However, for some reason the echo includes the "List1" ID name as the first entry. For instance the echo currently is: "List1, 1, 2, 3..". Would you be able to tell me how I can prevent that?
– ChartProblems
Nov 15 '18 at 22:26
Unless it's in the database, my code isn't outputting that value anywhere. Tryprint_r($compData)
to see what's actually in the array.
– miken32
Nov 15 '18 at 22:28
This works pretty much perfectly! Thank you very much. However, for some reason the echo includes the "List1" ID name as the first entry. For instance the echo currently is: "List1, 1, 2, 3..". Would you be able to tell me how I can prevent that?
– ChartProblems
Nov 15 '18 at 22:26
This works pretty much perfectly! Thank you very much. However, for some reason the echo includes the "List1" ID name as the first entry. For instance the echo currently is: "List1, 1, 2, 3..". Would you be able to tell me how I can prevent that?
– ChartProblems
Nov 15 '18 at 22:26
Unless it's in the database, my code isn't outputting that value anywhere. Try
print_r($compData)
to see what's actually in the array.– miken32
Nov 15 '18 at 22:28
Unless it's in the database, my code isn't outputting that value anywhere. Try
print_r($compData)
to see what's actually in the array.– miken32
Nov 15 '18 at 22:28
add a comment |
I don't understand why you're using that curly-brace notation at all when you're just copying it over into a multidimensional compData array anyway? Why not just directly put the data into the array:
for ($n = 1; $n <= $CI_NOYEARS; $n++) {
$compdata[$val]['data'] = $r[$n];
}
(You'll also need to declare it as an array at the top level of the foreach
of course.)
Side note: I would suggest not interpolating data directly into your query string like that, and instead using bound parameters: http://php.net/manual/en/mysqli-stmt.bind-param.php It's much more secure.
Thank you for helping out. I've tried your solution but my echo of the results in $compData['List1'] for instance doesn't return anything. The code above works just fine when I do execute it for every ID but due to the large amounts of ID's I just wanted to do this dynamic by using an array and foreach statement instead? I hope this makes sense. I will have a look at the link you provided as well.
– ChartProblems
Nov 15 '18 at 21:06
Did you declare the value as an array before the code I gave you? If so, and it's still not working, I'd suggest printing the value of$val
inside that loop, so you can make sure there is, in fact, a "List1" in there. (I'd also suggest using either print_r or var_dump when outputting arrays, instead of echo, since those will give you the full values of the array entries.)
– IceMetalPunk
Nov 16 '18 at 18:35
add a comment |
I don't understand why you're using that curly-brace notation at all when you're just copying it over into a multidimensional compData array anyway? Why not just directly put the data into the array:
for ($n = 1; $n <= $CI_NOYEARS; $n++) {
$compdata[$val]['data'] = $r[$n];
}
(You'll also need to declare it as an array at the top level of the foreach
of course.)
Side note: I would suggest not interpolating data directly into your query string like that, and instead using bound parameters: http://php.net/manual/en/mysqli-stmt.bind-param.php It's much more secure.
Thank you for helping out. I've tried your solution but my echo of the results in $compData['List1'] for instance doesn't return anything. The code above works just fine when I do execute it for every ID but due to the large amounts of ID's I just wanted to do this dynamic by using an array and foreach statement instead? I hope this makes sense. I will have a look at the link you provided as well.
– ChartProblems
Nov 15 '18 at 21:06
Did you declare the value as an array before the code I gave you? If so, and it's still not working, I'd suggest printing the value of$val
inside that loop, so you can make sure there is, in fact, a "List1" in there. (I'd also suggest using either print_r or var_dump when outputting arrays, instead of echo, since those will give you the full values of the array entries.)
– IceMetalPunk
Nov 16 '18 at 18:35
add a comment |
I don't understand why you're using that curly-brace notation at all when you're just copying it over into a multidimensional compData array anyway? Why not just directly put the data into the array:
for ($n = 1; $n <= $CI_NOYEARS; $n++) {
$compdata[$val]['data'] = $r[$n];
}
(You'll also need to declare it as an array at the top level of the foreach
of course.)
Side note: I would suggest not interpolating data directly into your query string like that, and instead using bound parameters: http://php.net/manual/en/mysqli-stmt.bind-param.php It's much more secure.
I don't understand why you're using that curly-brace notation at all when you're just copying it over into a multidimensional compData array anyway? Why not just directly put the data into the array:
for ($n = 1; $n <= $CI_NOYEARS; $n++) {
$compdata[$val]['data'] = $r[$n];
}
(You'll also need to declare it as an array at the top level of the foreach
of course.)
Side note: I would suggest not interpolating data directly into your query string like that, and instead using bound parameters: http://php.net/manual/en/mysqli-stmt.bind-param.php It's much more secure.
answered Nov 15 '18 at 20:58
IceMetalPunkIceMetalPunk
954716
954716
Thank you for helping out. I've tried your solution but my echo of the results in $compData['List1'] for instance doesn't return anything. The code above works just fine when I do execute it for every ID but due to the large amounts of ID's I just wanted to do this dynamic by using an array and foreach statement instead? I hope this makes sense. I will have a look at the link you provided as well.
– ChartProblems
Nov 15 '18 at 21:06
Did you declare the value as an array before the code I gave you? If so, and it's still not working, I'd suggest printing the value of$val
inside that loop, so you can make sure there is, in fact, a "List1" in there. (I'd also suggest using either print_r or var_dump when outputting arrays, instead of echo, since those will give you the full values of the array entries.)
– IceMetalPunk
Nov 16 '18 at 18:35
add a comment |
Thank you for helping out. I've tried your solution but my echo of the results in $compData['List1'] for instance doesn't return anything. The code above works just fine when I do execute it for every ID but due to the large amounts of ID's I just wanted to do this dynamic by using an array and foreach statement instead? I hope this makes sense. I will have a look at the link you provided as well.
– ChartProblems
Nov 15 '18 at 21:06
Did you declare the value as an array before the code I gave you? If so, and it's still not working, I'd suggest printing the value of$val
inside that loop, so you can make sure there is, in fact, a "List1" in there. (I'd also suggest using either print_r or var_dump when outputting arrays, instead of echo, since those will give you the full values of the array entries.)
– IceMetalPunk
Nov 16 '18 at 18:35
Thank you for helping out. I've tried your solution but my echo of the results in $compData['List1'] for instance doesn't return anything. The code above works just fine when I do execute it for every ID but due to the large amounts of ID's I just wanted to do this dynamic by using an array and foreach statement instead? I hope this makes sense. I will have a look at the link you provided as well.
– ChartProblems
Nov 15 '18 at 21:06
Thank you for helping out. I've tried your solution but my echo of the results in $compData['List1'] for instance doesn't return anything. The code above works just fine when I do execute it for every ID but due to the large amounts of ID's I just wanted to do this dynamic by using an array and foreach statement instead? I hope this makes sense. I will have a look at the link you provided as well.
– ChartProblems
Nov 15 '18 at 21:06
Did you declare the value as an array before the code I gave you? If so, and it's still not working, I'd suggest printing the value of
$val
inside that loop, so you can make sure there is, in fact, a "List1" in there. (I'd also suggest using either print_r or var_dump when outputting arrays, instead of echo, since those will give you the full values of the array entries.)– IceMetalPunk
Nov 16 '18 at 18:35
Did you declare the value as an array before the code I gave you? If so, and it's still not working, I'd suggest printing the value of
$val
inside that loop, so you can make sure there is, in fact, a "List1" in there. (I'd also suggest using either print_r or var_dump when outputting arrays, instead of echo, since those will give you the full values of the array entries.)– IceMetalPunk
Nov 16 '18 at 18:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327714%2fusing-curly-brackets-in-multidimensional-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There is rarely any use for variable variables that can't be solved with an associative array.
– miken32
Nov 15 '18 at 21:32
What exactly is the problem you're having?
– miken32
Nov 15 '18 at 21:33
My problem is that the data is not being added to the $compData array. The echo does not return anything. I am fairly sure that there is an error in the $compData[{$val}] = ${$val . "Array"} line.
– ChartProblems
Nov 15 '18 at 21:37
And I'm pretty sure your
echo
doesn't return anything because you're burying your data many levels deep. Your loops at the end there don't account for the fact that you've put things under thedata
element of the array, for example.– miken32
Nov 15 '18 at 21:44
I have tried to add more explanation and included a piece of code that works as intended. Hopefully this will clarify what the issue is?
– ChartProblems
Nov 15 '18 at 21:46