2
\$\begingroup\$

I put together a function to sort out values that I am getting from a curl request for different 'devices', it works as is, I am only wondering if there is an easier way to to this.

function convert($deviceName, $namesArray, $devicesArray) {
    $result = array();
    foreach ($devicesArray as $d) {
        $result [] = [
            'Device Type' => $deviceName,
            'Device' => $d[0],
            $namesArray[0] => $d[1],
            $namesArray[1] => $d[2],
            $namesArray[2] => $d[3],
            $namesArray[3] => $d[4],
            $namesArray[4] => $d[5],
            $namesArray[5] => $d[6],
            $namesArray[6] => $d[7],
            $namesArray[7] => $d[8],
            $namesArray[8] => $d[9],
            $namesArray[9] => $d[10]
        ];
    }
    return $result;
}

it takes in the following:

$namesArray = [ Operating System, Physical Size, Physical Size, Width, Height, Device-width, Px Per Inch, Px Density, Aspect Ratio, Popularity];

$devicesArray = 
Array
(

    [0] => Array
            (
            [0] => iPhone 4 (4, 4S)
            [1] => iOS
            [2] => 3.5
            [3] => 8.9
            [4] => 640
            [5] => 960
            [6] => 320
            [7] => 326
            [8] => 200% XHDPI
            [9] => 2 : 3
            [10] => 8.8
            )

    [1] => Array
            (
            [0] => iPhone 5 (5c, 5s)
            [1] => iOS
            [2] => 4.0
            [3] => 10.0
            [4] => 640
            [5] => 1136
            [6] => 320
            [7] => 326
            [8] => 200% XHDPI
            [9] => 40 : 71
            [10] => 7.9
            )
)

 and $deviceName field which can be 'Phone', 'Tablet' or 'Monitor'.

This gives the output

[0] => Array
        (
            [Device Type] => Phone
            [Device] => iPhone 4 (4, 4S)
            [Operating System] => iOS
            [Physical Size] => 8.9
            [Width] => 640
            [Height] => 960
            [Device-width] => 320
            [Px Per Inch] => 326
            [Px Density] => 200% XHDPI
            [Aspect Ratio] => 2 : 3
            [Popularity] => 8.8
        )

    [1] => Array
        (
            [Device Type] => Phone
            [Device] => iPhone 5 (5c, 5s)
            [Operating System] => iOS
            [Physical Size] => 10.0
            [Width] => 640
            [Height] => 1136
            [Device-width] => 320
            [Px Per Inch] => 326
            [Px Density] => 200% XHDPI
            [Aspect Ratio] => 40 : 71
            [Popularity] => 7.9
        )
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$
  1. Change $devicesArray to $devices. It is obvious enough that each device would contain an array of information.
  2. Change $namesArray to $deviceFields it is more meaningful.
  3. Change $deviceName to $deviceType. $deviceName seems to be misleading.
  4. Use $device instead of $d in the foreach loop, it is more meaningful.
  5. 'Device' is a field and should be the first field in $deviceFields. This is required for the following improvement.
  6. Use array functions to combine the arrays: array('Device Type' => $deviceType]) + array_combine($deviceFields, $device).

The function then becomes:

function convert($deviceType, $deviceFields, $devices) {
    $result = [];

    foreach ($devices as $device) {
        $result[] =
            ['Device Type' => $deviceType] +
            array_combine($deviceFields, $device);
    }

    return $result;
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ tnx, but, gives error : array_combine(): Both parameters should have an equal number of elements, so a minor correction to your code will be to add ['Device' => array_shift($device)] + before the array_combine.. \$\endgroup\$
    – Zlonki
    Mar 20, 2015 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.