Welcome! Log In Create A New Profile

Advanced

PHP 6 - Arrays in PHP

Posted by 77959132 NeoGek 
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
PHP 6 - Arrays in PHP
February 22, 2013 03:52PM
Done and dusted!
Re: PHP 6 - Arrays in PHP
February 26, 2013 08:52PM
PHP and arrays come in handy when one has to manipulate databases. This is one of the reasons why I took this course. I also learned an interesting technique from the textbook, creating a database dynamically using PHP instead of directly creating it in phpMyAdmin. I could manipulate the database tables to dynamically display the results of the user's search before.
avatar Re: PHP 6 - Arrays in PHP
February 26, 2013 09:17PM
Arrays really are very useful but I must say it is quiet differant to the arrays I'm use too.

This is one of the scripts I coded a couple of weeks ago, as you can see it is a lot differant to what we are learning. I have a feeling I'm going to have to keep my wits about me.

Language: PHP
<?php if (is_front_page()) {} elseif ( is_page( ';x-home'; ) ) { echo ';<div class="get-page">';; query_posts( array(';post_type'; => ';page';, ';numberposts'; => 2, ';post_status'; => ';publish';, ';order'; => ';ASC';, ';orderby'; => ';menu_order';, ';post__in';=> array( 412, 162, ), ) ); if (have_posts()) { while (have_posts()) { the_post(); echo ';<div class="pageboxlet"><h2><a href="';. get_permalink() .';" title="';. get_the_title() .';">';. get_the_title() .';</a></h2> <a href="';. get_permalink() .';" title="';. get_the_title() .';">';. get_the_post_thumbnail($page->ID, ';full';) .';</a><p>';; $thisContent = strip_tags($post->post_content); echo substr($thisContent, 0, 100); if(strlen($thisContent)>100){ echo ';...<a href="';. get_permalink() .';">click here</a>';; } echo ';</p></div>';; } } echo ';</div>';; }   elseif ( is_page( ';x-workshop'; ) ) { echo ';<div class="get-page">';; query_posts(';posts_per_page=8&order=DESC&orderby=post_date&cat=38';); if (have_posts()) { while (have_posts()) { the_post(); echo ';<div class="post"><h3>';. get_the_title() .';</h3>';; echo ';';. the_content() .';</div>';; } } echo ';</div>';; }   elseif ( is_page( ';x-competition'; ) ) { echo ';<div class="get-page">';; query_posts(';posts_per_page=8&order=DESC&orderby=post_date&cat=33';); if (have_posts()) { while (have_posts()) { the_post(); echo ';<div class="post"><h3>';. get_the_title() .';</h3>';; echo ';';. the_content() .';</div>';; } } echo ';</div>';; }   elseif ( is_page( ';x-news'; ) ) { echo ';<div class="get-page">';; query_posts(';posts_per_page=8&order=DESC&orderby=post_date&cat=41';); if (have_posts()) { while (have_posts()) { the_post(); echo ';<div class="post"><h3><a href="';. get_permalink() .';" title="';. get_the_title() .';">';. get_the_title() .';</a></h3><p>';; $thisContent = strip_tags($post->post_content); echo substr($thisContent, 0, 100); if(strlen($thisContent)>100){ echo ';...<a href="';. get_permalink() .';">read more</a>';; } echo ';</p></div>';; } } echo ';</div>';; }   elseif ( is_page( ';x-gallery'; ) ) { echo ';<ul class="post-gall">';; query_posts(';posts_per_page=9&order=DESC&orderby=post_date&cat=25';); if (have_posts()) { while (have_posts()) { the_post(); echo ';<li> <a href="';. get_permalink() .';" title="';. get_the_title() .';"> ';. get_the_post_thumbnail($thumbnail->ID, ';thumbnail';) .'; <h3>';. get_the_title() .';</h3></a> </li>';; } } echo ';</ul>';; } ?>

The above is wordpress smile

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
avatar Re: PHP 6 - Arrays in PHP
February 26, 2013 09:20PM
smileys with beer

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: PHP 6 - Arrays in PHP
February 28, 2013 04:02PM
alrighty getting real up in here... ive always struggled with arrays from the days of turbo pascal's 2D/3D arrays.. i just dont think im smart enough for it lol.. will have to figure it out at whichever situation I land..

off to the next section! hot smiley

Student number : 7803-010-2
Email and Gtalk for support : wilcovandeijl@gmail.com
Re: PHP 6 - Arrays in PHP
March 01, 2013 11:41AM
Section 6 was also interristing & fun.
Re: PHP 6 - Arrays in PHP
March 04, 2013 06:36AM
tried to run your code shado received this error message....
Language: PHP
Fatal error: Call to undefined function is_front_page() in C:\xampplite\htdocs\portfolio\shado.php on line 1
avatar Re: PHP 6 - Arrays in PHP
March 04, 2013 06:36AM
Enjoyed arrays section, especially dice simulation,sorting and foreach loops.
avatar Re: PHP 6 - Arrays in PHP
March 04, 2013 08:42PM
That's because is_front_page is native to Wordpress 2.5 and above.

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: PHP 6 - Arrays in PHP
March 05, 2013 07:18PM
I am revisiting arrays today as I am using them in a program I am developing. Is it a sloppy programming if I populate my array without specifying the 'KEYS' in the 'KEY-VALUE' pair? I did notice that sometimes the 'KEY-VALUE' pairs are declared but at other times only 'VALUES' are declared in the array.
Re: PHP 6 - Arrays in PHP
March 05, 2013 10:12PM
Done - most interesting is using "for each" together with associative key to find values.

Can think of may applications using arrays, such as applications using:
days of the week, months, age groups, class lists, mark sheets, issues of publications. booking/reservation of venues, etc.

Wish I had more time to try out some ideas, but have to move on...
Re: PHP 6 - Arrays in PHP
March 06, 2013 02:44PM
By far the most difficult topic for me so far is arrays but i am getting used to them, A serious concern for me is that if a variable is said to store one item and a an array is said to be like a variable storing a list of items therefore the logic is that arrays are way bigger that simple variables, hope this wont affect the smooth running of web applications as logic would have it it become more "heavier", jus a thought not sure on how true it is. Another concern for me is the sorting that we were introduced to like ksort, rsort, etc i am assuming in terms data structures its all in th coding of sort functions how its actually sorted outeg heapsort, bubblesort, quicksort but I guess when I reach the topic functions i will see how its done, but its good to be aware how its done so we can custome sort our arrays. 77932455, kindly post your code so we can see what mean and assist each other.

..&ru..
avatar
Mac
Re: PHP 6 - Arrays in PHP
March 06, 2013 04:21PM
Arrays are not easy to graps. But when you need them, you will learn them smiling smiley

Generally, you may have a form with check boxes (i.e. more than one option can be selected by user). Now, a check box(es) have the same name, but different values, so that means one variable with more than one value.

Language: PHP
<form action=""> <input type="checkbox" name="vehicle" value="motorbike">I have a motor bike<br> <input type="checkbox" name="vehicle" value="Car">I have a car </form>


That is where an array is useful - to send it as one, then extract them into separate variables, which you use as needed.
Re: PHP 6 - Arrays in PHP
March 06, 2013 04:34PM
Language: PHP
$_GET[';username';] $_POST[';username]
Are also arrays that we have already been using when working with forms. Arrays are very important to understand. Still working on them though...
Re: PHP 6 - Arrays in PHP
March 11, 2013 09:40PM
Can someone tell me why is it printing only 3 seasons?

Language: PHP
<?php   $seasons = array(1=>"Autumn",2=>"Winter",3=>"Spring",4=>"Summer");   $array_count = count($seasons);   for($key_Number = 1;$key_Number<$array_count;$key_Number++){ print $seasons[$key_Number]; }   ?>
avatar Re: PHP 6 - Arrays in PHP
March 12, 2013 03:00AM
Hi there, I think if you change the following piece of code:
Language: PHP
for($key_Number = 1;$key_Number<$array_count;$key_Number++)

to this:

Language: PHP
for ($key_Number = 1; $key_Number <= $array_count; $key_Number++)

or maybe a better solution would be this
Language: PHP
for ($key_Number = 1; $key_Number < $array_count+1; $key_Number++)

all 4 seasons are printed.

If you print the
Language: PHP
$key_Number
variable next to each
Language: PHP
$seasons[$key_Number]
, you will see what the key's value is in the for-loop. If I understand this correctly, if the key's value starts at 1, it gets to the
Language: PHP
$array_count
, which is 4, when it gets to the 3rd Season. So, the value that is in
Language: PHP
$array_count
stops the execution of the for-loop before the for-loop gets to the last value in the array. If you want to start the key values at 1 in stead of 0, you have to add 1 to the value of
Language: PHP
$array_count
. For example, if you want to start with a key of 11, you have to add 11 to the value of $array_count, like so:
Language: PHP
$seasons = array(11=>"Autumn", 12=>"Winter", 13=>"Spring", 14=>"Summer"); $array_count = count($seasons);   for ($key_Number = 11; $key_Number < $array_count+11; $key_Number++) { print $key_Number. " " .$seasons[$key_Number]; } ?>

Hope this helps.
avatar
Mac
Re: PHP 6 - Arrays in PHP
March 12, 2013 08:39AM
Perfect explanation... smiling smiley
Re: PHP 6 - Arrays in PHP
March 14, 2013 12:54AM
Thank you for breaking it down for me so perfect!
Re: PHP 6 - Arrays in PHP
March 14, 2013 12:59PM
This also helpful when working with arrays.
Sorting Arrays
avatar Re: PHP 6 - Arrays in PHP
March 16, 2013 08:21AM
Study on arrays went smooth. Other study I made online explained Arrays in table like formats and explained also that "The name “array” comes from the same Latin roots as the word “arrangement.” Clarity is to have all the your values in one column and your keys juxtaposed along side on the next column (e.g. all the seasons in one column, and their corresponding values on the next column).
Re: PHP 6 - Arrays in PHP
March 18, 2013 09:49AM
Question1

Hi. I do not want to go off topic, but since learning Arrays and actually understanding it better this time I want to know if Arrays or Lists or both are the way forward ?
Please note I have not touch LISTS before and heard over the weekend from a senior that they do not use Arrays anymore and only LISTS.

Question2:

Also how can you extract the or a [key] value from an Array by searching via a name(value) value?

[key1] => Value1, [key2] = Value2

Say I do not know they [key], but I know the value(Value2 for example). How do I extract the value together with the [key] to know what its [key] value is ?

Example: Search for Value2. Value2's key is [key2].

The reason why I ask is that I struggle to extract certain values out of an array inside an array.

Student Number: 78042879 (Part Time Courses)
avatar Re: PHP 6 - Arrays in PHP
March 18, 2013 01:09PM
Lists are greatly used in .net but one thing to remember when using lists is that list() assigns the values starting with the right-most parameter. If you are using plain variables, you don't have to worry about this. But if you are using arrays with indices you usually expect the order of the indices in the array the same you wrote in the list() from left to right; which it isn't. It's assigned in the reverse order.

As for question 2, why not just flip the equation around?

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: PHP 6 - Arrays in PHP
March 18, 2013 08:47PM
Shado77968239 Wrote:
-------------------------------------------------------

> As for question 2, why not just flip the equation
> around?

I was playing with it in December and I could not find a solution. Thought it would be interesting if you every got in a situation where you need toe extract a specific value and don't know the key?

Student Number: 78042879 (Part Time Courses)
Re: PHP 6 - Arrays in PHP
March 18, 2013 09:00PM
However frustrating arrays can be, but they will be necessary when extracting data from the database we have to build for the portfolio.
Re: PHP 6 - Arrays in PHP
March 19, 2013 02:07PM
WOW! This section was a lot to grasp, it is quite difficult to remember all of this but I am sure I will be forced to use Arrays soon enough. Found the "dice" roll script to be quite fun though spinning smiley sticking its tongue out On to the next section hot smiley
Re: PHP 6 - Arrays in PHP
March 20, 2013 10:32AM
done. the foreach syntax is very similar to perl.
the print_r function is very useful in visualizing arrays containing arrays
avatar Re: PHP 6 - Arrays in PHP
March 20, 2013 11:30PM
I've never been a huge fan of arrays, but I do agree that in some cases they are essential.
I prefer coding arrays using the square bracket method.
Although the code uses more lines, it seems much neater and easier to read.
I didn't realise that the array keys can be text values.
I always use numbers.
Re: PHP 6 - Arrays in PHP
March 20, 2013 11:52PM
My book has arrived and must say anyone who is not using this book is missing out. This book explains arrays in most awesome way.
Re: PHP 6 - Arrays in PHP
March 21, 2013 05:20PM
Language: PHP
<html> <head> <title> Arrays </title> </head> <body> <?php   $arrayName = array(';la';=>';cat';, ';bla';=>';dog';); foreach ($arrayName as $key => $value) {   if ($value == ';dog';) print($key);   }   ?> </body> </html>
avatar Re: PHP 6 - Arrays in PHP
March 22, 2013 11:07PM
Besides this exercise, I used arrays today at work on our intranet in/out board.
Re: PHP 6 - Arrays in PHP
March 26, 2013 08:35PM
Found the course notes very superficial and feel I need more examples. Where did I miss the roll dice script? Is it in the textbook? Ordered mine from Amazon.uk so hoping it will arrive soon.
avatar Re: PHP 6 - Arrays in PHP
March 26, 2013 10:37PM
Try Google for more examples.

AngelaWoodend-77672860 Wrote:
-------------------------------------------------------
> Found the course notes very superficial and feel I
> need more examples. Where did I miss the roll
> dice script? Is it in the textbook? Ordered mine
> from Amazon.uk so hoping it will arrive soon.

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
avatar
Mac
Re: PHP 6 - Arrays in PHP
March 27, 2013 08:01AM
The tut + handbook + this forum + Google + portfolio = you will learn a lot smiling smiley
Re: PHP 6 - Arrays in PHP
March 31, 2013 01:09PM
done
Sorry, only registered users may post in this forum.

Click here to login