1

I am optimizing some code. Some code I can change, some I can't. I am querying the database and checking it against a json_decode() array that is the new data. I find the differences and add or delete from the database as needed.

UPDATE: This is my query:

$sQuery = "SELECT column1, column2 FROM my_table WHERE id=my_id;

UPDATE2:

OK, so my query goes into a custom query function that I cannot change and this is what comes out:

    mysqli_result Object
    (
        [current_field] => 0
        [field_count] => 2
        [lengths] => 
        [num_rows] => 2
        [type] => 0
    )

I want to parse the result in such a way that is organized like this:

Array 
(
    [0] => Array
           (
               [column1] => value
           )
    [1] => Array
           (
               [column2] => value
           )
)

// end Update 2

I'm banging my head feeling stupid trying to parse the first array to match the organization of the second array. Help please?

Here is what I've tried:

while ($aRows = $aResults->fetch_assoc()) {
    foreach ($aRows as $iKey => $sValue) {

        if ($iKey % 2 == 0)
            $aPreviousList[$iKey][] = $sValue;

        if ($iKey % 2 == 1)
            $aPreviousList[$iKey][] = $sValue;
    }
}

But it comes out like this:

Array
(
    [ixfChangeMethod] => Array
        (
            [0] => 16
            [1] => 11
        )

    [sDetails] => Array
        (
            [0] => some details about 16
            [1] => some details about 11
        )

)

Am I going about this all wrong or is there another way I can parse both arrays that I can check the differences better?

9
  • fetch_assoc() should return a row where each member of the array is the name of the column... what does your query look like? Commented Jan 23, 2015 at 20:53
  • If you know you'll always have an even number of rows returned, you can just get a second row inside your loop; the first row will contain your key and the second your value. Commented Jan 23, 2015 at 20:54
  • I will have odd or even rows returned Commented Jan 23, 2015 at 20:55
  • @brazilianldsjaguar i'll add my query above Commented Jan 23, 2015 at 20:55
  • How is that possible? Then you'll end up with a key without a value at the end. Commented Jan 23, 2015 at 20:58

3 Answers 3

6

I've changed the code, is this better?

while ($aRow = $aResults->fetch_row()) {
    $aPreviousList[] = array($aRow[0] => $aRow[1]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I double checked to make sure I changed all the variables to the correct variables in my code, but this returned an empty array. :-/
This just makes an array with an array containing only the 'details' column. Array ( [0] => Array ( [16] => some details ) [1] => Array ( [11] => asdfasdfasdasd ) )
Ok, i've rolled back my last change
1

fetch_assoc returns an associative array.
fetch_row returns a numeric array.

So just use fetch_row instead of fetch_assoc.

Comments

1

You can try this

while ($aRows = $aResults->fetch_assoc()) 
{
    $aPreviousList[]=array($aRows['column1']=>$aRows['column2']);
}

print_r($aPreviousList);

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.