PHP multidimetional array null check - object

i have the following value passed to me via a webservice
print_r($result);
stdClass Object (
[array] => Array ( [0] => [1] => )
)
i break it down as follows
$result = $array->return;
foreach ($result as $val2)
{
$temp = $result[$i]->array[0];
$temp .= " - ". $result[$i]->array[1];
}
I want to check if the array is empty (as it is above). but i cant access the array via the
$result[$i]->array[0];
as i get Fatal error:
Cannot use object of type stdClass as array
What is the best way to check it?

stdClass is not an array, it is an object. But you access it like an array:
$result[$i]
^^^^
Shouldn't it be something like (without the foreach):
$array = $result->array;
$temp = vsprintf('%s - %s', $array);
UPDATE:
So to test if it is empty, you can just use
if (empty($result->array[0]))
....

Related

How do I access content from JSON string?

I am receiving a JSON object from the backend now I just want "result" array only in my template variable in my angular application from it.
{
"result":[
{"name":"Sunil Sahu",
"mobile":"1234567890",
"email":"abc#gmail.com",
"location":"Mumbai",
"Age":"19"
}
],
"status":200
}
Try with
variable_name["result"].
Try with
var data = response from the backend
var result = data.result;
$var = '{"result":[{"name":"Sunil Sahu","mobile":"1234567890","email":"abc#gmail.com","location":"Mumbai","Age":"19"}],"stats":200}';
If your $var is string, you need to turn it to "array" or "object" by json_decode() function
object:
$var_object = json_decode($var); //this will get an object
$result = $var_object->result; //$result is what you want to get
array:
$var_array = json_decode($var, true); //this will get an array
$result = $var_array['result']; //$result is what you want to get
Else if $var is object, direct use
$result = $var->result; //$result is what you want to get
As result is an array of objects, you can either use any loop to extract key value pair or you can directly access the array using index value.
var results = data["result"] // this would return an array
angular.forEach(results, function(value, key) {
//access key value pair
});
For accessing results in HTML, ng-repeat directive can be used.
Your question didn't explain further, but in the simple way try this :
const stringJson = `{
"result":[
{"name":"Sunil Sahu",
"mobile":"1234567890",
"email":"abc#gmail.com",
"location":"Mumbai",
"Age":"19"
}
],
"status":200
}`
const obJson = JSON.parse(stringJson);
console.log(obJson.result);

Puppet templates, defined types, and variable scope

I'm trying to get my head wrapped around why this happening. The following code will not write the variable $group, but it will write the array $users in the template.
define bar::foo(
String $group = $title,
Array $users
) {
file {'/tmp/my.file':
ensure => file,
content => epp('bar/test.epp'),
}
}
using an epp template that looks like this:
group: <%= $group %>
users: [<%= $users.map |$x|{ "'${x}'"}.join(', ') %>]
If I pass though the variable to the template, then $group gets written to 'my.file'.
define bar::foo(
String $group = $title,
Array $users
) {
file {'/tmp/my.file':
ensure => file,
content => epp('bar/test.epp', { group => $group }),
}
}
How can the variable $users be present in the template's scope, while $group is not in the first example?
If you are using epp and would like to get the $groups variable then you have to write the full scope of the variable $::bar::foo::groups inside your template.

Error: Undefined index: HTTP_X_FORWARDED_FOR

I have written this code
$ips = preg_split("/,/", $_SERVER["HTTP_X_FORWARDED_FOR"]);
$ip = $ips[0];
if ($key === $ip && $val === env('SERVER_ADDR')) {
$addr = env($ip);
if ($addr !== null) {
$val = $addr;
}
}
But I am getting following error:
<b>Notice</b>: Undefined index: HTTP_X_FORWARDED_FOR
Just dont use array keys without knowing for sure they always exist..
basic PHP one o' one
if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
// now only try to access this key
}
The alternative in Cake is to use wrapper methods that are designed to automatically check on the existence internally. Then you can just read the value directly. In your case env() checks on those server vars:
$result = env('HTTP_X_FORWARDED_FOR');

Kohana 3 validation rule error - Illegal offset type in isset or empty

I am trying to validate file, but I get following error: Illegal offset type in isset or empty . What I am doing wrong ?
$array = Validate::factory($_FILES);
$array->rule($_FILES['image'], 'Upload::not_empty');
if ($array->check())
{
$directory = DOCROOT.'uploads/';
$filepath = Upload::save($_FILES['image'], 'SDFFasreixcsd.jpg', $directory);
}
1 Use Validation not Validate
2. Pass the file name as first argument
$validation = Validation::factory($_FILES)->rule('image', 'Upload::not_empty');
if ($validation->check())
{
// Your code
}

How to pass multiple arguments to views_embed_view?

I have a form in .module file. In the form submit button I am embedding my view using views_embed_view function. I want to pass multiple arguments to the view.
Here is my code
print views_embed_view('testing_signup_info', 'default', '1,2,3');
The above code works fine and three arguments are passed to the view but i want to get sid from signup_log table and pass them as the arguments to the view.
Here is my code.
$result = db_query("SELECT sid from signup_log");
$rows = array();
while($row = db_fetch_array($result)) {
$r = $row['sid'];
$rows[$r] = $row['sid'];
drupal_set_message($r);
}
drupal_set_message(views_embed_view('testing_signup_info', 'default', '"' . $rows .'"'));
but here my view does not display.
I need help if someone know the solution.
How to pass sids retrieving from the table and pass as arguments to the view???
The snippet you included appears to pass in an array. Based on my understanding from this comment by merlinofchaos (the author of Views), it doesn't look like Views expects an array to be passed in. Try the following instead:
$result = db_query("SELECT sid from signup_log");
$rows = array();
while($row = db_fetch_array($result)) {
$r = $row['sid'];
$rows[$r] = $row['sid'];
drupal_set_message($r);
}
$rows_string = implode("+", $rows);
drupal_set_message(views_embed_view('testing_signup_info', 'default', $rows));

Resources