Convert php array to UTF8 recursively

This simple php function converts recursively all values of an array to UTF8.
The function mb_detect_encoding (line 4) checks if the value already is in UTF8, this way it will not reconvert.

1
2
3
4
5
6
7
8
9
10
function utf8_converter($array)
{
    array_walk_recursive($array, function(&$item, $key){
        if(!mb_detect_encoding($item, 'utf-8', true)){
                $item = utf8_encode($item);
        }
    });
 
    return $array;
}

How to detect if the device is an iPhone 5

The iPhone5 introduced a new resolution to support in our iOS applications. The IPhone 5 resolution is 640x1136px at 4 inch screen, which makes it more elongated than in previous devices.

You may want to detect if the application is being used from an iPhone 5 and change the behavior of an element.

There are some ways to do, this is one of them:

1
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568) ? NO : YES)

We have defined a constant that will help us to detect the resolution of the iPhone 5. In our code we’ll just use an if block to act accordingly.

1
2
3
4
5
6
7
8
if (IS_IPHONE5)
{
    // The app is running on iPhone 5
}
else
{
    // Not an iPhone 5
}

Anonymous functions (closures) in PHP >= 5.3

According to the PHP documentation, anonymous functions, or closures, allow the creation of functions that haven’t got a specified name.

Simple example:

$greeting = function($name)
{
    printf("Hello %s\r\n", $name);
};
 
$greeting('World');

But in this example we’ll go a little further.
Imagine you have this array:

$users = array(
             array('id' => 1, 'name' => 'Steve', 'birthday' => '1984-02-17'),
             array('id' => 2, 'name' => 'Bill', 'birthday' => '1985-07-07'),
             array('id' => 3, 'name' => 'James', 'birthday' => '1974-11-17')
         );

If we want to get the ids for each user, we would do something like this:

$ids = array();
foreach ($users as $user) {
    $ids[] = $user['id'];
}

But there is a much more elegant way to do this using anonymous functions (closures):

$ids = array_map(function ($user) {
    return $user['id'];
}, $users);

Much cleaner and elegant… Now we want to get the users who were born after 1980. Normally do this:

$filtered_users = array();
$start_date = strtotime('1980-01-01');
foreach ($users as $user) {
    if (strtotime($user['birthday']) >= $start_date) {
        $filtered_users[] = $user;
    }
}

Now we will use again anonymous functions or closures:

$start_date = strtotime('1980-01-01');
$filtered_users = array_filter($users, function($user) {
    global $start_date;
    return strtotime($user['birthday']) >= $start_date;
});

These are just a few examples of anonymous functions or closures in PHP.

How to get GPS coordinates from an address with PHP and Google Maps

With this simple method in PHP we can get the GPS coordinates from an address using Google Maps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
 
function getCoordinates($address){
    $address = urlencode($address);
    $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=" . $address;
    $response = file_get_contents($url);
    $json = json_decode($response,true);
 
    $lat = $json['results'][0]['geometry']['location']['lat'];
    $lng = $json['results'][0]['geometry']['location']['lng'];
 
    return array($lat, $lng);
}
 
 
$coords = getCoordinates("Wall Street, New York");
print_r($coords);
 
 
/*
Prints:
 
Array
(
    [0] => 41.3936254
    [1] => 2.1634189
)
*/

Playing a system sound in iOS app

We are going to play a system sound from our iOS app. You don’t always need to add to your iPhone or iPad application a audio track to play a sound, because the system has sounds that we can use in our application.

In this case, we are going to play the typical sound “beep“. To do this we will use the AudioToolbox framework.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#import <AudioToolbox/AudioToolbox.h>
 
SystemSoundID mBeep;
 
// Create the sound
NSString* path = [[NSBundle mainBundle] 
                     pathForResource:@"Beep" ofType:@"aiff"];
NSURL* url = [NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &mBeep);
 
// Play the sound
AudioServicesPlaySystemSound(mBeep);
 
// Dispose of the sound
AudioServicesDisposeSystemSoundID(mBeep);

In the official documentation you can read more about this topic, and you can also add vibration to these alerts.

How to shuffle a NSMutableArray [Objective-C]

The Objective-C NSArray object doesn’t have a specific function to shuffle the elements of the array.

With this simple code we can shuffle one NSMutableArray randomly. We will use the exchangeObjectAtIndex method for exchange items within the array and arc4random to get a random number we use to exchange items.

1
2
3
4
5
6
7
/* myArray is a NSMutableArray with some objects */
NSUInteger count = [myArray count];
for (NSUInteger i = 0; i < count; ++i) {
	int nElements = count - i;
	int n = (arc4random() % nElements) + i;
	[myArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}

Very useful, right? 🙂

Virtual functions in C++

In object-oriented programming we find the concept of polymorphism. It is the ability for different derived classes from the same superclass, use the same function differently.

Polymorphism in C++ is implemented with the help of virtual functions.

Better with an example.

We have two classes:

1
2
3
4
5
6
7
8
9
10
11
class Animal
{
    public:
        void eat() { std::cout << "I eat generic food"; }
}
 
class Cat : public Animal
{
    public:
        void eat() { std::cout << "I eat cat food"; }
}

We have this in main function:

1
2
3
4
5
Animal *animal = new Animal;
Cat *cat = new Cat;
 
animal->eat(); // Prints: "I eat generic food"
cat->eat();    // Prints: "I eat cat food"

Perfect, in this case we do not need virtual functions and everything works as expected.

But if for any reason we need to call the eat() function from an intermediate function:

1
2
// We can put this function at the beginning of our main.cpp file
void oneFunction(Animal *animal) { animal->eat(); }

Change our main function:

1
2
3
4
5
Animal *animal = new Animal;
Cat *cat = new Cat;
 
oneFunction(animal) // Prints: "I eat generic food"
oneFunction(cat)    // Prints: "I eat generic food"

As we can see, in this case always calls the eat() function of the superclass and not of the child class (Cat).

The solution is to convert the eat() function in a virtual function:

1
2
3
4
5
class Animal
{
    public:
        virtual void eat() { std::cout << "I eat cat food"; }
}

In main function:

1
2
oneFunction(animal) // Prints: "I eat generic food"
oneFunction(cat)    // Prints: "I eat cat food"

Although not necessary, I like indicate in the child class the virtual function too. This way, if we have a class that inherits from a superclass of another programmer, we know at a glance what are the overridden functions.

Therefore, this is our Cat class:

1
2
3
4
5
class Cat : public Animal
{
    public:
        virtual void eat() { std::cout << "I eat cat food"; }
}

Hello world! This is our blog

Welcome to NazcaLabs‘s blog! We will use this blog to write articles about web, mobile and video games development, writing tutorials, introduction to some of the tools we use, snippets, tips, etc.. We hope you find it useful.

If you want you can subscribe to our newsletter, from which you can receive news of our new projects or invitations to be the first to try them.

Also we will use it to introduce our new projects, so we started presenting our first mobile game. It’s “Find image pairs“, an iPhone game in which we need to find the matching pictures in as few moves as possible, trying to beat our own record. The game is ideal for children because, besides helping them to exercise their memory, the game does not require internet connection, no in-app purchases and has no advertising.

Any feedback is welcome! What has been your best score in “Find image pairs“? 😉
Our best score is 20 moves!

pairs