Skip to main content

PHP arrays - simple operations

Source: Flickr.com -  Upupa4me
Like all things in life, we need to start with simple things. So, in order to understand what arrays are in PHP, we need to take a look at the PHP manual to read what it is all about:
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.
The thing with PHP is that an array is probably one of the most used data structures in web applications build with PHP and used for a wide variety of purposes.

Let's look at a first usage of an array: storing multiple values in a list.

<?php
$array 
= [
    
'foo',
    
'bar',
    
'baz',
];


This is a PHP array with 3 elements. Now we can add an element at the end of this array.

// Adding an element at the of the array 
array_push($array'foobar');

Or even more efficient:

$array[] = 'barbaz';

Now our array looks like this:

$array = [
    
'foo',
    
'bar',
    
'baz',
    
'foobar',
    
'barbaz',
];


As you can see, PHP arrays are easy to create lists which you can now toss around in your application. But what if you need to reverse the process and remove elments from the end of your array?

// Removing the last element of an array 
$last array_pop($array);

Now $last will contain the value "barbaz" and the array itself is now reduced by one from the end.

$array = [
    
'foo',
    
'bar',
    
'baz',
    
'foobar',
]; 


Of course we can also pull elements from the beginning of the array.

// Removing the first element of an array 
$first array_shift($array); 

Now $first will contain the value "foo" and the array is now reduced by one from the beginning.

$array = [
    
'bar',
    
'baz',
    
'foobar',
]; 


In our next chapter let's have a look at associative arrays and what you can do with those.




Comments

Post a Comment

Popular posts from this blog

Speeding up database calls with PDO and iterators

When you review lots of code, you often wonder why things were written the way they were. Especially when making expensive calls to a database, I still see things that could and should be improved.
No framework development When working with a framework, mostly these database calls are optimized for the developer and abstract the complex logic to improve and optimize the retrieval and usage of data. But then developers need to build something without a framework and end up using the basics of PHP in a sub-optimal way.

$pdo = new \PDO( $config['db']['dsn'], $config['db']['username'], $config['db']['password'] ); $sql = 'SELECT * FROM `gen_contact` ORDER BY `contact_modified` DESC'; $stmt = $pdo->prepare($sql); $stmt->execute(); $data = $stmt->fetchAll(\PDO::FETCH_OBJ); echo 'Getting the contacts that changed the last 3 months' . PHP_EOL; foreach ($data as $row) { $dt = new \DateTime('2015-04-…

Deploy Docker containers fast to Microsoft Azure

DEPLOY DOCKER CONTAINERS FAST TO MICROSOFT AZURE It’s hard to ignore the fact thatDockeris a way to move forward for rapid application development, distributed architectures and microservices. For developersDockeroffers great advantages as they can build their containers specifically for the task they work on. They grab a base image of a container, modify it for their purpose and prepare the functionality inside the container. Quality, testing and security teams now have a single instance to look at and ensure all functional and regulatory requirements are met. System engineers now don’t have to worry about providing a system with the required specs as the container is already provisioned for that purpose. But where do you deploy yourDockercontainers? You can set up your existing bare metal infrastructure to allow them to run containers, but this also means you need to learn about securing your container infrastructure, which is not an easy task. Luckily “the cloud” offers container …

PHP Arrays - Associative Arrays or Hash Maps

Associative array or hash maps are listings of key and value pairs with a posibility to nest additional keys and values. An associative array is a very powerful construct within PHP.

In our previous article we discussed simple arrays, which in their turn are indexed associative arrays under the hood. Take the following example:

$array = [
'apple',
'banana',
'chocolate',
]; 

Is in fact an indexed associative array under the hood:

$array = [
0 => 'apple',
1 => 'banana',
2 => 'chocolate',
]; 

But associative arrays can be so much more than just an indexed array, and you will find many database operations returning arrays where the fields of a table are the keys in the array while their values are also the values within the array.

$productRowData = [
'product_id' => 1234,
'brand_id' => 321,
'product_name' => 'Our awesome product',
'prodcut_description' => 'This is our most awesome product.&#…