Dont display varaibile if it contains no value (PHP| - php

I am using SOAP to get some data from other service, i get XML and convert and display it with PHP. The thing is sometimes there is no value for it in XML file and i dont know how to display some message like "This thing was not set", instead of that i am just getting notice with
Notice: Array to string conversion in \data2.php on line 636
Array
What i tried:
if(isset($claim ['vehicle']['engine-cc']))
echo var_dump($claim ['vehicle']['engine-cc']);
output: array(0) { }
After that i try something like:
if(isset($claim ['vehicle']['engine-cc']))
echo var_dump(isset($claim ['vehicle']['engine-cc']));
Output: bool(true)
So it looks i am doing things bad, can you gimme some advise how can i fix it?
p.s. I know i can stop displaying errors and notices but its not the way how i want to "fix" it

you no need to echo after var_dump already have var_dump(used for printing array) or may be you have not an array for $claim ['vehicle']['engine-cc'] it's a string
var_dump($claim ['vehicle']['engine-cc']); // for array
echo $claim ['vehicle']['engine-cc'] // for string
returning bool(true) cause you have isset()(will return boolean true/false)
var_dump(isset($claim ['vehicle']['engine-cc']));
For printing array values you only need :-
var_dump($claim ['vehicle']['engine-cc']); or var_dump($claim);
will return you complete array output
for any check empty values try
if(!empty($claim ['vehicle']['engine-cc']))
var_dump($claim ['vehicle']['engine-cc']);

You can do it by simply echo ing the message.
`if( ! isset($claim ['vehicle']['engine-cc']))
echo 'engine-cc - This thing was not set';`

You need to use option "SOAP_SINGLE_ELEMENT_ARRAYS" most likely.
By default SOAP returns empty or a single value for a SOAP attribute as a string, not an array. Adding option SOAP_SINGLE_ELEMENT_ARRAYS into SOAP client request would force to return the value as an empty ARRAY rather than empty string. See example here.

Related

json_encode Returning a PHP Array

So I am working with PHP to pass a PHP array over a jQuery Ajax request to another PHP page. This is quite the task. For some reason, my json_encode is returning an array instead of a string, I am not quite sure why. Here is my PHP code:
$new_spreadsheet = nl2br($_POST['spreadsheet']);
$new_spreadsheet = explode('<br />', $new_spreadsheet);
array_shift($new_spreadsheet);
$new_spreadsheet = array_values($new_spreadsheet);
echo json_encode($new_spreadsheet);
I would show you the output, but it is really long. Basically this is outputting a PHP array which consists of each row on the spreadsheet. This is what I want to have, but the problem is that I don't want the quotes and everything in the array. I am pretty sure I need to run json_decode, but when I do that my code returns an error saying that the parameter needs to be a string. I can tell something is not right, but am not quite sure what I need to change. I would appreciate any advice.
Update: At this point, when I try to loop through the array and print each value, the first array index is equal to a double quote like so: ". There are double quotes in random values throughout the area. I am not quite sure about what is causing this.
If I echo the rows from within the json_encoded PHP array onto the console, I get the following output:
"
correct value
correct value
correct value
"
You're using JSON, which means you have to adhere to somewhat more stringent syntax rules than Javascript's. e.g.
<?php
$arr = array('This' => 'is', 'an' => 'array in php');
echo json_encode($array);
?>
output:
{"This":"is","an":"array in PHP"}
There is NO way to avoid getting quotes on the values, as they're a fundamental requirement of JSON (and Javascript). If you don't want quotes, then don't use JSON.
try only br.
$new_spreadsheet = explode("<br>", $new_spreadsheet);
It will work. and json_enode can never return an array. try var_dump and check.Also make sure before you post, use htmlspecialcharacters.

var_dump not printing the integer values

I am trying to read some values from the membase.
I observer that when there is any integer the following command is not working.
var_dump($memcache->get("keyset123"));
print_r($memcache->get("keyset123"));
If the get result is a string the above command prints.
If the get result is a Integer the above commands are printing none.
vardump prints =string(0) ""
print_r prints none.
can you please tell me what is the issue
That is because the $memcache->get() call is returning a string value. Your problem lies elsewhere (likely deeper within the code in use), not within var_dump().
Look into what you're storing within whatever is inside of the variable $memcache.
var_dump($memcache->get("keyset123"));
//outputs
//string(0) ""
Memcached is storing an empty string at the key "keyset123", otherwise you would be getting FALSE (key doesn't exist) or NULL (key exists, but no value exists)

Variable in $_POST returns as string instead of array

In my form i have fields with name photoid[] so that when sent they will automatically be in an array when php accesses them.
The script has been working fine for quite some time until a couple days ago. And as far as i can remember i havent changed any php settings in the ini file and havent changed the script at all.
when i try to retrieve the array using $_POST['photoid'] it returns a string with the contents 'ARRAY', but if i access it using $_REQUEST['photoid'] it returns it correctly as an array. Is there some php setting that would make this occur? As i said i dont remember changing any php settings lately to cause this but i might be mistaken, or is there something else i am missing.
I had the same problem. When I should recieve array via $_POST, but var_dump sad: 'string(5) "Array"'. I found this happens, when you try use trim() on that array!
Double check your code, be sure you're not doing anything else with $_POST!
Raise your error_reporting level to find any potential source. It's most likely that you are just using it wrong in your code. But it's also possible that your $_POST array was mangled, but $_REQUEST left untouched.
// for example an escaping feature like this might bork it
$_POST = array_map("htmlentities", $_POST);
// your case looks like "strtoupper" even
To determine if your $_POST array really just contains a string where you expected an array, execute following at the beginning of your script:
var_dump($_POST);
And following for a comparison:
var_dump(array_diff($_REQUEST, $_POST));
Then verifiy that you are really using foreach on both arrays:
foreach ($_POST["photoid"] as $id) { print $id; }
If you use an array in a string context then you'll get "Array". Use it in an array context instead.
$arr = Array('foo', 42);
echo $arr."\n";
echo $arr[0]."\n";
​
Array
foo
$_POST['photoid'] is still an array. Just assign it to a variable, and then treat it like an array. ie: $array = $_POST['photoid'];
echo $array[0];

Cookie Value not available, why?

I have tested this on my development computer, but now I have uploaded everything to the production server and I cant read out the value of the cookie.
I think the problem lies in the Serialization and Unserialization.
if (isset($_COOKIE['watched_ads'])){
$expir = time()+1728000; //20 days
$ad_arr = unserialize($_COOKIE['watched_ads']); // HERE IS THE PROBLEM
$arr_elem = count($ad_arr);
if (in_array($ad_id, $ad_arr) == FALSE){
if ($arr_elem>10){
array_shift($ad_arr);
}
$ad_arr[]=$ad_id;
setcookie('watched_ads', serialize($ad_arr), $expir, '/');
}
}
When I echo this: count($ad_arr) I receive the expected nr, 1 in this case, so there is a value there. But when I echo the value: echo $ad_arr[0]; I get nothing. Completely blank. No text at all.
Anybody have a clue?
if you need more info about something let me know...
Turns out it was stripslashes which was needed here.
Did a stripslashes() first and it worked unserializing the cookie.
You should understand that count returns 1 for most non-array values, including an empty string.
> php
<?php
echo count("");
^Z
1
So, to debug it, try var_dump'ing the $_COOKIE superglobal itself.
I would guess, that your $ad_arr is no array. You can check this with the "is_array()" function or by calling:
var_dump($ad_arr);
It sould have "array" in the output and not "string" (as Artefacto just already mentioned).
Another little tip:
If you want to store a associative array, you can use an encoded JSON String which use can save using the json_encode() gunction:
setcookie('watched_ads', json_encode($ad_arr), $expir, '/');
And loading the data you can use the opposite function json_decode():
$ad_arr = json_decode($_COOKIE['watched_ads'], true);
Adding true as a second paramter, you will get a associative array and not an object. Using the JSON format is also a good idea saving complex data within a database.
A and a last tip: "!in_array()" works just as fine as "in_array() == FALSE"

outputting all array values to file with php

Im working with a foreign API and im trying to get a fix on what all its sending to my file with the GET method.
How can i output something like print_r($_GET) to a file so i can read what all its sending?
If you have a hash, both of the listen solutions won't give you the keys. So here's a way to get your output formatted by print_r:
$var = print_r($your_array, 1);
file_put_contents("your_file.txt",$var);
The second option of print_r is boolean, and when set to true, captures the output.
It sounds like you need a log to store the $_GET variables being submitted to your script, for debug purposes. I'd do something like this appending values to the end of the file, so the file is not overwritten every request:
file_put_contents('path_to_log.txt', print_r($_GET, true), FILE_APPEND);
Writing to a file:
You could use file_put_contents() to create the file with your output. This function will accept a string, or an array, which releases you of the burden to convert your array into a writable-format.
file_put_contents("myget.txt", $_GET);
As stated in the comments, this option isn't ideal for multidimensional arrays. However, the next solution is.
Maintaining format:
If you'd like to maintain the print_r() formatting, simply set the second parameter to true to return the value into a variable rather than outputting it immediately:
$output = print_r($_GET, true);
file_put_contents("myget.txt", $output);

Categories