-1

Hi there I have an key => value array. Key is numeric index and value is associative array. I want to add new element at a particular position and wants to move the existing one ahead. My array looks like

Array
(
 [0] => Array
    (
        [States ] => States
        [Call Count] => Call Count
        [Leads] => Leads
    )

 [1] => Array
    (
        [States] => AK
        [Call Count] => 0
        [Leads] => 
    )

 [2] => Array
    (
        [States] => AL
        [Call Count] => 0
        [Leads] => 
    )

 [3] => Array
    (
        [States] => AR
        [Call Count] => 0
        [Leads] => 
    )
)

Now I want to insert one index like 2=>array('States'=>'ABC','Call Count' =>5, 'Leads'=>2) and wants index 2 to become 3 and 3 to become 4..... When I use

 array_slice($outboundSummaryData, 0, 2, true) +
            array('States ' => 'States', 'Call Count' => 'Call Count', 'Leads' => 'Leads') +
            array_slice($outboundSummaryData, 2, count($outboundSummaryData) - 2, true);

It overrides my 2 index.

When I use

array_splice($outboundSummaryData, 2, 0, array('States ' => 'States', 'Call Count' => 'Call Count', 'Leads' => 'Leads'));

It insert my new array elements as main indexes of the array.

Any Help?

Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
  • why are you using associative array in the first place? having this as a normal array will make array_splice do what you want – Fikri Marhan Jun 10 '13 at 06:18
  • You'd better use another data structure: list is what you need, not array. – user4035 Jun 10 '13 at 06:19
  • It sounds like you want a sorted array. What are the criteria? – Gordon Jun 10 '13 at 06:20
  • @FikriMarhan Its a db result so I have to do with it :-( – Awais Qarni Jun 10 '13 at 06:21
  • @Gordon Simply wants to add new elements in array as sub array and move existing one ahead – Awais Qarni Jun 10 '13 at 06:22
  • @AwaisQarni if there is no sort order, the order is arbitrary, then you can simply append to the end. but apparently it's important for you to insert at offset 2, so how do you know that the array should be at offset 2? – Gordon Jun 10 '13 at 06:26
  • @Gordon I need it at specific location. You can say its hardcoded – Awais Qarni Jun 10 '13 at 06:27
  • @hakre Thank you for the downvote. I have already searched but my creteria is different. – Awais Qarni Jun 10 '13 at 06:30
  • @hakre Please read question content before showing your expertise for downvoting – Awais Qarni Jun 10 '13 at 06:31
  • @AwaisQarni: Your criteria *might* be different (depends a bit where you want to split hair here) however different criteria do not needlessly qualify for a different question. – hakre Jun 10 '13 at 06:34
  • 1
    Also this one: http://stackoverflow.com/questions/10977812/how-to-splice-an-array-to-insert-array-at-specific-position – Gordon Jun 10 '13 at 06:36
  • Also after re-reading your question it's obvious you made a little mistake not understanding how inserting an array inside another array of array works. You just made a little mistake, the method work as announced. @Hendriq already wrote that to you about 15 minutes ago. – hakre Jun 10 '13 at 06:36

4 Answers4

1

I made it work with array_splice. And your datastructure can stay the same

$test = array(
    array('apple', 'juice'),
    array('ice', 'cream'),
    array('tea', 'pot'),
);

var_dump($test);

array_splice($test, 2, 0, array(2 => array('coffee', 'milk')));

var_dump($test);

Prety straightforward. The array that is added also needs the new index

hakre
  • 193,403
  • 52
  • 435
  • 836
MKroeders
  • 7,562
  • 4
  • 24
  • 39
1
$new_array = array_merge( 
   array_slice($outboundSummaryData, 0, 2, true),
   array( 
          array(
                'States' => 'States',
                'Call Count' => 'Call Count', 
                'Leads' => 'Leads'
               ) 
        ),
   array_slice($outboundSummaryData, 2, count($outboundSummaryData) - 2, true)
);

Result:

Array
(
    [0] => 0
    [1] => 1
    [2] => Array
        (
            [States] => States
            [Call Count] => Call Count
            [Leads] => Leads
        )

    [3] => 2
    [4] => 3
)
Andrey Volk
  • 3,513
  • 2
  • 17
  • 29
0

well.. you could always use splice and append...

$arr = [0=>"a",1=>"b",2=>"c"];
$splice_value = 2;
$spliced_arr = array_splice($arr, $splice_value);
for($i=$splice_value; $i < count($arr); $i++)
{
   $spliced_arr[] = $arr[$i];
}
MKroeders
  • 7,562
  • 4
  • 24
  • 39
0

if you dont mind changing from associative array to a list

$outboundSummaryData = array_splice(
     array_values($outboundSummaryData), 
     2, 0, array('States ' => 'States', 'Call Count' => 'Call Count', 'Leads' => 'Leads')
);
Fikri Marhan
  • 359
  • 1
  • 9