0

I would like to create an associative array from another array using a foreach loop. The first array only hold a set of id to retrieve data from mysql. Then, I would like to add more elements to the array.

//Array holding all the user_id
$user_id = array("0"=> 111, "1"=>222, "2"=>333);

//The user_id is used to return the data from another table
$sql = "SELECT first_name, last_name, age 
FROM user WHERE user_id = ?";

$statement = $DB->link->prepare($sql);

$user_data = array();
foreach($user_id as $user)
{
    $statement->bind_param("s", $user);
    $statement->execute();

    if($rs = $statement->get_result())
    {
        while($row = $rs->fetch_assoc())
        {   
            //Is it possible to do something like this?
            $user_data[$user]['id'] = $user;
            $user_data[$user]['first_name'] = $row['first_name'];
            $user_data[$user]['last_name'] = $row['last_name'];
            $user_data[$user]['age'] = $row['age'];
        }
    }   
}

How can I get the array to look like this array here?

$first_array = array(
    array(
        "user_id" => 111,
        "first_name"=>"Ben",
        "last_name"=>"White",
        "age"=>"43"),
    array(
        "user_id" => 222,
        "first_name"=>"Sarah",
        "Benning"=>"37",
        "age"=>"13"
    )
);
1
  • Have you tried running this code? What was the problem with it? Commented Oct 15, 2014 at 9:49

3 Answers 3

1

It's not a good practice to make database queries in a foreach loop.

You can use the following instead:

//Array holding all the user_id
$user_id = array("0"=> 111, "1"=>222, "2"=>333);
//The user_id is used to return the data from another table
$sql = "SELECT user_id, first_name, last_name, age 
FROM user WHERE user_id in (".implode(',', $user_id).")";

$statement = $DB->link->prepare($sql);

$statement->execute();

if($rs = $statement->get_result())
{
    while($row = $rs->fetch_assoc())
    {   
        //Is it possible to do something like this?
        $first_array[] = $row;
    }
}

If you want the field user_id to be id then you can change $sql var:

$sql = "SELECT user_id as id, first_name, last_name, age 
    FROM user WHERE user_id in (".implode(',', $user_id).")";
Sign up to request clarification or add additional context in comments.

Comments

0

I think an array_push might do the trick:

$first_array = array();    
while($row = $rs->fetch_assoc())
    {   
        $user_data['id'] = $user;
        $user_data['first_name'] = $row['first_name'];
        $user_data['last_name'] = $row['last_name'];
        $user_data['age'] = $row['age'];
        array_push($first_array, $user_data);
    }

Comments

0

The code in your post seems correct, but $user_data will look like this :

$user_data = array(
    "111" => array(
        "id" => 111,
        "first_name"=>"Ben",
        "last_name"=>"White",
        "age"=>"43"),
    "222" => array(
        "id" => 222,
        "first_name"=>"Sarah",
        "Benning"=>"37",
        "age"=>"13"),
    "333" => array( ... )
);

Comments

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.