0

I have some issue to re-arrange array in php.

Array
(
    [N] => Array
        (
            [68] => sssssss ttttttt
            [69] => uuuuuu vvvvvvvv
        )

    [D] => Array
        (
            [05] => zzzzzzzz zzzzzzzz
            [07] => tttttttttttt ttttttttttt
        )

    [P] => Array
        (
            [88] => yyyyyyy zzzzzzzz
        )

    [C] => Array
        (
            [04] => wwwwww wwwwwww
            [06] => iiiiiii iiiiiiii
            [41] => zzzzzzzzzz zzzzzzzzzz
        )

)

What I want is the following...

Array
(
    [N] => Array
        (
            [68] => sssssss ttttttt
            [69] => uuuuuu vvvvvvvv
            // only C are added here with N
            [04] => wwwwww wwwwwww
            [06] => iiiiiii iiiiiiii
            [41] => zzzzzzzzzz zzzzzzzzzz

        )

    [D] => Array
        (
            [05] => zzzzzzzz zzzzzzzz
            [07] => tttttttttttt ttttttttttt
        )

    [P] => Array
        (
            [88] => yyyyyyy zzzzzzzz
        )

    [C] => Array
        (
            [04] => wwwwww wwwwwww
            [06] => iiiiiii iiiiiiii
            [41] => zzzzzzzzzz zzzzzzzzzz
        )
)

I need the C-Element to be added to N, while all the remaining stay as they are, including the C itself.

How do I accomplish that?

0

3 Answers 3

2

Pretty straightforward stuff; just add the two arrays together like this:

// add C to N
$arr['N'] += $arr['C'];

If you don't know what the key names are and you just want to target the first and last item:

reset($arr); $first = key($arr);
end($arr); $last = key($arr);

$arr[$first] += $arr[$last];
Sign up to request clarification or add additional context in comments.

4 Comments

It is not really problem, but what it output is just the first-array added together N+C...but not loop through it...any way i learn some thing from your code...Thanks.
@MrInternet I'm afraid I don't follow; print_r($arr) should give exactly what you want as can be seen here.
No really, print_r gives only the sum of $arr['N'] += $arr['C']; But what about the others? like ['D'] ['P']? they are not included.
@MrInternet Wait, did you do print_r($arr['N'] += $arr['C']);? Check the earlier codepad link I sent you.
1

This is actually very simple. All you need to do is loop through the last array and add those key value pairs to the first. Here you go:

    //---- $mainArray is your array.


foreach ($mainArray['C'] as $key => $value)
{
      $mainArray['N'][$key] = $value;
}

print_r($mainArray);

Comments

0

Try this:

$last_array = $main_array[count($main_array) - 1];
$new_array = array_merge($main_array[0], $last_array);
array_shift($main_array);
array_unshift($main_array, $new_array);
var_dump($main_array);

6 Comments

Thanks for your quick reply, and put my code in very clear format, But unfortunately the code above seems not working, No out put (empty), but i am trying in different approach, may be some other idea?
array (size=4) 0 => null 'D' => array (size=2) 16 => string 'BENCHABLA BENCHABLA' (length=19) 17 => string 'BOULOUDINATS BOULOUDINATS' (length=25) 'P' => array (size=1) 14 => string 'AGAR AGAR' (length=9) 'C' => array (size=1) 13 => string 'ACHAR Sababa' (length=12)
The only problem I'm still having is the merge is resetting the key values. Is that an issue?
yes, I think so....I mean merge doesn't seems working, as you see the output i post...the first value [0] become Null...some other way please? I am trying too here..
by the way, to get the last array key we can use this $last_array = end($main_array);
|

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.