0

I have an array which appears like the one below. I want to slice it in half and create two new arrays from the two halves. However, if I use array_slice, it always returns null... I don't understand why.

array
  'pic' => 
    array
      0 => string '740' (length=3)
      1 => string '741' (length=3)
      2 => string '742' (length=3)
      3 => string '748' (length=3)
  'alt' => 
    array
      0 => string '' (length=0)
      1 => string '' (length=0)
      2 => string 'Test caption 1' (length=14)
      3 => string 'Test caption 2' (length=14)

The arrays I need should retain the keys, just sliced in half so I have two complimentary halves. For example the first half should look like:

array
  'pic' => 
    array
      0 => string '740' (length=3)
      1 => string '741' (length=3)
  'alt' => 
    array
      0 => string '' (length=0)
      1 => string '' (length=0)

and the second half:

array
  'pic' => 
    array
      0 => string '742' (length=3)
      1 => string '748' (length=3)
  'alt' => 
    array
      0 => string 'Test caption 1' (length=14)
      1 => string 'Test caption 2' (length=14)

thanks

ps - to Mike, this is what I'm using to make the array, actually it's constructed from other arrays itself:

$lodgepics = get_field('accommodation_rooms');
$featuredpics = get_field('featured_pics');
$showcasepics = array();
foreach ($featuredpics as $featuredpic) {
    if (isset($featuredpic['featured_pic'])&&!empty($featuredpic['featured_pic'])) $showcasepics[pic][] = $featuredpic['featured_pic'];
    if (isset($featuredpic['featured_alt'])) $showcasepics[alt][] = $featuredpic['featured_alt'];
else $showcasepics[alt][] = '';
}
foreach ($lodgepics as $lodgepic) {
    if(isset($lodgepic['accommodation_roomphoto'])&&!empty($lodgepic['accommodation_roomphoto'])) $showcasepics[pic][] = $lodgepic['accommodation_roomphoto'];
    if(isset($lodgepic['accommodation_roomname'])&&!empty($lodgepic['accommodation_roomname'])) $showcasepics[alt][] = $lodgepic['accommodation_roomname'];
    else $showcasepics[alt][] = '';
}

3 Answers 3

2

These are actually arrays of arrays. The primary array is an associative array, so you'll need to loop through all the keys of that array in order to get your slices:

$slice1 = array();
$slice2 = array();
foreach ($array as $key => $value)
{
    $slice1[$key] = array_slice($array[$key], ...);
    $slice2[$key] = array_slice($array[$key], ...);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This appears to be a partial solution, the '...' represents the offset and lengths that needs to be passed to the array_slice call.
Sorry, should have specified that. Didn't know what the expected behavior should be with an odd number of elements, so I didn't attempt it.
1

Hard for me to grasp the need to do something like what you are proposing, if it were me I wouldn't populate an array using your structure, I would create an array like this:

array
  0 => 
    array
      'pic' => string '740' (length=3)
      'alt' => string '' (length=0)
  1 => 
    array
      'pic' => string '741' (length=3)
      'alt' => string '' (length=0)
  2 => 
    array
      'pic' => string '742' (length=3)
      'alt' => string 'Test Caption 1' (length=0)
  3 => 
    array
      'pic' => string '748' (length=3)
      'alt' => string 'Test Caption 2' (length=0)

This array structure keeps the image attributes together, rather than keeping them separate.

tomtheman5 has 80% there, but left out the offset, and length params to the array_slice calls that need to be passed to get the correct slices. However he does make a good point, if you stick with your array structure, you may run into issues if you do not have matching elements in each 'pic' and 'alt' arrays. If this is not a concern, then consider the following snippet:

$slice1 = array();
$slice2 = array();    

foreach ($array as $key => $value) {

    $arrayCount = count($array[$key]);
    $arrayHalfCount = ($arrayCount / 2);

    $slice1[$key] = array_slice($array[$key], 0, $arrayHalfCount);
    $slice2[$key] = array_slice($array[$key], $arrayHalfCount);
}

-- Edit --

$lodgepics = get_field('accommodation_rooms');
$featuredpics = get_field('featured_pics');
$showcasepics = array();
foreach ($featuredpics as $featuredpic) {
    if ((isset($featuredpic['featured_pic'])) && (!empty($featuredpic['featured_pic']))) {
        $currentPic['pic'] = $featuredpic['featured_pic'];
        $currentPic['alt'] = (isset($featuredpic['featured_alt'])) ? $featuredpic['featured_alt'] : ''; 
        $showcasepics[] = $currentPic;
}
}

foreach ($lodgepics as $lodgepic) {
    if ((isset($lodgepic['accommodation_roomphoto'])) && (!empty($lodgepic['accommodation_roomphoto']))) {
        $currentPic['pic'] = $lodgepic['accommodation_roomphoto'];    
        $currentPic['alt'] = (isset($lodgepic['accommodation_roomname'])) ? $lodgepic['accommodation_roomname'] : '';
        $showcasepics[] = $currentPic;
}
}

$showcasepicsCount = count($showcasepics);
$showcasepicsHalfCount = ($showcasepicsCount / 2);

$slice1 = array_slice($showcasepics, 0, $showcasepicsHalfCount);
$slice2 = array_slice($showcasepics, $showcasepicsHalfCount);

3 Comments

Hi Mike, thanks your advice is very helpful I've included the code I use to construct the array in my original post, can you check it? maybe I can simply tweak the array and make it easier to slice and use - thanks
@Fulvio: Updated post with proposed way to build your image array.
1

array_chunk — Split an array into chunks

$array1 = array("1" => "green", "2" => "brown", "3" => "blue", "4" => "black");
$array2 = array("5" => "yellow","6" => "orange","7"=> "violet","8"=> "white");

$arraylength1   = ceil(count($array1)/2);
$res1           = array_chunk($array1,$arraylength1);
$arraylength2   = ceil(count($array2)/2);
$res2           = array_chunk($array2,$arraylength2);

echo "<pre>";
print_r($res1);
print_r($res2);
echo "</pre>";
exit;

Try This.

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.