do not understand passing in arrays to a function - php

So, here is my code and I would like to understand how to get the array out.
function testArray($name, $ages){
$name;
$ages = array();
echo $name . $ages[0] . '<br>' . $ages[1];
}
testArray("michael", [29,45]);
I would like 'ages' to be an array. Is this the correct way to do it?

Pass the array as array:
<?PHP
function testArray($name, $ages){
//$name;
//$ages;
echo $name . $ages[0] . '<br>' . $ages[1];
}
testArray("michael", array(29,45));
?>

The problem with what you are doing is that you are rewriting your array. Instead of
function testArray($name, $ages){
$name;
$ages = array();
echo $name . $ages[0] . '<br>' . $ages[1];
}
testArray("michael", [29,45]);
use
function testArray($name, $ages){
$name;
echo $name . '<br />' . $ages[0] . '<br />' . $ages[1];
}
testArray("michael", [29,45]);
and if you get a parse error, use this:
function testArray($name, $ages){
$name;
echo $name . '<br />' . $ages[0] . '<br />' . $ages[1];
}
testArray("michael", array(29,45));

Related

How to properly get a return value from a PHP function?

The code snippet below generates '12290' from the second echo output but the third one is still zero.
How do I get a proper return value from mytest() function?
$testId = 0;
echo 'first: ' . $testId . '<br>';
function mytest($postId) {
if (get_post_type($postId) === 'artist') {
$testId = $postId;
}
echo 'second: ' . $testId . '<br>';
return $testId;
}
mytest(12290);
echo 'third: ' . $testId . '<br>';
You forgot to assign the return value to the variable again.
It should be this:
$testId = 0;
echo 'first: ' . $testId . '<br>';
function mytest($postId) {
if (get_post_type($postId) === 'artist') {
$testId = $postId;
}
echo 'second: ' . $testId . '<br>';
return $testId;
}
$testId = mytest(12290);
echo 'third: ' . $testId . '<br>';
Instead of:
mytest(12290);
echo 'third: ' . $testId . '<br>';
Try:
echo 'third: ' . mytest(12290) . '<br>';

How to get array from API

I want to get the episode number and Released from
http://www.omdbapi.com/?t=the+walking+dead&season=6&r=json
but I don't know how to extract arrays from the api.
Can someone help me?
My code is:
$title = 'the+walking+dead';
$title2 = urlencode($title);
$json=file_get_contents("http://www.omdbapi.com/?t=$title2&season=6&r=json");
$details=json_decode($json);
if($details->Response=='True')
{
$episodios=$details->Episodes;
}
$title = 'the+walking+dead';
$title2 = urlencode($title);
$json = file_get_contents("http://www.omdbapi.com/?t=$title2&season=6&r=json");
$details = json_decode($json);
if ($details->Response == 'True') {
echo 'There are ' . count($details->Episodes) . ' episodes<br />';
foreach ($details->Episodes as $key => $episode) {
echo 'Episode criteria number is ' . ($key + 1) . '<br />';
echo 'Episode Number: ' . $episode->Episode . '<br />';
echo 'Released: ' . $episode->Released . '<br />';
}
}
<?php
$title = 'the+walking+dead';
$title2 = urlencode($title);
$json=file_get_contents("http://www.omdbapi.com/t=$title2&season=6&r=json");
$details=json_decode($json);
if($details->Response=='True')
{
$episodios=$details->Episodes;
foreach ($episodios as $episode) {
echo 'Episode Number: ' . $episode->Episode . '<br />';
echo 'Released Date: ' . $episode->Released . '<br />';
}
}
?>

Last iteration of PHP foreach loop code change

I have a block of code thats working perfectly to pull data about different office locations.
What I would like to do is be able to make the last iteration of this loop change the div class to something else so I can apply a different set of css styles.
$fields = get_group('Offices');
foreach($fields as $field){
echo'<div class="oloc">';
if($locationVar==NULL || $locationVar!=$field['office-location'][1]) {
echo '<a name="' . strtolower(str_replace(' ', '-', $field['office-location'][1])) . '"></a><h3>' . $field['office-location'][1] . '</h3>';
$locationVar = $field['office-location'][1];
} else {
echo "<br />";
}
if($field['office-gm'][1]){
echo '<div class="gm"><img src="http://maps.googleapis.com/maps/api/staticmap?center=' . $field['office-gm'][1] . '&zoom=9&size=250x250&markers=color:blue|label:A|' . $field['office-gm'][1] . '&sensor=false"></div>';
}
if($field['office-name'][1]){
echo '<strong>' . $field['office-name'][1] . '</strong><br /><br />';
}
if($field['office-phone'][1]){
echo 'Phone: ' . $field['office-phone'][1] . '<br />';
}
if($field['office-fax'][1]){
echo 'Fax: ' . $field['office-fax'][1] . '<br />';
}
if($field['office-address'][1]){
echo '<br />Address:<br />' . strip_tags($field['office-address'][1], '<br><br />') . '<br />';
}
if($field['office-webpage'][1]){
echo 'Web: ' . 'Office Webpage<br />';
}
if($field['office-email'][1]){
echo 'Email: ' . 'Office Email<br />';
}
if($field['office-emp'][1]){
echo 'Jobs: ' . 'Employment Application<br />';
}
if($field['office-fb'][1]){
echo 'Facebook: ' . 'Facebook<br />';
}
if($field['office_office_twitter'][1]){
echo 'Twitter: ' . 'Twitter<br />';
}
echo '</div>';
}
For cases like these you can use a CachingIterator and the hasNext() method:
$fields = get_group('Offices');
$it = new CachingIterator(new ArrayIterator($fields));
foreach($it as $field)
{
...
if (!$it->hasNext()) echo 'Last:';
...
}
Put every echo in a var, this class definition in a other var. Then at the end of your foreach you check like so:
$i = 0;
foreach(...
if( $i == count($fields) ) { // change class }
Try this
$i = 0;
$total = count($fields);
$final = false;
foreach($fields as $field){
$i++;
$final = ($i + 1 == $total)? true : false;
if($final)
echo'<div class="NEW_CLASS">';
else
echo'<div class="oloc">';
....
}
First you should get the total count of the offices so that when you are on the last iteration, you can do something about it.
$fields = get_group('Offices');
$fields_count = count($fields);
$i = 0;
foreach ($fields as $field) {
$is_final = ++$i == $fields_count;
echo '<div class="oloc' . ($is_final ? ' oloc-final' : '') . '">';
[...]
}

Web service Array problem

I have this web service http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/Social/Login?Token=210029242357724|fd4eef8a839f24db2a9fedcd.1-100001001235070|Nro7dAY411DJRn7E8zB6MOXHjq8
And I'm having problems catching some values like:
clubId, clubName, clubLogo, relationType, and dateAdded.
I just don't know how to handle the array.
My code:
<?php
function getUserInfo() {
$json = file_get_contents('http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/Social/Login?Token=210029242357724|fd4eef8a839f24db2a9fedcd.1-100001001235070|Nro7dAY411DJRn7E8zB6MOXHjq8');
$data = json_decode($json, TRUE);
$v= $data['data'];
$_SESSION['userinfid'][] = $v['id'];
$_SESSION['userinfnickname'][] = $v['nickname'];
$_SESSION['userinfvisibility'][] = $v['visibility'];
$_SESSION['userinffirstname'][] = $v['first_name'];
$_SESSION['userinflastname'][] = $v['last_name'];
$_SESSION['userinfgender'][] = $v['gender'];
$_SESSION['userinfdialect'][] = $v['dialect'];
$_SESSION['userinfstatus'][] = $v['status'];
$_SESSION['userinfadmissiondate'][] = $v['admission_date'];
$_SESSION['userinflastaccess'][] = $v['last_access'];
$_SESSION['userinfusername'][] = $v['username'];
$_SESSION['userinfpoints'][] = $v['points'];
$_SESSION['userinfranking'][] = $v['ranking'];
$_SESSION['userinfsessionID'][] = $v['sessionID'];
$_SESSION['userinfpublicProfile'][] = $v['publicProfile'];
$_SESSION['userinfemail'][] = $v['email'];
$_SESSION['userinfmobile'][] = $v['mobile'];
$_SESSION['userinfimageURL'][] = $v['imageURL'];
$_SESSION['userinfclubURL'][] = $v['clubURL'];
$_SESSION['userinfcontact'][] = $v['contacts']['contact'];
$_SESSION['userinfcontactType'][] = $v['contacts']['contactType'];
$_SESSION['userinfisdefault'][] = $v['contacts']['is_default'];
$_SESSION['userinfclubId'][] = $v['clubs']['clubId'];
$_SESSION['userinfclubName'][] = $v['clubs']['clubName'];
$_SESSION['userinfclubLogo'][] = $v['clubs']['clubLogo'];
$_SESSION['userinfrelationType'][] = $v['clubs']['relationType'];
$_SESSION['userinfdateAdded'][] = $v['clubs']['dateAdded'];
}
getUserInfo();
echo 'IDClube: ' . $_SESSION['userinfclubId'][0] . '<br />';
echo 'NomeClube: ' . $_SESSION['userinfclubName'][0] . '<br />';
echo 'LogoClube: ' . $_SESSION['userinfclubLogo'][0] . '<br />';
echo 'RelationType: ' . $_SESSION['userinfrelationType'][0] . '<br />';
echo 'DataAdicionado: ' . $_SESSION['userinfdateAdded'][0] . '<br />';
?>
And if there is only one value for each key of session then
<?php
function getUserInfo() {
$json = file_get_contents('http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/Social/Login?Token=210029242357724|fd4eef8a839f24db2a9fedcd.1-100001001235070|Nro7dAY411DJRn7E8zB6MOXHjq8');
$data = json_decode($json, TRUE);
$v= $data['data'];
foreach($v as $key => $value)
{
$_SESSION['userinf'.$key] = $value;
}
}
getUserInfo();
echo 'IDClube: ' . $_SESSION['userinfclubs']['clubId'] . '<br />';
echo 'NomeClube: ' . $_SESSION['userinfclubs']['clubName'] . '<br />';
echo 'LogoClube: ' . $_SESSION['userinfclubs']['clubLogo'] . '<br />';
echo 'RelationType: ' . $_SESSION['userinfclubs']['relationType'] . '<br />';
echo 'DataAdicionado: ' . $_SESSION['userinfclubs']['dateAdded'] . '<br />';
Be careful,there's more than one club in clubs, so you'll need to do something like this :
foreach ($v['clubs'] as $value) {
$_SESSION['userinfclubId'][] = $value['clubId'];
$_SESSION['userinfclubName'][] = $value['clubName'];
$_SESSION['userinfclubLogo'][] = $value['clubLogo'];
$_SESSION['userinfrelationType'][] = $value['relationType'];
$_SESSION['userinfdateAdded'][] = $value['dateAdded'];
}

How to suffix apostrophe-s, with optional middle name and last name?

I'm trying to display the apostrophe 's after the full name for example Samuel L. Jackson’s but if the last name or middle name is left out the 's is prefixed by a space, for example: Samuel ’s. Can some one help me correct this problem?
Thanks
Here is the PHP code.
if(!empty($first_name) || !empty($middle_name) || !empty($last_name)) {
echo = $first_name . ' ' . $middle_name . ' ' . $last_name . ' \'s';
}
$text = array();
if(!empty($first_name)) {
$text[] = $first_name;
}
if(!empty($middle_name)) {
$text[] = $middle_name;
}
if(!empty($last_name)) {
$text[] = $last_name;
}
if(count($text) > 0) {
echo implode(' ', $text).'\'s';
}
echo trim($first_name . ' ' . $middle_name . ' ' . $last_name). ' \'s';
should do the trick?
One more issue: If you have a first and last name, there will be two spaces in between... is that going to be a problem at some point?
echo trim($first_name . ' ' . $middle_name . ' ' . $last_name) . '\s';
trim will get rid of any trailing spaces.
$a = ""
if (!empty($first_name)
$a .= $first_name . " "
if (!empty($middle_name)
$a .= $middle_name . " "
if (!empty($last_name)
$a .= $last_name . " 's"
This should do the trick.
Why are you using Single Quote ?
You could simply use "'s" no need of escaping.
echo $first_name.(empty($middle_name) ? '' : $middle_name.' ').$last_name."'s"
alternative
$names = array($first_name);
if(!empty($middle_name))
$names[] = $middle_name;
$names[] = $last_name;
echo implode(' ', $names)."'s";
echo
(empty($first_name) ? '' : $first_name) .
(empty($middle_name) ? '' : ' ' . $middle_name) .
(empty($last_name) ? '' : ' ' . $last_name) . "'s";
If the variables will always be set:
echo str_replace(
' ',
' ',
$first_name . ' ' .
$middle_name . ' ' .
$last_name . "'s");
To display a proper single quote in HTML, replace the ' with ’, which displays as ’.
may be
echo htmlspecialchars($full_name, ENT_QUOTES);
will solve the problem
$full_name = trim($first_name.' '.$middle_name);
if(!empty($full_name) && !empty($last_name)){
$full_name .=' '.$last_name."'s";
echo $full_name;
}

Categories