0

i am getting array output as:

Array ( [0] => Array ( [0] => 000000 [1] => 000400 [2] => 000450 ) ) Array ( [0] => Array ( [0] => 000350 [1] => 000400 ) ) 

i want output like this:

Array ( [0] => Array ( [0] => 000000 [1] => 000400 [2] => 000450 ) ) Array ( [1] => Array ( [0] => 000350 [1] => 000400 ) ) 

my php code is:

$select = mysql_query("SELECT ac_no, cust_name, GROUP_CONCAT(install_amt SEPARATOR ',') as installamt FROM ankali_slabpay  WHERE agent_id=$agent_id GROUP BY ac_no");
while($row1 = mysql_fetch_array($select)){
$a = $row1['installamt'];

$outerARR = explode(" ", $a);
$arr = array();
foreach ($outerARR as $arrvalue){
    $innerarr = explode(",", $arrvalue);
    $arr[] = $innerarr;
}

print_r($arr);
}
3
Contributors
4
Replies
24
Views
3 Years
Discussion Span
Last Post by diafol
0

I feel like there is a piece missing here. What are you trying to accomplish by this?

What you are doing there is generating two completely separate arrays, each of them containing only a single of your installamt GROUP_CONCAT results. I'd have thought the desired result would be a single multidimensional array with each of the fields in separate elements. At least, that is my impression based on the little info we have.

Consider the explode on line #5. What is the point of that statement? Where is this space you are splitting the field on comming from? As far as I can see, from nowhere. Which means the foreach on line 7 is only ever going to go through one iteration, and the $arrvalue will be exactly equal to $a and $row1['installmant']. (Copying that into $a is, by the way, completely pointless.)

You could, in fact, refactor that code to look like this:

$sql = "
    SELECT 
        ac_no, cust_name, 
        GROUP_CONCAT(install_amt SEPARATOR ',') as installamt 
    FROM ankali_slabpay  
    WHERE agent_id=$agent_id 
    GROUP BY ac_no";
$select = mysql_query($sql);
if (!$select) {
    trigger_error("SQL Query failed: " . mysql_error(), E_USER_ERROR);
}

while($row1 = mysql_fetch_array($select)) {
    $arr = array();
    $arr[] = explode(",", $row1['installamt']);

    print_r($arr);
}

This should give you the same result as your code. However this makes it far more obvious how little point there is in the $arr array being defined like that. It would make far more sense to define the array outside th loop and add the exploded values into that array.

$arr = array();
while($row1 = mysql_fetch_array($select)) {
    $arr[] = explode(",", $row1['installamt']);
}
print_r($arr);

The result of this should then become:

Array ( 
    [0] => Array ( 
        [0] => 000000 
        [1] => 000400 
        [2] => 000450 
    ),
    [1] => Array ( 
        [0] => 000350 
        [1] => 000400 
    ) 
)

Which makes much more sense, at least from where I'm sitting.

Additionally note two things in my code:

  1. How I've re-formatted the SQL query. They need to be readable as well, just like any other part of the code. You shouldn't cram a whole bunch of code into a single line like that.

  2. How I verify the result of the mysql_query call. This you should always do. Always. There is aboslutely no excuse for not doing this, one way or another.

0

thanks Atli for quick reply, i've modified my code as yours, still getting same result there is no change.

Array ( [0] => Array ( [0] => 000000 [1] => 000400 [2] => 000450 ) ) Array ( this should be 1 [0] => Array ( [0] => 000350 [1] => 000400 ) ) 
1

I know there is no change. I said as much in my post. That example wasn't a solution, just a way to point out the problems in your own code.

Read what I wrote, don't just copy-paste the code examples.

Votes + Comments
agreed
0

@blue

You've posted a number of threads on the same-ish topic, but in none of them is there an explanation of what you're trying to accomplish (apologies if I'm wrong). When you ask specifics such as how do I create an array from a csv-type string, then you get the specific answer, but that possibly doesn't help much in the wider context of what you're trying to do.

Could you please outline what you need as a finished view/display and perhaps we can work backwards to your SQL (which seems OK).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.