How to get a string from requested array PHP - php

I have a file swifts.php with code (I have written this after a lot of research)
<?php
$swift= array(
'PBANUA2XXXX' => 'PRIVATBANK',
'Swift2' => 'word2',
'Swift3' => 'word3',
'Swift4' => 'word4',
'Swift5' => 'word5',
'etc' => 'word6',
'etc..' => 'word7',
);
echo http_build_query($swift) . "\n";
echo http_build_query($swift, '', '&');
?>
My question is how to receive a string for example when I request the Swift3 with https://example.com/swifts.php?swift=Swift3
I want to receive in a page just the string word3 but it shows me all the arrays like: PBANUA2XXXX=PRIVATBANK&Swift2=word2&Swift3=word3& etc etc....
How I can get what I request?

You have to get query string by using $_GET.
Then use your array to get value
echo $swift[$_GET['swift']]; // as $_GET['swift'] = Swift3
so $swift[$_GET['swift']] = 'word3';

Related

Data in array does not come out the same

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

Getting a value from associative arrays PHP

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

PHP associative 2 dimensional array of string recources

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

using extract in php to print array value

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.

Decode URL into an array rather than a string

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

Categories