Home > Software design >  Creating multidimensional array in php and slicing it for looping
Creating multidimensional array in php and slicing it for looping

Time:07-27

I'm trying to create a simple multidimensional array in php so that I can slice it (to get 4 items from the array) and then loop on it so that I can display images with their names/numbers one at a time.

The main issue right now is that I'm trying to dump the sliced array on the frontend and getting an illegal offset error. I think my logic is correct, but is my array not formatted correctly for it?

$urls = array(
    [0] => array(
            "url" =>  '123.jpg',
            "name" => 'Cat_123',
            "number" => 123,
        ),
    [1] => array(
        "url" => '265.jpg',
        "name" => 'Cat_265',
        "number" => 265
    ),
    [2] => array(
        "url" => '121.jpg',
        "name" => 'Cat_121',
        "number" => 121,
    ),
    [3] => array(
        "url" => '245.jpg',
        "name" => 'Cat_245',
        "number" => 245,
    ),
    [4] => array(
        "url" => '4445.jpg',
        "name" => 'Cat_4445',
        "number" => 4445,
    ),
    [5] => array(
        "url" => '3332.jpg',
        "name" => 'Cat_3332',
        "number" => 3332,
    ),
);

$multid_array = array($urls);

$sliced_array = array();  

foreach ($multid_array as $sub_array) {
    $sliced_array[] = array_slice($sub_array, 0, 4);
}

CodePudding user response:

Remove [, ] from index numbers. Also shown is an alternative to array_slice().

$urls = array(
    0 => array(
            "url" =>  '123.jpg',
            "name" => 'Cat_123',
            "number" => 123,
        ),
    1 => array(
        "url" => '265.jpg',
        "name" => 'Cat_265',
        "number" => 265
    ),
    2 => array(
        "url" => '121.jpg',
        "name" => 'Cat_121',
        "number" => 121,
    ),
    3 => array(
        "url" => '245.jpg',
        "name" => 'Cat_245',
        "number" => 245,
    ),
    4 => array(
        "url" => '4445.jpg',
        "name" => 'Cat_4445',
        "number" => 4445,
    ),
    5 => array(
        "url" => '3332.jpg',
        "name" => 'Cat_3332',
        "number" => 3332,
    ),
);

foreach ($urls as $k => $v) {
    if ($k < 4) $sliced_array[] = $v;
}

echo '<pre>'.print_r($sliced_array, 1).'</pre>';

CodePudding user response:

Extract a slice of your array. Get 4 items. No need to loop. Pay attention to the syntax to create the array:

$urls = array(
    array(
        "url" =>  '123.jpg',
        "name" => 'Cat_123',
        "number" => 123,
    ),
    array(
        "url" => '265.jpg',
        "name" => 'Cat_265',
        "number" => 265
    ),
    array(
        "url" => '121.jpg',
        "name" => 'Cat_121',
        "number" => 121,
    ),
    array(
        "url" => '245.jpg',
        "name" => 'Cat_245',
        "number" => 245,
    ),
    array(
        "url" => '4445.jpg',
        "name" => 'Cat_4445',
        "number" => 4445,
    ),
    array(
        "url" => '3332.jpg',
        "name" => 'Cat_3332',
        "number" => 3332,
    ),
);

$sliced_array = array_slice($urls, 0, 4);

var_dump($sliced_array);
  • Related