Home > database >  How to modify an array to get another array
How to modify an array to get another array

Time:05-25

I have a manually filled array with directory names that are used to run a function for image modification. Number and names of these directories changes frequently, so to simplify it I scan the main directory to get the names of the subdirectories. The problem is that the format of this directory array is not as I need it.

This is the manually filled array:

$aManipulations = array(
    array('x109_110x110'),
    array('x1380'),
    array('x1800'),
    array('x2012'),
    array('x387'),
    array('x40'),
    array('x561'),
    array('x60'),
    array('x640'),
    array('x669'),
    array('x671'),
    array('x700'),
);

Result:

Array
(
    [0] => Array
        (
            [0] => x109_110x110
        )

    [1] => Array
        (
            [0] => x1380
        )

    [2] => Array
        (
            [0] => x1800
        )

    [3] => Array
        (
            [0] => x2012
        )

    [4] => Array
        (
            [0] => x387
        )

    [5] => Array
        (
            [0] => x40
        )

    [6] => Array
        (
            [0] => x561
        )

    [7] => Array
        (
            [0] => x60
        )

    [8] => Array
        (
            [0] => x640
        )

    [9] => Array
        (
            [0] => x669
        )

    [10] => Array
        (
            [0] => x671
        )

    [11] => Array
        (
            [0] => x700
        )

)

Other array generated by scan dir:

$dir = glob('images/onthefly/products/' . '*', GLOB_ONLYDIR);
$dir = str_replace("images/onthefly/products/", "", $dir);
$aManipulations = array(
    array($dir),
);

Result:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => x109_110x110
                    [1] => x1380
                    [2] => x1800
                    [3] => x2012
                    [4] => x387
                    [5] => x40
                    [6] => x561
                    [7] => x60
                    [8] => x640
                    [9] => x669
                    [10] => x671
                    [11] => x700
                )

        )

)

So what has to be done to transform the automatically filled array?

###################### Update ######################

foreach ($aManipulations as $aManipulation) {
    smarty_modifier_manipulateImage(
            DIR_WS_ORIGINAL_IMAGES . $aQuery['products_image'],
            $aManipulation[0],
            isset($aManipulation[1]) ? $aManipulation[1] : '',
            isset($aManipulation[2]) ? $aManipulation[2] : false
    );
}

CodePudding user response:

From what I understand, you need to manipulate your array generated by scan dir as that of the manually filled array.

As you can see the $dir is an array so you can loop through it to access individual directory and subdirectory names. You just have to create an array with that name.

You can do it using for loop.

$requiredArray = array();
foreach ($dir as $name) {
array_push($requiredArray, array($name));
}
print_r($requiredArray);

Or Using array_map:

$dir = array('x109_110x110','x1380','x1800','x2012','x387','x40','x561','x60','x640','x669','x671','x700');
$func = function(String $value): Array {
    return array($value);
};

print_r(array_map($func, $dir));

CodePudding user response:

If you must convert the glob() generated array into the array structure you have used before then

foreach( $dir as $d ) {
    $aManipulations[][] = $d;
}

will do that for you

  • Related