Contact Us

Even More PHP Array Functions

Following on from our previous post showing us two more PHP Array functions , in this instance we are going to build further looking at even more PHP Array Functions.

 

array_diff()

The array_diff() function is useful when trying to differences between two arrays.

The syntax is as follows:

array_diff( array1, array2, array3… );

The array_diff() function compares two or more arrays based on their values and returns the differences between them in array form showing what is in array 1 that is not in array 2, 3, 4 etc.

An example is shown below:

$array1        =    array( ‘red’, ‘green’, ‘blue’, ‘yellow’ );
$array2        =    array( ‘red’, ‘green’, ‘blue’, ‘purple’ );

$arrayDiff    =    array_diff( $array1,$array2 );

The output of the above is as follows:

Array ( [3] => yellow )

In addition there is the array_diff_key() function which is exactly the same as array_diff() except it uses the array keys instead of values to find the differences and the array_diff_assoc() function uses both the key and values in its search for a difference.

array_fill()

The array_fill() function is a bit of a strange one and is hard to find immediate usage for it but hey it’s here so let’s learn it, it may be useful for you! It is used to fill out an array with values.

The syntax is as following:

array_fill( index, number, value );

The first parameter (index) is used to define where we will start our array index, the next parameter is the number of values to insert and the third value parameter specifies which value to fill the array with.

An example is shown below:

$arrayFill    =    array_fill( 0, 5, ‘newValue’ );

The output of the above is:

Array ( [0] => newValue [1] => newValue [2] => newValue [3] => newValue [4] => newValue ) 

In addition to this there is the array_fill_keys( keys, value ) function which fills out an array with values while specifying keys.

An example is shown below:

$keys        =    array( ‘A’, ‘B’, ‘C’, ‘D’ );
$array        =    array_fill_keys( $keys, ‘Letter’ );

The output of the above is:

Array ( [A] => Letter [B] => Letter [C] => Letter [D] => Letter )

Hopefully these functions will help you when organising your data in your PHP Applications. If you would like any help with your PHP development or web projects then why not get in contact with us here at Network Intellect.

This article was posted by



Leave a Reply

What our clients say

"It’s like having your own in-house digital marketing consultancy rather than an external company. I no longer think about them as a supplier. They’re part of the team."
Marketing Manager, Education
"They have always been professional and focused on delivering results in a timely and cost effective manner. It is enlightening to have an organisation that takes the time to make recommendations on actual data analysis."
Business Manager, Charity
"They (Network Intellect) continue to exhibit their digital expertise by helping us to establish a competitive advantage by pushing the boundaries and innovating their technology and performance solutions."
Head of Marketing Communications, Education
"Because of their talent, expertise and level of service our on-line business has increased dramatically."
Founder, Electronics
"I would have no hesitation in recommending them as an analytics partner to understand and improve the performance of a website."
Vice President, Retail
Network Intellect continue to give us a fantastic service, fast and friendly and are always ready to go the extra mile. They have gone above and beyond the call of duty to deliver our results.
Director, E-Cigarette Web
"We found that Network Intellect had the depth of knowledge we needed and after speaking to them a few times we decided to go with them. It’s the best decision we’ve made in some time."
Owner, Retail
"Their services offered great value for money. I would not hesitate to recommend them as a digital partner to others."
Strategic Marketing Manager, Education
"Jason and his team at Network Intellect embody efficiency and efficacy. I would have no hesitation in recommending this excellent company to others."
Marketing Manager, Education
Network Intellect have managed our product data feeds with exceptional care and meticulous detail. I know I can trust them to make the right decisions for our business."
Director, E-Commerce
"It’s like having your own in-house digital marketing consultancy rather than an external company. I no longer think about them as a supplier. They’re part of the team."
Marketing Manager, Education
Read all testimonialsRead all testimonials

We can help you be great

We have friendly staff ready to help you get on your path to greatness...
Network Intellect Menu