0

Post Updated

I would like to create dynamic Array in PHP to provide the same output like static Array below, I have tried to do it with while statement but it doesn't work. Could you please provide me some tips?

I would like to use 2 values from MySQL and save them in to $to variable so first send_to1 (user account) and then value1 (amount)

<?php
$con=mysqli_connect("localhost","test","test","test");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM users");

$to=Array(

while($row = mysqli_fetch_array($result))
  {

$send_to1 => $value1,
$send_to2=> $value2
  }

);

mysqli_close($con);
?>
7
  • $to[$send_toX] = $valueX; ? Commented Nov 13, 2013 at 11:05
  • Arrays in PHP are always dynamic in every imaginable aspect. Describe what ooperation you would like to perform on an array! Commented Nov 13, 2013 at 11:08
  • 3
    Change $new_array[] = $row[] to $new_array[] = $row $row is already an array Commented Nov 13, 2013 at 11:08
  • replace print_r($new_array[] = $row[]); with $new_array[] = $row; Commented Nov 13, 2013 at 11:10
  • 1
    @user2511459 $to[$i]['valx'] = $row['valx']; ... but you need a counter here, or an id at least ... Commented Nov 13, 2013 at 11:17

3 Answers 3

4

It's as simple as just adding elements to the array:

//Setup blank array
$example = array();

//Create a loop, for example purposes.
foreach(range(0, 99) as $i){

    //Create random variable that we'll add to our array
    $randVar = mt_rand(1, 90000);

    //$i will be our array key/index
    $example[$i] = randVar;

}

//var_dump the array so you can see the structure / end result
var_dump($example);

You could also create it like so:

//Create random array key/index
$myRandKey = mt_rand(1, 90000);

//Create random variable value.
$myRandVar = mt_rand(1, 90000);

//Setup an array
$example = array(
    $myRandKey => $myRandVar
);

//Another array key that we'll add to our array
$secondKey = 'test';

//Add it
$example[$secondKey] = 'This is just an example!';

//Dump out the array
var_dump($example);

array_push will also work (using mysql_fetch_assoc, like in your example):

$example = array();
while($row = mysql_fetch_assoc($result)){
    array_push($example, $row);
}
var_dump($example);

In your particular example (since you added your code):

print_r($new_array[] = $row[]);

should be changed to:

print_r($new_array[] = $row);

In your code, I'd change it to:

$new_array = array();
while($row = mysqli_fetch_array($result)){
  $new_array[] = $row;
}

Or, if you want to key your array by a unique column (Primary key, for example):

$new_array = array();
while($row = mysqli_fetch_array($result)){
  $new_array[$row['id']] = $row;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Have a look at my updated answer. See code added at the end of the answer.
Also, if you get array(0), it could be because your query isn't returning any rows and/or your query is failing and you're not checking to see if you've got MySQL errors.
@user2511459 Where are you getting the send to variables from?
1

Look, this is so easy, you just need to pay more attention to the answers you're getting here.

Here is the simplest way you can do it:

$to = array();
$to[] = array($send_to1 => $value1);
$to[] = array($send_to2 => $value2);

while ( $row = mysqli_fetch_array($result) ) {
    $to[] = array($row['send_tox' => $row['valuex']);
}

You need to first understand how Arrays and Loops work in PHP, then try to make a dynamic array in a loop.

Comments

1

Actually you almost got it. It's lower A in 'array'

To initialize an empty array:

$to = array();

In your case (you already have some values), you can do:

$to = array(
   $send_to1 => $value1,
   $send_to2=> $value2
);

In either case, you can later add more elements doing

$to[$someOtherKey] = $someOtherValue;

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.