1

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.

2
  • The code-colouring shows an error with $someQuery.=" where somecolumn like "%$item['match']%"; The number/type of brackets do not match. try: $someQuery.=" where somecolumn like '%$item['match']%' "; Commented Aug 16, 2015 at 17:58
  • Where is $someArray generated..? Commented Aug 16, 2015 at 18:18

2 Answers 2

1

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".

Sign up to request clarification or add additional context in comments.

3 Comments

After several months away, I managed to solve the problem myself. Thanks anyway.
You should post your answer. Someone else may encounter this issue.
There. Posted my answer.
0

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.

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.