PHP append json with $ to http request body - php

$body->append('{"stage_id": "43"}');
I would like to make the "43" dynamic and use a $variable for the value: How should look the Syntax look like?

You can use just like this:
<?php
$var = 43;
$array = array('stage_id'=>$var);
$json = json_encode($array);
$body->append($json);
?>

This will help you
$body->append('{"stage_id": $variable}');

First of all you have to declare a variable and assign your dynamic value in it as per your requirement.
$variable = Value; // if value is number like 0,1,2,3,4 or anything.
$variable = "value"; // if value is string put it inside ' or " quotes.
Now you can use your value as a variable:
body->append("{'stage_id': '$variable'}");

Related

php array syntax ${ is confusing me

I create a $values array and then extract the elements into local scope.
$values['status'.$i] = $newStatus[$i];
extract($values);
When I render an html page. I'm using the following
<?php if(${'status'.$i} == 'OUT'){ ?>
but am confused by what the ${ is doing and why $status.$i won't resolve
$status.$i means
take value of $status variable and concatenate it with value of $i variable.
${'status'.$i} means
take value of $i variable, append id to 'status' string and take value of a variable 'status'.$i
Example:
With $i equals '2' and $status equals 'someStatus':
$status.$i evaluated to 'someStatus' . '2', which is 'someStatus2'
${'status'.$i} evaluated to ${'status'.'2'} which is $status2. And if $status2 is defined variable - you will get some value.
I wanted to add to the accepted answer with a suggested alternate way of achieving your goal.
Re-iterating the accepted answer...
Let's assume the following,
$status1 = 'A status';
$status = 'foo';
$i = 1;
$var_name = 'status1';
and then,
echo $status1; // A status
echo $status.$i; // foo1
echo ${'status'.$i}; // A status
echo ${"status$i"}; // A status
echo ${$var_name}; // A status
The string inside the curly brackets is resolved first, effectively resulting in ${'status1'} which is the same as $status1. This is a variable variable.
Read about variable variables - http://php.net/manual/en/language.variables.variable.php
An alternative solution
Multidimensional arrays are probably an easier way to manage your data.
For example, instead of somthing like
$values['status'.$i] = $newStatus[$i];
how about
$values['status'][$i] = $newStatus[$i];
Now we can use the data like,
extract($values);
if($status[$i] == 'OUT'){
// do stuff
}
An alternative solution PLUS
You may even find that you can prepare your status array differently. I'm assuming you're using some sort of loop? If so, these are both equivalent,
for ($i=0; $i<count($newStatus); $i++){
$values['status'][$i] = $newStatus[$i];
}
and,
$values['status'] = $newStatus;
:)

PHP - Adding string value to associative array

This seem so simple, and yet I can't find a solution anywhere.
What I want to do is add the contents (r-value) of a variable to an associative array instead of a reference to the variable.
For example, I want this:
$myStr1 = "sometext";
$myStr2 = "someothertext";
$myArray = array(
"key1"=>$myStr1,
"key2"=>$myStr2
);
echo($myArray["key1"]);
To produce this:
"sometext"
Instead of this:
"1" // why??
Any help would be appreciated.
EDIT:
The above works; my bad. Here's the real problem - my $myStr1 variable isn't just assiged a string literal like in the above; it's created using the following syntax:
$myStr1 = "sometext" + anObject->intProperty + "moretext";
Basically I use the + to concatenate various types into a string. Maybe + isn't doing what I think it's doing?
EDIT:
It was definitely the + operator. I casted all non-strings to strings and used . to concatenate instead.
You've got it correct the first time. Try this:
$myStr1 = "sometext";
$myStr2 = "someothertext";
$myArray = array(
"key1"=>$myStr1,
"key2"=>$myStr2
);
unset($myStr1);
echo($myArray["key1"]);
Even though we unset() the $myStr1 variable, it still echoed sometext.
It should be noted that while it is possible to set $myStr1 by reference, it's not the default.
Try your code and its result is:
sometext

variable in session name

Is it possible to have a variable in a session name?
Like if I have a variable: $id
I want something like this:
$_SESSION['number'$id]
So that if $id=1
$_SESSION['number1']
And if $id=65
$_SESSION['number65']
Yes - you can do this.
All you have to do is concatenate a string value as the index in the $_SESSION array.
$id= 42;
$result = $_SESSION['number'.$id];
Now $result will be equal to the value of $_SESSION at the index "number42";
The same can be done for any associative array as well.
$any_assoc_array = array('index42'=>'Hooray!');
$id= 42;
$result = $any_assoc_array['index'.$id];
echo $result;
OUTPUT :
Hooray!
In PHP you can place variable names within a string literal if in double-quotes, so the following is perfectly legal. Note that the variable can appear anywhere in the string literal.
$result = $_SESSION["number$id"];

Getting object variable using string + variable

I would like to do something like this:
echo $myObject->value_$id but I don't know proper syntax and I'm not sure if it is possible.
$id is some PHP variable, for example has value 1. In the end, I would like to get $myObject->value_1 but the number part (1) should be dynamic.
The feature is called variable properties:
<?php
$myObject = (object)NULL;
$myObject->value_1 = 'I am value nr 1';
$id = 1;
echo $myObject->{"value_$id"};
This works:
$variableName = 'value_whatever_1337';
echo $myObject->$variableName;
$name = "value_" . $id;
echo $myObject->$name;

Making a variable out of another variables' value in php (php basics)

I need to make a dynamic variable based on a loop, what i am looking for is a variable something like
$var = "foo";
$$var = "bar";
echo $foo; // bar
but for me it should be more like a fixed parameter attached to the dynamic part like
$var='123';
$'current_'.$$var=some value; // not correct syntax
echo $current_123 should give 'some value';
Use curly braces:
$${'current_' . $var} = $some_value;

Categories