Welcome! Log In Create A New Profile

Advanced

PHP6 - Arrays in PHP

Posted by Gaia77490614 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
PHP6 - Arrays in PHP
February 15, 2012 04:05PM
I have completed this section
avatar Re: PHP6 - Arrays in PHP
February 16, 2012 02:48PM
section completed.
Re: PHP6 - Arrays in PHP
February 20, 2012 01:34PM
Section completed
Must say everything so far is very nicely explained grinning smiley
Re: PHP6 - Arrays in PHP
February 20, 2012 02:06PM
Arrays are awesome guys.

$array = array(array('name'=>'innocent', 'surname'=>'langa', 'age'=>25),
array('name'=>'simo', 'surname'=>'zaca', 'age'=>24));

foreach ($array as $key => $value) {
if ($value == 25)
$array[$key]['winner'] = 'Yes';
}

Would this works guys i haven't testing.
What is the best way to walk an array.
Re: PHP6 - Arrays in PHP
February 20, 2012 08:17PM
Section completed.

This section is the main reason why I registered for this course. Arrays in PHP can be confusing when compared to other languages. The => operators do not actually test values but insert them.

innocent,

Your code does not work. You need to specify the age field in the if statement.

Language: PHP
$array = array(array(';name';=>';innocent';, ';surname';=>';langa';, ';age';=>25), array(';name';=>';simo';, ';surname';=>';zaca';, ';age';=>24));   foreach ($array as $key => $value) { if ($value[';age';] == 25) { $array[$key][';winner';] = ';Yes';; echo $array[$key][';winner';]; } }
Re: PHP6 - Arrays in PHP
February 20, 2012 08:47PM
I have completed this section. I found I could understand arrays fine. Although, 77586182 I found the code you put above confusing. I have never done php before so it looks more complex than the examples in the section. There are arrays within an array? Can you explain to me what is happening in the code above line by line? If it is not too much trouble? Just want to make better sense of how it is working.
Re: PHP6 - Arrays in PHP
February 20, 2012 11:22PM
Hi Sian,

The code above is a bit more complex than the examples from the php tutorials but you can find a better explanation on page 68 of the Beginning PHP5, Apache, And MySQL Web Development Textbook. Basically it is an array with two arrays inside as values. I will use the sample from the textbook first and then explain the code in my previous post.

Taken from page 68 of the text book.

You can also have arrays within arrays (also known as multi-dimensional arrays). In the earlier example,
you had two people sitting at one table. What if you pulled up another table and added a few more people
to the mix? How in the heck would you store everyone’s information and keep it all separate and
organized? Like this!


Language: PHP
<?php $table1 = array(“husband” => array(“firstname”=>”Albert”, “lastname”=>”Einstein”, “age”=>124), “wife” => array(“firstname”=>”Mileva”, “lastname”=>”Einstein”, “age”=>123)); //do the same for each table in your restaurant ?>

Then if someone asks you, “Hey, what are the first names of the couple sitting at table one?,” you can
easily print the information with a few simple echo statements:


Language: PHP
<?php echo $table1[“husband”][“firstname”]; echo&; echo $table1[“wife”][“firstname”]; ?>

This script would produce the output “Albert & Mileva.”

The code from my previous example is a multi-dimensional array. It contains two persons with a 'name', 'surname' and 'age'.

Language: PHP
$array = array(array(';name';=>';innocent';, ';surname';=>';langa';, ';age';=>25), array(';name';=>';simo';, ';surname';=>';zaca';, ';age';=>24));

The foreach loop checks each value in the multi-dimensional array ($array) and uses the $key variable as the current index. So the $key variable will be the index of the first person and then change to the index of the second person after the loop has executed once.

Language: PHP
foreach ($array as $key => $value) {

$value is the data of the current person that is being validated by the loop. On the first execution of the loop, $value will contain an array with the values of the first person (name="innocent", surname="langa", age="25"winking smiley.

The if statement checks if the age of the person is equal to 25. If the statement is true, a new field is added to the person array called "winner" with the value "Yes".

Language: PHP
if ($value[';age';] == 25) { $array[$key][';winner';] = ';Yes';; echo $array[$key][';winner';]; }
Re: PHP6 - Arrays in PHP
February 21, 2012 08:22AM
^^^ Thanks very helpful post
Re: PHP6 - Arrays in PHP
February 21, 2012 10:26AM
Edited by Mac: I have deleted your post because you have not read this....http://osprey.unisa.ac.za/phorum/read.php?570,162261
Re: PHP6 - Arrays in PHP
February 21, 2012 01:02PM
If I were to look for a certain value, let's say in the above example look which one of the two array's within the array has a key named "winner", how would you do that?
Obviously you would use an if statement to see if it can find the key named "winner" within the foreach function...

But how would the line look to print only the array with the key named "winner"???
Re: PHP6 - Arrays in PHP
February 21, 2012 01:24PM
My sincere apology mac, i did not read that post.

77471466_Alwyn, my solution would be

Language: PHP
$array = array(array(';name';=>';innocent';, ';surname';=>';langa';, ';age';=>25), array(';name';=>';simo';, ';surname';=>';zaca';, ';age';=>24));   foreach ($array as $key => $value) { if ($value[';age';] == 25) { $array[$key][';winner';] = ';Yes';; echo $array[$key][';winner';]; } }   while (list($name, $surname, $winner) = each($array)) { if (isset($winner)) { echo $winner; } }

or if you know the key position
Language: PHP
echo $array[0][';winner';];

In fact there are many ways.
Re: PHP6 - Arrays in PHP
February 22, 2012 09:09AM
Thanks Innocent, much appreciated!
Where did you learn of the funcion "list" and "each" if I may ask?
Re: PHP6 - Arrays in PHP
February 22, 2012 03:00PM
Hi 77471466_Alwyn

In the book, on page 197 chapter 7
Especially when you want to get info about image uploaded.
Re: PHP6 - Arrays in PHP
February 23, 2012 08:21AM
77592972_innocent Wrote:
-------------------------------------------------------
> Hi 77471466_Alwyn
>
> In the book, on page 197 chapter 7
> Especially when you want to get info about image
> uploaded.


Thank you!
Re: PHP6 - Arrays in PHP
February 25, 2012 04:16PM
Done
avatar Re: PHP6 - Arrays in PHP
February 25, 2012 06:17PM
Completed this section.

Something's bothering me, though. I can't get the list() example snippet (77592972_innocent) – pasted from above – to work.
Language: PHP
$array = array(array(';name';=>';innocent';, ';surname';=>';langa';, ';age';=>25), array(';name';=>';simo';, ';surname';=>';zaca';, ';age';=>24));   foreach ($array as $key => $value) { if ($value[';age';] == 25) { $array[$key][';winner';] = ';Yes';; echo $array[$key][';winner';]; } }   while (list($name, $surname, $winner) = each($array)) { if (isset($winner)) { echo $winner; } }

I think there's something very wrong with it, since I get an 'undefined offset' error. When I print out the $name, $surname and $winner using the following echo statement:
Language: PHP
while (list($name, $surname, $winner) = each($array)) { echo ';name:';.$name.'; surname:';.$surname.'; winner:';.$winner; .... }

I get undesirable results:
name: 1 surname: Array winner:
... only that one result.

I'm going to work with it a bit to see if I can see what's going wrong. I haven't used list() before, so not an expert, but does it maybe have something to do with the each($array) being the wrong thing to use with list(...)?

I will let you know if I figure out what's happening....

Thanks
avatar Re: PHP6 - Arrays in PHP
February 25, 2012 06:48PM
@Alwyn

I would replace the while(list(...) = ...){} statement above with something that I think is simpler:
Language: PHP
//for every nested array inside of the main array foreach($array as $nested_array){   //check if the nested array we';re currently looking at has a key called ';winner'; if(isset($nested_array[';winner';])){   // tell everyone that this person is a winner echo $nested_array[';name';].'; ';.$nested_array[';surname';].'; is a winner';; } }

...But maybe I am missing something special about list() that I've previously missed. I will explore a bit more.

Personally it's not the way I would have gone about setting and checking for 'winner', but it is interesting.
Re: PHP6 - Arrays in PHP
February 27, 2012 03:19PM
So far this is what i came up with but im still working on it.
Im sorry for the bug
Language: PHP
<?php   $array = array(array(';name';=>';innocent';, ';surname';=>';langa';, ';age';=>25), array(';name';=>';simo';, ';surname';=>';zaca';, ';age';=>24));   foreach ($array as $key => &$value) { if ($value[';age';] == 25) { $array[$key][';winner';] = ';Yes';; //echo $array[$key][';winner';]; } } $arr = array(); foreach($array as $key => $value) { if (is_array($value)) { foreach($value as $k => $v) { array_push($arr, $v); } } else { array_push($arr, $value); } }   reset($arr); $innocent = array_chunk($arr, 4); $me = array_merge($innocent[0], $innocent[1]); while(list($key, $winner) = each($me)) { if ($winner == ';Yes';) echo "There is a winner!"; } ?>
Re: PHP6 - Arrays in PHP
February 29, 2012 01:44PM
Ive learned to create an array on a fly using functions

Language: PHP
function a() { $args = func_get_args(); return $args; }   //To create an array $array = a(';a';, ';b';);   //Results // array(';a';, ';b';)
Re: PHP6 - Arrays in PHP
February 29, 2012 03:31PM
I am done with this chapter.
Re: PHP6 - Arrays in PHP
February 29, 2012 09:49PM
I enjoyed these chapter much
Re: PHP6 - Arrays in PHP
February 29, 2012 09:50PM
Not forgetting spliting
Re: PHP6 - Arrays in PHP
March 05, 2012 11:28PM
I have completed this section.
Re: PHP6 - Arrays in PHP
March 06, 2012 12:01AM
An easy PHP foreach example:

Language: PHP
<?php   $movie = array( "title" => "Rear Window", "director" => "Alfred Hitchcock", "year" => 1954, "minutes" => 112 );   echo "<dl>";   foreach ( $movie as $key => $value ) { echo "<dt>$key:</dt>"; echo "<dd>$value</dd>"; }   echo "</dl>"; ?>

This will output:
Language: PHP
title: Rear Window director: Alfred Hitchcock year: 1954 minutes: 112
Re: PHP6 - Arrays in PHP
March 06, 2012 01:23AM
Another simple example to wish me a Happy Birthday:

Language: PHP
<?php $name = array(';77585607';, ';Luke';, ';John';); $bday = array(';6 March';, ';11 November';, ';26 January';); $comment = array(';Happy Birthday to me';, ';No Comment';, ';No Comment';);   foreach($name as $key => $name) { echo $name." - ".$bday[$key]." - ".$comment[$key]."<br />\n"; } ?>

This will output:
Language: PHP
77585607 - 6 March - Happy Birthday to me Luke - 11 November - No Comment John - 26 January - No Comment
Re: PHP6 - Arrays in PHP
March 08, 2012 12:32PM
Completed this section.
Re: PHP6 - Arrays in PHP
March 09, 2012 09:46AM
Done and dusted!
Re: PHP6 - Arrays in PHP
March 10, 2012 07:26PM
Completed the section. Enjoyed the band mention it reminded me of the many ways PHP can be used.
Re: PHP6 - Arrays in PHP
March 11, 2012 01:27PM
Done and Dusted!

Arrays are awesome!, storing multiple variables in one :O
Re: PHP6 - Arrays in PHP
March 11, 2012 06:54PM
Completed this section
Sorry, you do not have permission to post/reply in this forum.