Hakdo
Hakdo

Reputation: 121

PHP/SQL array access error

I'm trying to make a new column with results from an SQL query in PHP:

$someArray= array(array('match'=>'123'), array('match'=>'456'), array('match'=>'789')); //arbitrary number of elements
foreach($someArray as $key=>$item){
    $someArraysDouble[]=$item;
}
$someQuery="select count(*) as somecount from sometable";
$probe1=array();
$probe2="0";
$probe3="0";
$probe4="0";
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";//myDB uses MySQL
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
foreach($someArray as $key=>$item) {
    $someQuery.=" where somecolumn like "%$item['match']%";
    $blahblah=$conn->query($someQuery);
    if ($blahblah->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $row['match']=$item['match'];
            $probe1[]=$row;
        }
    }
    $conn->close();
}
foreach($someArraysDouble as $key1=>$item1) {
    foreach($probe1 as $key2=>$item2) {
        if($item2['match']==$item1['match']) {
            $probe2=$item1['somecount'];
            $probe3=$item2['somecount'];
            $item1['somecount']=$item2['somecount'];
            $probe4=$item1['somecount'];
        }
    }
}

The output HTML looks like this:

<html>
<head></head>
<body>
{$probe2}<br>{$probe3}<br>{$probe4}<br><br>
{loop $probe1 $key1 $item1}
{$item1['somecount']}<br><br>
{/loop}
<br><br>
{loop $someArraysDouble $key2 $item2}
{$item2['somecount']}<br><br>
{/loop}
</body>
</html>

Result is... something I don't understand:
- $probe2 is null, which is expected.
- $probe3 is the count value for last element, which is expected.
- $probe4 is the count value for last element, which is expected.
- The first loop with $probe1 produces the count value for each element, which is expected.
- The second loop with $someArraysDouble produces nothing, which is NOT expected. HOW can this happen?

For some reason that I'm not sharing in order to keep this question concise, I need to have the count value for each element outputted via $someArraysDouble.

Upvotes: 1

Views: 79

Answers (2)

Hakdo
Hakdo

Reputation: 121

Turns out it was me not understanding how foreach in PHP works.

function array_generate(){
return array(array('match'=>'123'), array('match'=>'456'), array('match'=>'789')); //arbitrary number of elements
}
$someArray=array_generate();
var_dump($someArray);
foreach($someArray as $heavy=>$load)
{
    $load['addedvalue']="newvalue";
    $test1[$heavy]="newvalue";
    $test2[$heavy]=$load['addedvalue'];
    var_dump($someArray);
}
var_dump($someArray);
var_dump($test2);

The n+2 var_dump($someArray)s (where n==count($someArray)) all show the same.

array(3) { [0]=> array(1) { ["match"]=> string(3) "123" } [1]=> array(1) { ["match"]=> string(3) "456" } [2]=> array(1) { ["match"]=> string(3) "789" } }

But var_dump($test2) shows:

array(3) { [0]=> string(8) "newvalue" [1]=> string(8) "newvalue" [2]=> string(8) "newvalue" }

This means the addedvalue of each element of $someArray does not persist once $key changes. So my solution is to use a new array, which is synced with the original array, length-wise.

Upvotes: 0

Peter Gluck
Peter Gluck

Reputation: 8236

I suspect that this line is not performing as you expect because you have not created $someArraysDouble before entering the loop:

$someArraysDouble[]=$item;

Try creating an empty array first, like this:

$someArraysDouble = array(); // <== Initialize the array first

$someArray = array(array('match'=>'123'), array('match'=>'456'), 
               array('match'=>'789')); //arbitrary number of elements   
foreach($someArray as $key=>$item){
  $someArraysDouble[] = $item;
}

See the PHP Array docs for more info, specifically the section "Creating/modifying with square bracket syntax".

Upvotes: 1

Related Questions