Number after print_r output? [duplicate] - php

This question already has answers here:
What does "1" mean at the end of a php print_r statement?
(3 answers)
Closed 5 years ago.
I have the following PHP code:
<?php
$array = ["test", "1", "2", "3"];
$id = 0;
echo "ID: 1 - <pre>", print_r($array), "</pre><br/>";
When I execute the code, the output is the following:
ID: 1 - Array
(
[0] => test
[1] => 1
[2] => 2
[3] => 3
)
1<br/>
If you look closely, you will notice a number after the array output of print_r. Why is this number showing up and is there a way to stop it from showing up?

Calling print_r() without a second parameter outputs the value and then returns true - which is the 1 your getting in the output. You probably want to use print_r($array, true) to get it to return the value as a string and then be able to wrap it in the HTML tags you want.

print_r() without it's second argument will return boolean true, which turns to "1" when converted to a string.
Add true as the second parameter to make print_r return it's output.

Related

What does the number outside an array printed with print_r mean? [duplicate]

This question already has answers here:
What does "1" mean at the end of a php print_r statement?
(3 answers)
Closed 5 years ago.
Given the following code:
<?php
$array = [1,2,3,[4,34]];
echo print_r($array);
?>
It results in the following output in the browser:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => Array ( [0] => 4 [1] => 34 ) ) 1
What does the 1 outside the array, at the end of the output attempting to display? Tried searching the PHP docs, but apparently no such number appeared at the end of their example.
The 1 you see is the truthy value of the print_r() function.
Because print_r() is a function in itself, it evaluates to truthy.
In your example, print_r() runs and outputs your array, followed by echo, which echoes out the truthy value of print_r().
To eliminate this erroneous 1, simply remove the echo from your code (which isn't needed, as print_r() outputs to the DOM by itself):
<?php
$array = [1,2,3,[4,34]];
print_r($array);
?>
Hope this helps :)

change array value in php through array place [duplicate]

This question already has answers here:
Deleting an element from an array in PHP
(25 answers)
Closed 5 years ago.
How to change or update array value in PHP through array number?
Example:
$array1 = array("cat","dog","mouse","dog");
I want to change the value of dog in the 2nd array only
Use Variable variables:
$a = 'array' . '1';
$$a[1] = 'doggg'; #value changed
$b = 'array' . '2';
$$b[1] = 'newDogg'; #value changed
if you want to remove 2 index then
$array1 = array('dog','mouse','cat','mouse');
unset($array1[1]);
print_r($array1);
Output
Array ( [0] => dog [2] => cat [3] => mouse )
//if you are looking for remvoing duplicate value then you can use array_unique
$result=array_unique($array1);
print_r($result);
Output
Array ( [0] => dog [1] => mouse [2] => cat )
If you mean you have to remove an element from an array, probably this will help.
array_splice( $array1, OFFSET, 1 );
replace OFFSET with the index you want to remove.
Edit :-
To delete element use unset(arrayVar[key])
For eg :-
unset($array1[1]) - will delete the "mouse" at key/index 1 if you wish to delete 2nd occurance then use unset($array1[3])
Original :-
Explain your problem in more detail please !
What do you mean by 2nd array and where exactly is it ?
Do you mean to edit the 2nd occurance of "dog" in array ?
If so then it would be $array1[3]="someNewValue"

How to convert array into JSON object in php [duplicate]

This question already has answers here:
Converting MySQL result array to JSON [duplicate]
(2 answers)
Closed 6 years ago.
I want to convert my array value:
Array ( [page_1] => fifth [page_2] => first [page_3] => fourth [page_4] => third )
Into JSON format is given below
{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}
Can anyone please help me
You want to serialize you array.
You need to use serialize()
<?php
$a = array (
'page_1' => 'fifth',
'page_2' => 'first',
'page_3' => 'fourth',
'page_4' => 'third');
echo serialize($a);
// Outputs: a:4:{s:6:"page_1";s:5:"fifth";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"third";}
?>
$json = json_encode($array);
and otherwise
$array = json_decode($json, true);
When I insert the value in table it is inserting like
s:107:"a:4:{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}";
don't know why cause when i display it that is right but in table it is inserting something like above

Key value in PHP from URL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to explode URL parameter list string into paired [key] => [value] Array?
Quick question
I have a URL sting that I wish to output the Key value as an array eg
$url ="www.domain.com?test=1&test2=2&test3=3";
and wish to have the output as an array
key => value so I can call any of the keys
eg
array (
test => 1,
test1 => 2,
test2 => 3,
)
cant use explode &
Just thinking do i have to do a loop and match between & and = for the key
I would use parse_url() with parse_str():
$url = parse_url('www.domain.com?test=1&test2=2&test3=3');
parse_str($url['query'], $keyvalue);
var_dump($keyvalue);
$keyvalue should contain your desired array.

Converting string with keys and values to array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
unserialize problem
I have a string in the form of:
a:16:{i:0;s:3:"696";i:1;s:3:"698";i:2;s:3:"690";}"
I am looking at turning this back into array, so that it will be along the lines of:
array(16) {
0 => 696,
1 => 698,
2 => 690
}
Any ideas how to do this?
Thanks
It looks like a serialized PHP string, try
$array = unserialize($value);
Manual: http://php.net/manual/en/function.unserialize.php
Update
The string contains a flaw, as it expects a array of 16 elements, but only 3 given.
Consider:
$a = array (
0 => '696',
1 => '698',
2 => '690'
);
$s = serialize($a);
will result in:
"a:3:{i:0;s:3:"696";i:1;s:3:"698";i:2;s:3:"690";}"
Use the unserialize() function.
$array = unserialize($serialized_string);

Categories