Call variable inside array element [closed] - php

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"];
?>

Related

Find all reaccurances of text inbetween two strings [closed]

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
I'm trying to find a text between $something and $something_else and ditch it out in an array.
I would think you need preg_match to do this but I have been trying alot and still have no idea.
This should work no matter what $something and $something_else is.
You need to read the documentation of preg_match and preg_match_all.
Here's a simple example that will match whatever content inside (double quotes)..
<?php
preg_match_all('~"(.*?)"~',
'Hey there "I will be matched", because "I am inside the double quotes"',
$out, PREG_PATTERN_ORDER);
print_r(($out[0]));
OUTPUT :
Array
(
[0] => "I will be matched"
[1] => "I am inside the double quotes"
)
Correct me if i am wrong. We can use explode one string into a array. Use pre_match_all for the another string with each word of the array . In this way, it will work with any string.

Get reference variable from multidimensional array [closed]

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.

How to convert te JSON string to a JSON object in php? [closed]

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 9 years ago.
Improve this question
I got a object like this:
{"1":"FFS","2":"S"}, and I use json_decode function to convert it into an array, but I am not success with it. It becomes the array like this: {"FFS", "S"}, but missing the {"1", "2"}, Can I convert it to become a dict or something, that I can access both value? Thanks.
use true param, to convert object to associative array for json_decode(), like do:
$str = '{"1":"FFS","2":"S"}';
echo "<pre>"; print_r(json_decode($str, true));
gives::
Array
(
[1] => FFS
[2] => S
)
$myjsonobject = json_decode('{"1":"FFS","2":"S"}');
Should working.
Try: print_r($myjsonobject); to validate.
Its working.

Variable in array (returning null) [closed]

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 9 years ago.
Improve this question
I cannot seem to be able to use the variable into the array. It must be a syntax error.
Please, help me sort this out.
$id=$_POST['eventid'];
$data = array('message' => $id);
$id content is null.
Thanks.
Your coding is nice except the $_POST value is not getting fetched. I have commented the POST var just to make sure your code works fine.
<?php
$id='test';//$_POST['eventid'];
$data = array('message' => $id);
print_r($data);//output : Array ( [message] => test )
?>
$id = $_POST['eventid'];
$data = array('message' => $id);
If $id is null, it means that $_POST['eventid'] is null.
Also, are you sure that it is actually null, and not a blank string? Because there's a slight difference between the two.
My guess is that $_POST['eventid'] does not exist or the value is an empty string.

Display exploded array [closed]

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 9 years ago.
Improve this question
This is a very noob question. The previous developer has coded the lines as below
$a = array("30"=>"ok","40"=>"yes");
$b = "hi";
$c = $a."|".$b;
$d = explode("|",$c);
print_r($d[0]);
How can I display the array array("30"=>"ok","40"=>"yes")? print_r($d[0]); seems to print just array
This prints "array" instead of the actual array values is because this line:
$c = $a."|".$b;
What you're doing is saying:
$c = [array] + [string] + [string];
which will force array to be transformed into a string, which is just "array"
if you really want a | separated string of array indices, you could theoretically do this:
$c = implode("|",$a)."|".$b;
But the real best solution here would be to add something to the array before exploding the array:
$a['50'] = 'hi';
$d = explode("|", $c);

Categories