Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8395251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:15:10+00:00 2026-06-09T20:15:10+00:00

I have an array built from a database query. Based on the values posuition

  • 0

I have an array built from a database query. Based on the values posuition with the array I need to assign another string to it.

I thought an if statement within a foreach loop would be the way forward but I’m having some trouble.

Below is my code……

$test = array(
            array("test", 1),
            array("test2", 2),
            array("test4", 4),
            array("test5", 5),
            array("test3", 3),
            array("test6", 6)
            );


foreach($test as $t) {
if($t[1]==1){
    array_push($t, "hello World");
    }
}
print_r$test);

Everything seams to work other than the array_push. If i print_r($test) after the loop nothing has been added.

Am I doing something monumentally stupid here?…

This is what I get if i print_r($test)

Array
(
[0] => Array
    (
        [0] => test
        [1] => 1
    )

[1] => Array
    (
        [0] => test2
        [1] => 2
    )

[2] => Array
    (
        [0] => test4
        [1] => 4
    )

[3] => Array
    (
        [0] => test5
        [1] => 5
    )

[4] => Array
    (
        [0] => test3
        [1] => 3
    )

[5] => Array
    (
        [0] => test6
        [1] => 6
    )

)

I would be expecting test 1 to have a 3rd value in there called “hello world”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:15:11+00:00Added an answer on June 9, 2026 at 8:15 pm

    Foreach loop works with a copy of an array. That’s why if you want to change the original array, you should use reference.

    foreach($test as &$t) {
       if($t[1]==1){
          array_push($t, "hello World"); // or just $t[] = "hello World";
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array built from the URL of a webpage. If an item
I have built up an array of objects, created from a class, I wrote
I have built a custom hashmap using two arrays. One contains keys another values.
I have an SQLite database from which I need to delete records periodically. What
I have pages of data, pulled from a database. I need to be able
I have this $data array: (built on a shell, not a form) (debugged here)
I have an sql database. In this database is 3 tables I need to
I'm wondering how to query a database using an array, like so: $query =
I have built a series of views in a PostgreSQL database that includes a
I would like to have a custom function to insert/edit/delete records from a database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.