Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7060051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:18:22+00:00 2026-05-28T04:18:22+00:00

I have an array which appears like the one below. I want to slice

  • 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][] = '';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:18:23+00:00Added an answer on May 28, 2026 at 4:18 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which is a list of domains, I want to print
i have an array which could look like this: $config[$name][$array]['key'] = 'value'; // here
I have an indexed array which I've generated from an associative array with this
i would like to have an array of objects in excel that call one
I have an array which contains another array Would I notate it this way?
I have an array which has the contents as the result of an sql
I have an array which i need to sort its elements by occurrence then
I have an array which contains around 50-200 hyperlinks. How can I pass this
Lets say I have an array which has n dimensions. Now in order to
I have a string say string s =C:\\Data , I have an array which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.