I have an array which I create. When reading the array with print_r it is not returning with the correct data inputted! I am missing specific sectionsn such as < & > brackets with its headings.
How can i preserve these?
Code:
$params = array(
"Parm1" => "test",
"Parm2" => "hi",
"Parm3" => GUID(),
"Parm4" => "lol",
"Parm5" => "
<R>
<R1>the</R1>
<R2>dog</R2>
<R3>is</R3>
<R15>happy</R15>
<R20>today</R20>
</R>
");
Basically the only data that is jumbled up is the Parm5 section. I want everything inside to return exactly as it is! EG: Reading as is i only receive Array ( [Parm1] => test [Parm2] => hi [Parm3] => B18BE727-8F79-4D4A-80EA-3974B1429F78 [Parm4] => lol [Parm5] => the dog is happy today ) from print_r
I want to return:
Array ( [Parm1] => test [Parm2] => hi [Parm3] => B18BE727-8F79-4D4A-80EA-3974B1429F78 [Parm4] => lol [Parm5] => <R><R1>the</R1> <R2>dog</R2> <R3>is</R3> <R15>happy</R15> <R20>today</R20></R> )
escape param5 with htmlspecialchars('<R> ..... </R>'). Your browser currently sees it as html tags and parses it.
Use htmlspecialchars on the return value of print_r:
echo "<pre>";
echo htmlspecialchars(print_r($params, true));
echo "</pre>";
I have an array..
$file=array(
'uid' => '52',
'guarantee_id' => '1116',
'file_id' => '8',
'file_category' => 'test',
'meta' =>'{"name":"IMAG0161.jpg","type":"image\/jpeg","tmp_name":"\/tmp\/phpzdiaXV","error":0,"size":1749244}',
'FileStorage' => array()
)
and I am trying to extract the name using
$fileName = $file['meta'['name'];
which gives me a Illegal string offset 'name' error.
The value of $file['meta'] is a string, not an array. That means your approach to access the value does not work.
It looks like that meta value string is a json encoded object. If so you can decode it and then access the property "name" of the resulting object.
Take a look at this example:
<?php
$file = [
'uid' => '52',
'guarantee_id' => '1116',
'file_id' => '8',
'file_category' => 'test',
'meta' =>'{"name":"IMAG0161.jpg","type":"image\/jpeg","tmp_name":"\/tmp\/phpzdiaXV","error":0,"size":1749244}',
'FileStorage' => []
];
$fileMeta = json_decode($file['meta']);
var_dump($fileMeta->name);
The output obviously is:
string(12) "IMAG0161.jpg"
In newer version of PHP you can simplify this: you do not have to store the decoded object in an explicit variable but can directly access the property:
json_decode($file['meta'])->name
The output of this obviously is the same as above.
This is happening because your meta is a json, so you should decode and then access whatever you need, not that I placed true as second parameter becuase i wanted to decode as an associative array instead of an object
$decoded = json_decode($file['meta'],true);
echo $decoded['name'];
//print IMAG0161.jpg
You can check a live demo here
But you can easily access as an obect
$decoded = json_decode($file['meta']);
echo $decoded->name;
//print IMAG0161.jpg
You can check a live demo here
<?php
$file=array(
'uid' => '52',
'guarantee_id' => '1116',
'file_id' => '8',
'file_category' => 'test',
'meta' =>'{"name":"IMAG0161.jpg","type":"image\/jpeg","tmp_name":"\/tmp\/phpzdiaXV","error":0,"size":1749244}',
'FileStorage' => array()
);
$meta=$file['meta'];
$json=json_decode($meta);
echo $json->name;
?>
Below is my code:
<?php
$text_res = array(
'eng' => array('chapter' => 'Chapter'),
'rus' => array('chapter' => 'Глава')
);
echo $text_res['eng']['chapter'];
?>
why is it printing empty string?
This particular code works. The question was asked not quite correctly. See the comments
I'm using extract passing it an associative array $arr but it doesn't work properly. I pass the array to my function extract($arr) but when I want to extract value it doesn't work. how can use it correctly?
here my array:
$arr = array('id' => 1, 'name' => 'abc','address' => 'street abc');
If you're using it like this it should work.
<?php
$arr = array('id' => 1, 'name' => 'abc','address' => 'street abc');
extract($arr);
echo $id . "\n";
echo $name . "\n";
echo $address;
?>
Output:
1
abc
street abc
This code shows the output fine. Post your exact code so that we can reproduce the problem.
I am currently working with PayPals API and want to transform one of its response from a name-value pair to an array.
So far I have used urldecode() to decode the response to the following:
RECEIVERBUSINESS=foo#bar.com&RECEIVEREMAIL=another#email.com&MOREINFO=lots more info`
What I would like is to have the following:
RECEIVERBUSINESS => 'foo#bar.com'
RECEIVEREMAIL => 'another#email.com'
MOREINFO => 'lots more info'
I'm just not quite sure how to get there!
parse_str is what you're looking for:
parse_str('RECEIVERBUSINESS=foo#bar.com&RECEIVEREMAIL=another#email.com&MOREINFO=lots more info', $arr);
/*
print_r($arr);
Array
(
[RECEIVERBUSINESS] => foo#bar.com
[RECEIVEREMAIL] => another#email.com
[MOREINFO] => lots more info
)
*/
Look into explode -
// poulate array from URL parameters
$returnedInfo = explode('&', $dataStringFromUrl);
http://us.php.net/explode