My programm read info from textbox and show it to user, so the problems is in middle part of that operation. How to make $myArray don't re-create itself when i update page?
session_start();
if (!isset($myArray)) $myArray = array();
$myArray[] = $_POST['nameAuthor'];
foreach($myArray as $key=>$value)
{
echo "$key->$value";
}
//$_SESSION['arr'] = $myArray;
Try this:
session_start();
if(!isset($_SESSION['arr'])) $_SESSION['arr'] = array();
$_SESSION['arr'][] = $_POST['nameAuthor'];
foreach($_SESSION['arr'] as $key => $value)
{
echo "$key->$value";
}
Related
I have the code to show TripUUID. But it showed just one data. The code is as follows:
function list_of_tripreview(){
$data = $this->data;
$arr = [];
$data['list_of_tripreview'] = $this->cms_model->list_of_tripreview();
$TripUUID = $data['list_of_tripreview'][0]['TripUUID'];
echo json_encode($TripUUID);
This code gives the output TripUUID on index 0. How do I revise this program to show all index data without [0]?
You can use foreach loop
$data['list_of_tripreview'] = $this->cms_model->list_of_tripreview();
$datas = $data['list_of_tripreview'];
//$TripUUID = $data['list_of_tripreview'][0]['TripUUID'];
foreach($datas as $key => $item){
echo $item['TripUUID'].'<br>';
}
here is what I'm trying to do. I'm retrieving information from a database via array. What is happening is the information from the previous array is going into the next array.
Here is the code:
$i = 0;
foreach ($array_name as $key => test_name) {
$id = $test_name['id']
foreach ($test_name['id] as $key => $test_id {
$data = ModelClass::Information($test_id);
$array_name[$i]['new_infroamtion'] = $data'
}
}
So right now based on the code data from the table is correctly going into the first array, however, information based from the first array is going into the second array..
Let me know if you need anymore information.
Thank you
You are using $array_name while you are iterating through $array_name. This is valid code if you want to do this, but I don't think you do. You need to change the second $array_name to something else.
$i = 0;
foreach (**$array_name** as $key => test_name) {
$id = $test_name['id']
foreach ($test_name['id'] as $key => $test_id {
$data = ModelClass::Information($test_id);
**$array_name**[$i]['new_infroamtion'] = $data
}
}
I did find a solution. What I had to do was add the following
$s = array()
Then in the for loop, I added the following code:
foreach ($test_name['id] as $key => $test_id {
$data = ModelClass::Information($test_id);
$s[] = $data
$array_name[$i]['new_infroamtion'] = $s'
}
I have got following array in php:
theArray('id':'123','akey':'a';
'id':'234','akey':'b';
'id':'567','akey':'c';)
I would like to dynamically add another key in a loop so that my array will look like:
theArray('id':'123','akey':'a', 'anotherkey':'1';
'id':'234','akey':'b'; 'anotherkey':'1';
'id':'567','akey':'c'; 'anotherkey':'1';)
The code I have written is the following:
foreach($theArray as $row)
{
$row['anotherkey'] = "1";
}
but it is not working. What am I doing wrong?
You're not actually storing your new value in $theArray, you're just assigning it to your temporary $row variable. What you want to do is this:
foreach($theArray as $key => $row) {
$theArray[$key]["anotherkey"] = "1";
}
Try with
foreach($theArray as &$row)
{
$row['anotherkey'] = "1";
}
foreach($theArray as $key => $row)
{
$theArray[$key]['anotherkey'] = "1";
}
is more robust
I want to save values send via POST in a session array:
$reply = array('thread_id', 'reply_content');
$_POST['thread_id'] = 2; # test it
$_SESSION['reply'] = array();
foreach ($reply as $key)
{
if (in_array($key, $_POST))
{
$_SESSION['reply'][$key] = $_POST[$key];
}
}
var_dump($_SESSION['reply']);
For example I want to check if the keys 'thread_id' and 'thread_content' are send in post, if they are then I want to save them in a session array called reply, using the same keys.
So for example if 'thread_id' is send via POST:
$_POST['thread_id'] = 'blah';
Then this should get saved in a session called 'reply', using the same key:
$_SESSION['reply']['thread_id'] = 'blah';
How can this be done?
In general, your approach looks valid, but I'm going to guess that you may not be calling session_start(), which is necessary to persist session data.
session_start();
if(!$_SESSION['POST']) $_SESSION['POST'] = array();
foreach ($_POST as $key => $value) {
$_SESSION['POST'][$key] = $value;
}
var_dump($_SESSION['POST']);
in_array($needle, $haystack) checks whether $needle is a value in $haystack and not a key. Use array_key_exists or isset instead:
foreach ($reply as $key)
{
if (array_key_exists($key, $_POST))
{
$_SESSION['reply'][$key] = $_POST[$key];
}
}
Or:
$_SESSION['reply'] = array_merge($_SESSION['reply'], array_intersect_key($_POST, array_flip($reply)));
Use this
$reply = array('thread_id', 'reply_content');
$_POST['thread_id'] = 2; # test it
$_SESSION['reply'] = array();
foreach ($reply as $key)
{
if (isset($_POST[$key]))
{
$_SESSION['reply'][$key] = $_POST[$key];
}
}
how take string from array define as new array,
how to code in php
$column = array("id","name","value");
let say found 3 row from mysql
want result to be like this
$id[0] = "1";
$id[1] = "6";
$id[2] = "10";
$name[0] = "a";
$name[1] = "b";
$name[2] = "c";
$value[0] = "bat";
$value[1] = "rat";
$value[2] = "cat";
I want to take string from $column array define as new array.
how to code it?
or if my question is stupid , my please to have suggestion.
thank you.
Answer I made on your previous question:
$result = mysql_query($sql);
$num = mysql_num_rows($result);
$i = 0;
if ($num > 0) {
while ($row = mysql_fetch_assoc($result)) {
foreach($row as $column_name => $column_value) {
$temp_array[$column_name][$i] = $column_value;
}
$i++;
}
foreach ($temp_array as $name => $answer) {
$$name = $answer;
}
}
I can't see why you'd want to model your data like this, you're asking for a world of hurt in terms of debugging. There are "variable variables" you could use to define this, or build global variables dynamically using $GLOBALS:
$somevar = "hello"
$$somevar[0] = "first index"; // creates $hello[0]
$GLOBALS[$somevar][0] = "first index"; // creates $hello[0] in global scope
try
$array = array();
foreach ($results as $r){
foreach ($column as $col ){
$array[$col][] = $r[$col];
}
}
extract ($array);
or you can simply do this
$id = array();
$name = array();
$value = array();
foreach ( $results as $r ){
$id[] = $r['id']; // or $r[0];
$name[] = $r['name'];
$value[] = $r['value'];
}
Hope this is what you asked
This is pretty dangerous, as it may overwrite variables you consider safe in your code. What you're looking for is extract:
$result = array();
while ($row = mysql_fetch_array($result))
foreach ($column as $i => $c)
$result[$c][] = $row[$i];
extract($result);
So, if $result was array( 'a' => array(1,2), 'b' => array(3,4)), the last line defines variables $a = array(1,2) and $b = array(3,4).
You cannot use variable variables right on for this, and you shouldn't anyway. But this is how you could do it:
foreach (mysql_fetch_something() as $row) {
foreach ($row as $key=>$value) {
$results[$key][] = $value;
}
}
extract($results);
Ideally you would skip the extract, and use $results['id'][1] etc. But if you only extract() the nested array in subfunctions, then the local variable scope pollution is acceptable.
There is no need for arrays or using $_GLOBALS, i believe the best way to create variables named based on another variable value is using curly brackets:
$variable_name="value";
${$variable_name}="3";
echo $value;
//Outputs 3
If you are more specific on what is the array you receive i can give a more complete solution, although i must warn you that i have never had to use such method and it's probably a sign of a bad idea.
If you want to learn more about this, here is a useful link:
http://www.reddit.com/r/programming/comments/dst56/today_i_learned_about_php_variable_variables/