开发者

Pushing a value onto the end of an array

开发者 https://www.devze.com 2023-02-24 09:18 出处:网络
I have the following array, it\'s full of gibberish (I got bored using lorem ipsum so I started typing random stuff -- I\'m testing ¬_¬).

I have the following array, it's full of gibberish (I got bored using lorem ipsum so I started typing random stuff -- I'm testing ¬_¬).

I used mysqli_fetch_array to fetch this array.

[array1]

Array
(
    [0] => Array
        (

            [title] => this is a new thread, and it is great and it should work

            [thread_id] => 27

            [content] => <p>hello, how are you? and what are you doing y'all and this should work</p> 

            [username] => umar

            [author_id] => 12

            [tags] => Array
                (
                    [0] => lorem
                )

        )

    [1] => Array
        (

            [title] => this is my second thread and it should work fine, just fine

            [thread_id] => 28

            [content] => <p>this is is good, that I think it should have a thread of its own, don't you think?</p> 

            [username] => umarrazzaq

            [author_id] => 12

            [tags] => Array
                (
                    [0] => thread
                    [1] => less
               开发者_开发技巧 )

        )

)

I have another array [array2]:

    Array ( [0] => Array (  [replies] => 2  [id] => 27 )
 [1] => Array (  [replies] => 1  [id] => 28 ) ) 

I want to push this second array onto the first array, where the IDs match.

e.g.

So the first array will become:

 [0] => Array
            (

                [title] => this is a new thread, and it is great and it should work

                [thread_id] => 27

                [content] => <p>hello, how are you? and what are you doing y'all and this should work</p> 

                [username] => umar

                [author_id] => 12

                [tags] => Array
                    (
                        [0] => lorem
                    )

                **[replies] => 2**

            )

I've tried passing by reference and using array_push in a foreach loop, but it only does it for one.


try this:

foreach($array1 as $key=>&$arr){
    $arr['replies'] = $array2[$key]['replies']
}


It seems that you could do this also with a JOIN on your table

foreach($array1 as $key => $value)
    {
     $array1[$value['thread_id']] = $value;

    }

    foreach($array2 as $value)
    {
      array_push($array1[$value['id']], $value);
    }


if i am getting your question correct then you want to merge array where array2.id and array1.thread_id is same. In that case you can't use array_merge. Unless you change your second array as something like below.

array( [$thead_id] => Array ( [replies] => 2) .... ) then you can use array_merge easily .

0

精彩评论

暂无评论...
验证码 换一张
取 消