Data in array does not come out the same - php

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

Related

How to get a string from requested array 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';

how to find a key and set the values to a variable in php array

I have the following array (print_r output) -
Array ( [Ds_Date] => 08%2F06%2F2018 [Ds_Hour] => 12%3A46 [Ds_SecurePayment] => 1 [Ds_Amount] => 1000 [Ds_Currency] => 978 [Ds_Order] => 180608104552 ........
How do I set the Ds_Amount key and value to a variable to use later?
thanks
Craig.
I think you should create your array this way:
<?php
$array = array(
"Ds_Date" => "08%2F06%2F2018",
"Ds_Hour" => " 12%3A46",
"Ds_SecurePayment" => "1",
...
);
?>
And then acces it like this:
$array["Ds_Date"]
Hope it helps...

Unexpected output when using PDO Fetch

I'm hoping somebody knows what is going on with this. I've never seen anything like this before. I'm trying to re-key the results from an indexed array to a keyed array using the value of 'code' in the results as it is a unique key. This happens in both PHP 5.4.16 and PHP 7.0.10
The following code produces very strange output:
$stmt = $conn->prepare( $sql );
$stmt->execute( $params );
$key = 'code';
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
print "Value of $key is '{$row['code']}'\n";
$rows[ ($row['code']) ] = $row;
}
This produces the following output:
'alue of code is '1286
] => Array
(
[code] => 1286
[EmployeeID] =>
[Name] =>
)
Notice how the value of $row['code'] is printed to STDOUT after the single quotes and the V in Value is cut off. This causes my re-keyed array ($rows) to have no key value. I've re-keyed arrays from database results thousands of times before and never had a problem until I started using PDO. Anybody have any ideas?
I simplified the quoting by issuing:
print "Value of $key is '"."{$row[$key]}"."'\n";
which resulted in:
'alue of EmployeeCode is '1286
I am mystified why it's cutting off the 'V' in Value.
I can get similar output without using any database calls this way:
$row = [
'code' => "1286\r",
'EmployeeID' => '',
'Name' => ''
];
print "Value of $key is '{$row['code']}'\n";
$rows[ ($row['code']) ] = $row;
print_r($rows);
Output:
'alue of is '1286
Array
(
] => Array
(
[code] => 1286
[EmployeeID] =>
[Name] =>
)
)
The \r character causes the output cursor to jump to the start of the line, so the ' that's overwriting your 'V' character is actually the second single-quote you're printing.
Likewise, in the strange output of print_r(), the \r is jumping the output cursor to the start of the line, so the ] => Array is overwriting what came before it, which was [1286\r.
I suspect the data in your database contains literal \r characters.

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

Retrieve first key in multi-dimensional array using PHP

I would like to retrieve the first key from this multi-dimensional array.
Array
(
[User] => Array
(
[id] => 2
[firstname] => first
[lastname] => last
[phone] => 123-1456
[email] =>
[website] =>
[group_id] => 1
[company_id] => 1
)
)
This array is stored in $this->data.
Right now I am using key($this->data) which retrieves 'User' as it should but this doesn't feel like the correct way to reach the result.
Are there any other ways to retrieve this result?
Thanks
There are other ways of doing it but nothing as quick and as short as using key(). Every other usage is for getting all keys. For example, all of these will return the first key in an array:
$keys=array_keys($this->data);
echo $keys[0]; //prints first key
foreach ($this->data as $key => $value)
{
echo $key;
break;
}
As you can see both are sloppy.
If you want a oneliner, but you want to protect yourself from accidentally getting the wrong key if the iterator is not on the first element, try this:
reset($this->data);
reset():
reset() rewinds array 's internal
pointer to the first element and
returns the value of the first array
element.
But what you're doing looks fine to me. There is a function that does exactly what you want in one line; what else could you want?
Use this (PHP 5.5+):
echo reset(array_column($this->data, 'id'));
I had a similar problem to solve and was pleased to find this post. However, the solutions provided only works for 2 levels and do not work for a multi-dimensional array with any number of levels. I needed a solution that could work for an array with any dimension and could find the first keys of each level.
After a bit of work I found a solution that may be useful to someone else and therefore I included my solution as part of this post.
Here is a sample start array:
$myArray = array(
'referrer' => array(
'week' => array(
'201901' => array(
'Internal' => array(
'page' => array(
'number' => 201,
'visits' => 5
)
),
'External' => array(
'page' => array(
'number' => 121,
'visits' => 1
)
),
),
'201902' => array(
'Social' => array(
'page' => array(
'number' => 921,
'visits' => 100
)
),
'External' => array(
'page' => array(
'number' => 88,
'visits' => 4
)
),
)
)
)
);
As this function needs to display all the fist keys whatever the dimension of the array, this suggested a recursive function and my function looks like this:
function getFirstKeys($arr){
$keys = '';
reset($arr);
$key = key($arr);
$arr1 = $arr[$key];
if (is_array($arr1)){
$keys .= $key . '|'. getFirstKeys($arr1);
} else {
$keys = $key;
}
return $keys;
}
When the function is called using the code:
$xx = getFirstKeys($myArray);
echo '<h4>Get First Keys</h4>';
echo '<li>The keys are: '.$xx.'</li>';
the output is:
Get First Keys
The keys are: referrer|week|201901|Internal|page|number
I hope this saves someone a bit of time should they encounter a similar problem.

Categories