Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
http://sandbox.onlinephpfunctions.com/code/88256ea59ecdeae948cf664b477e113d7263f2c8
As you can see I use $this->options as input value.
What I'm trying to achieve here is getting option from value by key name and return it.
That is the easy part, but what I want to do with variable from returnOption is setting new key on it and see changes in $this->options array.
How can I archieve this?
function & returnOption(&$options, $optionName)
{
foreach($options as $key => &$value)
{
if($key === $optionName)
return $value;
}
}
$op =& $this->returnOption($this->options, $optionName);
$op['newValue'] = 'value';
var_dump($this->options);
Something like that should work. Then again, $this->options should probably be some sort of object with proper accessors for the options you're trying to get to.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 months ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I am trying to access an associative array $foo from a function inside the class. When I log the contents from another function it is empty. I am really unsure what I am doing wrong.
class Item {
function __construct($x = 1) {
$y = do_something($x);
$foo = [
'id' => $y['anotherID'],
'name' => $y['name']
];
}
function insertData($data) {
$variable = $this->foo['id'];
// if I print $this->foo['id'] I get no output
}
}
I have also tried another recommendation of using self::$foo but got all sorts of errors about private and static?
You need to use $this->foo = [] instead of $foo = []. It makes it a property of the class.
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 3 years ago.
Improve this question
I have a very simple web app that is capturing RFID tag reads and then submits it into the Database.
I have a function that was to pass the information through a filter and remove the duplicates and then return an array of unique tag reads.
The function looks like this
$txtarea = $_POST["rfid"];
rfid($txtarea);
function rfid($txtarea){
$array = explode("\r\n", $txtarea);
$rfid_array1 = array_unique($array);
return $rfid_array1;
}
I then use Print_r to check the contents of the array to make sure it works.
When I run the code inside the function I do not get a result returned but when I run the following outside the function
$txtarea = $_POST["rfid"];
rfid($txtarea);
$array = explode("\r\n", $txtarea);
$rfid_array1 = array_unique($array);
It returns the values correctly ?
I am very new to PHP so I apologize if this question seems a little basic.
The function rfid returns a value which you could capture in a variable.
$rfid_array1 = rfid($txtarea);
Note that you could shorten the function a bit:
function rfid($txtarea){
return array_unique(explode("\r\n", $txtarea));
}
Demo on https://3v4l.org/DY8Ts
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
I'm having to put together a PHP snippet involving capturing form submission data. I'm trying to do this the "right" way with OOP PHP, and I'm struggling with array usage since my Googling seems to bring up sample code that is either too simple or too complex for what I'm trying to do.
How do I create a class method that adds keys and values to an array?
My code, which does not work currently:
class Connection
{
private $postItem = array();
public function addPostItem($key,$value)
{
$postItem[$key] -> $value;
}
public function printPostItem() {
return $this->postItem;
}
}
$c1 = new Connection;
$c1->addPostItem('FirstName','John');
$c1->addPostItem('LastName','Doe');
$c1->addPostItem('Email','JohnDoe#mail.com');
var_dump($c1->printPostItem()); // shows no array content
If I'm going against other best practices here, please let me know.
Use this:
public function addPostItem($key,$value)
{
$this->postItem[$key] = $value;
}
Change:
$postItem[$key] -> $value;
To:
$this->postItem[$key] = $value;
Also as Amal said turn on error reporting.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
How to access array key inside foreach function and then pass to mysql query. The following code returns blank page. When the "key" is static id='2', there are no problems. But I need to use the array keys, instead of static values.
foreach ($base as $key => &$value) {
$value = db_query("SELECT * FROM {bo_subject} WHERE exam LIKE '%$key%'")->fetchAllKeyed() or exit(mysql_error());
}
foreach ($array as $key => &$value) {
...
}
(see Manual)
EDIT: Pass $value by reference
I wouldn't recommend using "&" ever, till You are really sure what You are doing. Also i propose to use "for" instead of "foreach" because is a bit faster, more predictable and it is "right" way to change array during loop (for that in most cases reference is used). For getting keys from current value use key() function. It's well documented on php.net manual.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Is it possible to read array element using variable?
I'd like to set $vid in one place depending on the configuration, and then use multiple times i.e. $detailrow["customfields1"];
I want to do this:
$vid = 1;
$detailrow["customfields$vid"];
But no response.
Tried:
$detailrow["customfields{$vid}"];
$detailrow['customfields'.$vid];
but result is the same.
Of course you can do this:
$tmp=array("name" => "foo", "bar" => "name", "field1" => "value1");
You could then do sth. like
echo $tmp["name"];
will print 'foo'
echo $tmp[$tmp["bar"]];
will also print 'foo'
Or
$i=1;
echo $tmp["field".$i]
will print 'value1'
i have tested your code and its working
<?php
$vid = 1;
$detailrow["customfields1"]="rajeev";
echo $detailrow["customfields$vid"];
?>