0

I have an array comprising months (keys) and values See shortened example below. It is being generated by a legacy client system so I can only manipulate what I am presented with (not ideal).

$cost_data= array
            (
                [9] => 1110
                [10] => 111
                [8] => 100
                [7] => 200
                [6] => 690
                [11] => 222
            )

Note-the non linear keys represent months of the year.

I need to change the data structure to the format below and am not having much luck after several hours of trying different approaches and searching.

   $cost_data = array
   (
     array("9",1110),
     array("10",111),
     array("8",100),
     array("7",200),
     array("6",690),
     array("11",222)
    );  

Is there a simple built in function or does anyone know a neat method for achieving this transformation?

4
  • what is that you're getting? a print_r dump (string)? and then convert into array data type? Commented Nov 27, 2017 at 2:53
  • you spent several hours of trying different approaches, but don't include at least one of those different approaches? Why should we help when you don't actually show any effort other than posting? Commented Nov 27, 2017 at 2:57
  • @Ghost i am just showing the print_r output generated by the php array. Just thought it would be easier to explain that way as i cannot get the php code generating the array. So yes thats a print_r of $cost_data output from the other system Commented Nov 27, 2017 at 2:57
  • @Sean i have tried so many approaches with the methods based on array_combine, array_map, numerous foreach attempts etc and have not managed to solve the problem.. I think pasting all my attempts into the question would serve to confuse rather than help. Just figured someone would have seen this issue before and feel good about helping a novice, Commented Nov 27, 2017 at 3:03

2 Answers 2

1

Here is my go.

<?php
$cost_data= [
    9 => 1110,
    10 => 111,
    8 => 100,
    7 => 200,
    6 => 690,
    11 => 222,
];

asort($cost_data);  //sort

array_walk($cost_data,function(&$v,$k){
    $v = [$k,$v];
});

print_r(array_values($cost_data));

Outputs ( after some cleanup):

array (
  0 => array(9,1110),
  1 => array(10,111),
  2 => array(8,100),
  3 => array(7,200),
  4 => array(6,690),
  5 => array(11,222)
)

You can test it here.

http://sandbox.onlinephpfunctions.com/code/95557489eb19e9e34c55abef71accd1219682350

If you don't care about the keys, you can skip the array_values which resets the numeric indexes, and save a bit on performance. Without the array_values it looks like this

array (
  9 => array(9,1110),
  10 => array(10,111),
  8 => array(8,100),
  7 => array(7,200),
  6 => array(6,690),
  11 => array(11,222)
)

So, like I said you can save a tiny bit on performance by leaving it out. I didn't measure it but I would wager it's a bit faster then array_map, as it does pass by reference.

update

I timed both the array_map and mine array_walk. And for 30k iterations

Array Walk Time: 0.045436
Array Map Time: 0.047619 

I would say their is no significant difference between the two.

Sign up to request clarification or add additional context in comments.

6 Comments

sure, I almost thought the other guy had the same one when I went to post it. But then I realized that was array_map ... lol
just an extension to that question the output gives me an array soted as followsarray ( array(11,222 ),array(9,1110), array(10,111), );
what would you suggest to get it into the form array ( array(9,1110), array(10,111), array(11,222), ie sorted by the second level value. Thanks again
just add asort($cost_data); before converting it, so just above the array_walk it's easier to sort it when its just a 1 level array, and a in the asort means associative so it preserves the keys, I'll add it in my answer. -note- sorting is pass by reference so there is no assignment of a return value, in other words you don't need to do this $cost_data = asort($cost_data);, literally you just do this asort($cost_data);
that does look much easier. Thank you. I should really make yours the accepted answer now. Its much much more precise.
|
1
$cost_data = array(
    9 => 1110,
    10 => 111,
    8 => 100,
    7 => 200,
    6 => 690,
    11 => 222
);


$converted = array_map(function($k, $v) { return array($k, $v); }, array_keys($cost_data), $cost_data);

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.