Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm having a problem with a simple include statement and I have no idea what's causing it.
First I'm storing information about tables and columns in a file called dbInit.php
which looks like this:
<?php
$aTableNames = array (
"tbl_crs" => "tbl_crs",
"tbl_lct" => "tbl_lct",
"tbl_prf" => "tbl_prf",
"tbl_qst" => "tbl_qst",
"tbl_uni" => "tbl_uni",
"tbl_usr" => "tbl_usr"
);
$aAuxTableNames = array (
"aux_crs_lct" => "aux_crs_lct",
"aux_lct_prf" => "aux_lct_prf",
"aux_uni_crs" => "aux_uni_crs"
);
?>
Now I simply want to access these arrays from another file. So I included it like this:
include "Ini/dbInit.php";
So far so good. Now I want to use the values of the arrays like this:
$sTable1 = $aAuxTableNames["aux_uni_crs"];
$sTable2 = $aTableNames["tbl_crs"];
The directory looks like this:
How can I access these arrays in the way sown above?
Thanks so much in advance.
Best regards
Amnney
I had no issue implementing this. So basically This is what I did. I copied your structure by having an include file with the arrays, exactly as you have posted here. Then I created a test file on the root of my server. This test file was php as well and I did the include as normal. Created the new variables, and then print_r(''); the 2 new variables you created out and received no errors.
This is my dbinit.php file:
<?php
$aTableNames = array (
"tbl_crs" => "tbl_crs",
"tbl_lct" => "tbl_lct",
"tbl_prf" => "tbl_prf",
"tbl_qst" => "tbl_qst",
"tbl_uni" => "tbl_uni",
"tbl_usr" => "tbl_usr"
);
$aAuxTableNames = array (
"aux_crs_lct" => "aux_crs_lct",
"aux_lct_prf" => "aux_lct_prf",
"aux_uni_crs" => "aux_uni_crs"
);
?>
Ok so exactly as yours is. Then I created my file on the root called testpage.php:
<?php
include('inc/dbinit.php');
$sTable1 = $aAuxTableNames["aux_uni_crs"];
$sTable2 = $aTableNames["tbl_crs"];
print_r($sTable1);
print_r($sTable2);
?>
My structure as indicated: Structure
Absolutely no issues running this or retrieving the data. Hope this helps.
Final Output: Final Output
Try this:
// Ini/dbInit.php
<?php
return array(
'aTableNames' => array(
"tbl_crs" => "tbl_crs",
"tbl_lct" => "tbl_lct",
"tbl_prf" => "tbl_prf",
"tbl_qst" => "tbl_qst",
"tbl_uni" => "tbl_uni",
"tbl_usr" => "tbl_usr"
),
'aAuxTableNames' => array(
"aux_crs_lct" => "aux_crs_lct",
"aux_lct_prf" => "aux_lct_prf",
"aux_uni_crs" => "aux_uni_crs"
),
);
In file, where you want to get Ini/dbInit.php
<?php
$config = include "Ini/dbInit.php";
$sTable1 = $config['aAuxTableNames']['aux_uni_crs']
$sTable2 = $config['aTableNames']['tbl_crs'];
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an array that looks like this:
And I need to translate it into this:
Some things to know about the $startArray is that it is dynamic based on the amount of people submitted on a form. Each person always has just those three fields though (custom_12, custom_13, custom_14 aka fName, lName, email). So if there were 5 members, the 5th members fName would be the key custom_12-5 in the start array.
I've been looking around on stackoverflow for a question just like this and I was not able to find one. What would be the steps taken and possible array functions or anything else for creating the $endArray ?
There's actually a builtin function for this: https://www.php.net/manual/en/function.array-chunk.php
For example, array_chunk($startArray, 3) will give you the base for your new array, you'll just need to then iterate through it and rename the keys.
Alternatively, just iterate through the array yourself and add the values to a new array depending on the index of the current iteration.
Thanks to Charlie's advice, I came up with this.
$startArray = array(
'custom_12' => 'john',
'custom_13' => 'johny',
'custom_14' => 'john#johny.com',
'custom_12-2' => 'bob',
'custom_13-2' => 'bobby',
'custom_14-2' => 'bob#bobby.com',
'custom_12-3' => 'don',
'custom_13-3' => 'donny',
'custom_14-3' => 'don#donny.com'
);
$middleArray = array_chunk($startArray, 3);
$endArray = array_map(function($val) {
return array(
'fName' => $val[0],
'lName' => $val[1],
'email' => $val[2]
);
}, $middleArray);
echo "<pre>";
print_r($endArray);
echo "</pre>";
And the output is exactly what I wanted:
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The output from the code below only shows me the last row of my data. Any ideas? I want it to show me all rows.
Here is the code:
$FinalSet = array();
while ($ResultSet = mysqli_fetch_array($Query)){
$FinalSet = array(
'idExam' => $ResultSet[0],
'SubjectName' => $ResultSet[1],
'ExamTime' => $ResultSet[2],
'ExamDate' => $ResultSet[3],
'IntakeCode' => $ResultSet[4],
'Scope' => $ResultSet[5],
);
}
echo json_encode($FinalSet);
You have to add them to your array. At the moment you are replacing the array itself.
You can do this by using
$FinalSet[] = array(
instead of
$FinalSet = array(
You can also use array_push, which does the same:
array_push($FinalSet, array(......));
You are already initialized $FinalSet = array(); and you are storing data single index so your data has been override and you found last row your data. Now you can use $FinalSet[] instead of $FinalSet. You will get all data.
Try this, You can use $FinalSet[] or array_push() to push the array content into the main array. Currently, you are jut replacing the same array respective of the loop counts. So you getting the last array value in the variable.
$FinalSet[] = array(
'idExam' => $ResultSet[0],
'SubjectName' => $ResultSet[1],
'ExamTime' => $ResultSet[2],
'ExamDate' => $ResultSet[3],
'IntakeCode' => $ResultSet[4],
'Scope' => $ResultSet[5]
);
instead of
$FinalSet = array(
'idExam' => $ResultSet[0],
'SubjectName' => $ResultSet[1],
'ExamTime' => $ResultSet[2],
'ExamDate' => $ResultSet[3],
'IntakeCode' => $ResultSet[4],
'Scope' => $ResultSet[5],
);
Try the following in the loop:
while (...) {
$FinalSet[] = array(...);
}
Below is my code:
<?php
$text_res = array(
'eng' => array('chapter' => 'Chapter'),
'rus' => array('chapter' => 'Глава')
);
echo $text_res['eng']['chapter'];
?>
why is it printing empty string?
This particular code works. The question was asked not quite correctly. See the comments
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have one code, I cannot imagine, how can I read this array, using php. please help me to..
array(
'serialize_data' => array(
array('name' => 'cadidate_id_0', value => '81112890V'),
array('name' => 'cadidate_id_1', value => '822312890V'),
array('name' => 'cadidate_id_2', value => '873312110V'),
array('name' => 'cadidate_id_3', value => '873312890V')
)
);
foreach($array as $key=>$value){
echo $value[0]['name'];
echo $value[0]['value'];
}
use above line to print your array element, like index number 0.
Have you tried
print_r($your-array);
?
To access individual levels it looks like you need to go down a level or two. I.e.
echo $your-array['serialize_data'][0]['name'];
you can use following method.
$array = array(
'serialize_data' => array(
array('name' => 'cadidate_id_0', value => '81112890V'),
array('name' => 'cadidate_id_1', value => '822312890V'),
array('name' => 'cadidate_id_2', value => '873312110V'),
array('name' => 'cadidate_id_3', value => '873312890V')
)
);
foreach($array as $key=>$value){
echo '<pre>'; print_r($value); echo '</pre>';
}
using this method you can read or access its name and value by passing the index number.
like:
echo '<pre>'; print_r($value[0]); echo '</pre>';
try
//grab array of name and value
$array=$data-array['serialize_data']
//traverse
foreach($nv as $array)
{
$name=$nv['name'];
$value=$nv['value'];
//do something to name
print $name;
//do something to value
print $value;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have the following example array:
Array
(
[0] => Array
(
[artist] => Artist43141
)
[1] => Array
(
[artist] => Artist2313
)
[2] => Array
(
[artist] => ArtistAdsa
)
[3] => Array
(
[artist] => ArtistAadas
)
)
How do I turn it into something like: "Artist43141, Artist2313, ArtistAdsa, ArtistAadas"
I'd like to know because as far as I tried with some similar questions already answered I wasn't able to find out how.
I would say something like this would work just fine:
$newArray = array_map(function($a) {return $a['artist'];},$oldArray);
No need for loops, or anything so complicated.
You want to create a space separated string containing only the values of the array.
To do so, try:
<?php
$array = Array
(
array(
'artist' => 'Artist43141'
),
array(
'artist' => 'Artist2313'
),
array(
'artist' => 'ArtistAdsa'
),
array(
'artist' => 'ArtistAadas'
),
);
$str = implode(' ', array_map(function($a){ return current($a); }, $array));
Check the PHP Manual for current() and array_map() to learn more about what the above example is doing.
Happy coding!
EDIT:
According to your comment:
I have one more issue, one of my artist values has a special character 'é', and now it doesn't print them. Where should I apply the htmlspecialchars() (if that's supposed to apply)?
…I want to add the following to this answer:
Modify the above code example like this to handle character encoding (assuming you're using UTF-8 encoding):
$str = implode(' ', array_map(function($a)
{
return htmlentities(current($a));
}, $array));
In case you're not using utf8 yet, start doing so by adding this to your php codes which echo to the browser:
header('Content-Type: text/html; charset=utf-8');
or the following in the header section of your html files:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8" />
...
So read up on PHP's htmlentities() and utf8 encoding.
Have fun.