0

I am trying to insert batch query in code igniter, I am not able to make array_merge work. Don't know whats the problem. M getting blank array.

        $epin_amt = $this->input->post('amount');
            $qty = $this->input->post('qty');
            $data = array();
            for ($i = 0; $i <= $qty; $i++) {
                $array = array(
                    'epin'   => mt_rand(100000, 999999),
                    'amount' => $epin_amt,
                );
             array_merge($data, $array);
            }
print_r($data) ; // Produce : array( )
0

3 Answers 3

2

You have to assign the merged array back to your $data variable:

<?php

$epin_amt = /*$this->input->post('amount')*/ 5;
$qty = /*$this->input->post('qty')*/6;

$data = array();
for ($i = 0; $i <= $qty; $i++) {
    $array = array(
        'epin'   => mt_rand(100000, 999999),
        'amount' => $epin_amt,
    );
 $data = array_merge($data, $array);
}

print_r($data) ;
Sign up to request clarification or add additional context in comments.

2 Comments

But this time I am getting only 1 array . Array ( [epin] => 653552 [amount] => 100 )
Stack overflow recommends that you provide an expected result. There's no way I can know what you expect now, because looking at your code, all I see is a single $epin_amount, and a single $qty value. Please revise your question to show the expected result, because as it is, based on the wording of your question, you are getting answers that are correct, they just don't meet your needs.
2

array_merge returns array. You need something like this:

$result = array_merge($data, $array);

Comments

0

You are merging the arrays but it's not done by reference so you're throwing the resulting array away. array_push() it instead, that will keep adding the arrays to your $data array:

<?php
$epin_amt = 10;
$qty = 20;
$data = array();
for ($i = 0; $i <= $qty; $i++) {
    $array = array(
        'epin'   => mt_rand(100000, 999999),
        'amount' => $epin_amt,
    );
 array_push($data, $array);
}
print_r($data) ;

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.