On my php page, I am getting the follow output:
Array ( [contact/email] => users_name#email_address.com )
This is produced via the following line in the php code:
print_r($openid->getAttributes());
How do I extract the text users_name#email_address.com from that array and stick it into a variable $strEmail;?
So when I echo the variable:
echo $strEmail;
It should output the following on screen:
users_name#email_address.com
Assign the array to a variable and then you can easily access it:
$attributes = $openid->getAttributes();
$strEmail = $attributes['contact/email'];
echo $strEmail; // => users_name#email_address.com
In your specific case, you can do:
$strEmail = reset($openid->getAttributes());
However it's better to suggest as this will work for other cases,too:
$attributes = $openid->getAttributes();
$strEmail = $attributes['contact/email'];
For more details see the PHP Manual about arrays how to access them.
Related
In PHP i have array variable from another function like this
$v->params:
(
[{"username":"myusername","email":"myemail#gmail_com","phone":"0123456789","password":"abc123","fullname":"myfullname","register_ip":"127_0_0_1","country":"Qu\u1ed1c_Gia","birthday":"N\u0103m_sinh","gender":"male","bank_code":"Ng\u00e2n_h\u00e0ng","ip":"127_0_0_1","os":"Windows_10","device":"Computer","browser":"Mozilla_Firefox_77_0"}] =>
)
Now i want to access to it item, how can i code to access item value like this:
$password = $v->params->password; //myemail#gmail_com
I new with PHP thank you all
The data seems the wrong way round as it's the key of the array rather than a value.
So using array_keys()[0] to get the first key and then json_decode this...
$data = json_decode(array_keys($v->params)[0]);
you can then use the $data object to get at the values...
echo $data->username;
I am beginner to laravel. I am trying to access array input from my API request.
For postman, I have array as key called file_name_list and its value like ["m_profile.png","aa_image.jpg","new_pan.jpg"]. I want to access that array in my controller. That values should go into 3 seperate variables like
$profile = m_profile.png
$aadhar = aa_image.jpg
$pan = new_pan.jpg
For that I am trying to use replace and explode functions in controller.
$filenamelist1 = Str::replaceArray(array(' ','"', '[',']'),[""], $request->file_name_list);
$filename_str = explode(",",$filenamelist1);
After this I want to store values from explode array to 3 variables as mentioned above using for loop
But I am facing problems like in Str::replaceArray 2 parameter should be array and for explode 2 parameter should be string.
How should I use replace and explode to get required result? please guide. Thanks in advance.
You can use list method like below:
list( $profile , $adhar, $pan) = $request->file_name_list;
Check the Docs
If Your array size fixed then you assign that by using list
list( $profile,$adhar,$pan) = $request->file_name_list;
<?php
$value = '["m_profile.png","aa_image.jpg","new_pan.jpg"]';
$value = str_replace("[","",$value); // clean [
$value = str_replace("]","",$value); // clean ]
$arrayValues = explode('","',$value); // explode with ","
print_r($arrayValues);
output
Array (
[0] => "m_profile.png
[1] => aa_image.jpg
[2] => new_pan.jpg"
)
Replace " when you access the values
Hi I'm using a simple import to CSV script that brings information into an array from a CSV file.
The sample output of the print_r($data) statement is:
Array ( [0] => Array ( [Email,Name,"Message Number","Date Added", ...etc ]) => email#email,com,myname,1001,"10/27/16 9:54pm EDT",... etc ) 1 => Array ( [Email,Name,"Message Number","Date Added",
print_r($data[0])
shows Array ( [Email,Name,"Message Number","Date Added",
I'm trying to isolate the variables but each time I get a null result on the quoted values.
$csv="members.csv";
$importer = new CsvImporter($csv,true);
$data = $importer->get();
echo "<pre>";
$n=0;
$messageNumber = $data[$n]['Message Number'];
What am I doing wrong? It should return '1001' in that variable.
Note: I'm using the class CsvImporter on this page in PhP FgetFSV
Again, what I'm looking for is the definition to isolate an individual variable in an easy way.
Thank you!
Perhaps someone has a simpler way to read a CSV into a PHP Array than using the above class?
In the example given in the php docs for CsvImporter it's changing the default delimiter to \t. Try add ',' as the 3rd param for the class:
$importer = new CsvImporter($csv,true, ',');
Hope this helps!
$csv="members.csv";
$importer = new CsvImporter($csv,true, ',');
$data = $importer->get();
echo "<pre>";
$n=0;
$messageNumber = $data[$n]['Message Number'];
print $messageNumber;
Returns the correct '1001' as expected.
The solution is marked above.Thanks to Ross. Simply changing the $importer = new CsvImporter($csv,true, ','); solved the entire puzzle.
I'm trying to build URL query from an Array that looks like that:
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
I would like to get query like that:
localhost/get?serial=3804689&serial=3801239&serial=3555689
(you get the idea)
I'm trying to use http_build_query($serials, 'serial', '&'); but it adds the numeric index to the prefix 'serial'.
Any idea how to remove that numeric index?
Well you can't really have the same GET parameter in the string, After all if you try to access that variable server side, what would you use?
$_GET['serial'] - But which serial would it get?
If you really want to get a list of serials, Simply turn the array into a string, save it as an array and there you go. for example :
$serials = "string of serials, delimited by &";
Then you can use the http build query.
Maybe use a foreach:
$get = "localhost/get?serial=" . $serials[0];
unset( $serials[0] );
foreach( $serials AS serial ){
$get .= "&serial=$serial;
}
Just as an FYI, PHP doesn't handle multiple GET variables with the same name natively. You will have to implement something fairly custom. If you are wanting to create a query string with multiple serial numbers, use a delimiter like _ or -.
Ex: soemthing.com/serials.php?serials=09830-20990-91234-12342
To do something like this from an array would be simple
$get_uri = "?serial=" . implode("-", $serials);
You would be able to get the array back from a the string using an explode to
$serials = explode("-", $_GET['serials']);
Yes its quite possible to have such format, you have to build it query string by indices. Like this:
No need to build the query string by hand, use http_build_query() in this case:
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$temp = $serials;
unset($serials);
$serials['serial'] = $temp;
$query_string = http_build_query($serials);
echo urldecode($query_string);
// serial[0]=3804689&serial[1]=3801239&serial[2]=3555689&serial[3]=3804687&serial[4]=1404689&serial[5]=6804689&serial[6]=8844689&serial[7]=4104689&serial[8]=2704689&serial[9]=4604689
And then finally, if you need to process it somewhere, just access it thru $_GET['serial'];
$serials = $_GET['serial']; // this will now hold an array of serials
You can also try this
$get = "localhost/get?serial=";
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$list = implode("&serial=",$serials);
echo $get.$list;
Having the same GET parameter will not work this way. You will need to use an array in the get parameter:
localhost/get?serial[]=3804689&serial[]=3801239&serial[]=3555689
IF you just print $_GET through this URL, you will receive
Array ( [serial] => Array ( [0] => 380468 [1] => 3801239 [2]=> 3555689) )
Then you can do this to build your query string
$query_str = 'serial[]='.implode('&serials[]=',$serials);
You can try this version.
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$get = implode('&serials[]=',$serials);
echo 'test';
var_dump($_GET['serials']);
Result
I'm using json_encode on an object and stored on a hidden text field, When i was passing to the next page i didn't get any data
Code:
$flight = json_encode($od->FlightSegments);
Response page:
<?php print_r($_POST); ?> printed Array ( [flight] => { )
serialize and unserialize not working on my object.
Can any one tell me what's going wrong ?
To see XML literally on the web page, use:
<?php echo '<pre>' . htmlentities(print_r($_POST, true)) . '</pre>'; ?>
Since serialize and unserialize are not giving me the correct output for me, I solved my problem using below code.
$count = 0;
$_SESSION['fl'] = [];
foreach($response->Response__Depart->OriginDestinationOptions->OriginDestinationOption as $od){
$flight = json_encode($od->FlightSegments);
$_SESSION['fl'][$count] = $flight;
}
Stored entire object into a session array