0

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
    );
}
5
  • 5
    Can I ask you why you want to have an array consisting of arrays with only one value? To me the last result looks way more logic. What am I missing? Commented May 25, 2022 at 7:59
  • And just using the $dir array rather than putting it in another array i,e, $aManipulations = array( array($dir) ); would be even more logical. why bury an array in an array when there is only one array Commented May 25, 2022 at 8:02
  • I updated the code above, maybe that helps? Commented May 25, 2022 at 8:06
  • The one like it is in manually array Commented May 25, 2022 at 8:08
  • a) manually filed array b) automatically filled array c) b) must be like a) better? Commented May 25, 2022 at 8:10

2 Answers 2

3

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Your previous code worked, all you had to do was change the : String return type into : Array return type
2

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.