Jump to content

Recommended Posts

Hi,

It's been a few years since i've done any php coding and it seams like I have forgotten how to combine 2 arrays!

I am trying to combine 

This ....

 
Array
(
    [0] => Array
        (
            [0] => 6
            [1] => 12.25000000000000
            [2] => 14.50000000000000
        )

    [1] => Array
        (
            [0] => 7
            [1] => 12.43700000000000
            [2] => 13.12500000000000
        )
}

 

etc, etc

and this ....

Array
(
    [6] => 11.00000000000000
    [7] => 11.31200000000000
    [17] => 14.18700000000000
}

etc etc

to make ...

 
Array
(
    [0] => Array
        (
            [0] => 6
            [1] => 12.25000000000000
            [2] => 14.50000000000000
            [3] => 11.00000000000000
        )

    [1] => Array
        (
            [0] => 7
            [1] => 12.43700000000000
            [2] => 13.12500000000000
            [3] => 11.31200000000000
        )

    [2] => Array
        (
            [0] => 17
            [1] => 16.12500000000000
            [2] => 15.75000000000000
            [3] => 14.18700000000000
        )
}

 

I've tried 

$out2 = array();
foreach ($out as $value){
    $out2[] = array_merge($value, array($water[$key]));

}

but this seams to alter the value of the combined array value in the 3rd key.

Can anyone point to what I have done wrong?

 

Regards 

Link to comment
https://forums.phpfreaks.com/topic/307601-array-help/
Share on other sites

Not sure I agree with Notion's post.

First - line 1 seems irrelevant.

And from my look at your post OP, you have an array of individual arrays followed by an array of individual elements with numeric keys.  When you add the second array to the first array - how do you want to "fit" them in?  I think you will quickly run into duplicate keys and start losing matched data as I see it.

Do you want each element of array #2 to be a single entry in the first array? If so how do you want to handle the key of this new element?

Or do you want to add each element of array 2 as a one-element array in array 1 with a new sequentially assigned index for that new array item in array1?

Edited by ginerjm
Link to comment
https://forums.phpfreaks.com/topic/307601-array-help/#findComment-1560311
Share on other sites

Hi,

The arrays come from three sql queries. I have already combined two which output to a graph to show temperature over a 24 hour period, now I have added another temperature probe and  I want to add the third array to the existing one. 

I don't know a way of querying the db to produce a array like this

Array
(
    [0] => Array
        (
            [0] => 6    (Hour from db)
            [1] => 12.25000000000000  (Temp probe 1)
            [2] => 14.50000000000000  (Temp probe 2)
            [3] => 11.00000000000000  (Temp probe 3)
        )

etc etc..

Regards

Link to comment
https://forums.phpfreaks.com/topic/307601-array-help/#findComment-1560313
Share on other sites

Assuming the common identifier in the first array always has the first position:

<?php

foreach( $arr_1 as $k => $data ) {
  if ( isset( $arr_2[$data[0]] ) ) {
    $arr_1[$k][] = $arr_2[$data[0]];
  }
}

?>

This will add data from the second array to the first array

Edited by alpine
Description added
Link to comment
https://forums.phpfreaks.com/topic/307601-array-help/#findComment-1560315
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.