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 8553191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:35:45+00:00 2026-06-11T14:35:45+00:00

I have this array $arr = array( ‘one’ => array( ‘slidertitle’ => ‘lorem ipsum’,

  • 0

I have this array

$arr = array(
    'one' => array(
    'slidertitle' => 'lorem ipsum',
    'sliderlocation' => 'http://localhost/images/1.jpg',
    'sliderdescription' => 'this is a good lorem ipsum image',
    'sliderposition' => 1
    ),
    'two' => array(
    'slidertitle' => 'second slider',
    'sliderlocation' => 'http://localhost/images/2.jpg',
    'sliderdescription' => 'this space was reserved for a link source code here',
    'sliderposition' => 2
    ),
    'six' => array(
    'slidertitle' => 'sixth slider',
    'sliderlocation' => 'http://localhost/images/6.jpg',
    'sliderdescription' => 'this is the sixth slider,like,really!',
    'sliderposition' => 6
    )
);

which i need to look like this

$arr = array(

     'two' => array(
    'slidertitle' => 'second slider',
    'sliderlocation' => 'http://localhost/images/2.jpg',
    'sliderdescription' => 'this space was reserved for a link source code here',
    'sliderposition' => 2
    ),

    'six' => array(
    'slidertitle' => 'sixth slider',
    'sliderlocation' => 'http://localhost/images/6.jpg',
    'sliderdescription' => 'this is the sixth slider,like,really!',
    'sliderposition' => 6
    ),
      'one' => array(
    'slidertitle' => 'lorem ipsum',
    'sliderlocation' => 'http://localhost/images/1.jpg',
    'sliderdescription' => 'this is a good lorem ipsum image',
    'sliderposition' => 1
    )
);

I am attempting to do that by defining the expected array structure and introducing a dummy array.I then chunk the array and merge each chunk to the array format and i plan to finally unset the dummy and i am left with the array i want and in the order i want.

$arrayFormat = array(
   'dummy' => array(
    'slidertitle' => 'xxxx',
    'sliderlocation' => 'xxxxxxx',
    'sliderdescription' => 'xxxxxx',
    'sliderposition' => 0
    )
);
$arrayLength = count($arr);
$afterChunk = array_chunk($arr,$arrayLength);
$one = $afterChunk[0][0];
$two = $afterChunk[0][1];
$mergedArray = array_merge($arrayFormat,$one);
$secondMergedArray = array_merge($mergedArray,$two);
echo '<pre>';
print_r($secondMergedArray);
echo '</pre>';

The problem is array_chunk() does not include the key of the array so i am getting

Array (
    [dummy] => Array
        (
            [slidertitle] => xxxx
            [sliderlocation] => xxxxxxx
            [sliderdescription] => xxxxxx
            [sliderposition] => 0
        )

    [slidertitle] => second slider
    [sliderlocation] => http://localhost/images/2.jpg
    [sliderdescription] => this space was reserved for a link source code here
    [sliderposition] => 2 )

when i print_r($secondMergedArray);.is there something that can be done to array_chunk() to include the array key or is there any other array function that can help me get individual array inclusive of the key?.

  • 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-06-11T14:35:46+00:00Added an answer on June 11, 2026 at 2:35 pm

    It’s really hard to tell what you’re wanting in terms of how to sort the elements. You’ve not been very clear in the question. There has to be something in the array that you know what order it needs to be.

    In the absence of any clues as to what that is, I’m going to assume you want to specify the order of the array keys manually.

    So, the current array is array('one'=>... , 'two'=>... , 'six'=>... ) and you want to sort those keys in an order you want to specify manually.

    The solution is to use the uksort() function, along with a separate array specifying your sort order:

    $arr = ... //input array as specified in the question
    $sortOrder = array('two','one','six');
    
    uksort($arr, function ($a, $b) use ($sortOrder) {
        $sortMe = array_flip($sortOrder);
        if ($sortMe[$a] == $sortMe[$b]) { return 0; }
        return ($sortMe[$a] < $sortMe[$b]) ? -1 : 1;
    });
    
    print_r($arr);
    

    Outputs your array in ‘two’,’one’,’six’ order. Change the $sortOrder array as required.

    Hope that helps.

    Note: the syntax I’ve provided above only works in PHP 5.3 and above. (if you’re using an older version, you need to upgrade)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Noob question here! I have an array with hashes that looks like this: arr
I have this PHP code - <?php for($i=1; $i<=1000; $i++) { $array=array(); $array[$i]=54*$i; $arr=array($array[$i].,);
i have this code : NSMutableArray *arr = [NSMutableArray array]; for (int i =
I have array like this var Arr = [ 'h78em', 'w145px', 'w13px' ] I
I have an array like this var records:Object = {}; var arr:Array = [
I have an array. var arr =[23,45,78,89]; I want to delete 78 from this
I have this array: arr = [/Users/user/Documents/public/170/41/texts/10.txt, /Users/user/Documents/public/170/41/texts/11.txt, /Users/user/Documents/public/170/41/texts/12.txt, /Users/user/Documents/public/170/41/texts/14.txt, /Users/user/Documents/public/170/41/texts/15.txt, /Users/user/Documents/public/170/41/texts/18.txt, /Users/user/Documents/public/170/41/texts/6.txt, /Users/user/Documents/public/170/41/texts/8.txt]
I have this array: var arr = []; arr.push({name:k1, value:abc}); arr.push({name:k2, value:hi}); arr.push({name:k3, value:oa});
If I have for example this array-variable: var arr=[ [1,2,3] [4,5,6] ] How can
I have this code: val arr: Array[Int] = ... val largestIndex = { var

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.