mysql - PHP/SQL array access error -
i'm trying make new column results sql query in php:
$somearray= array(array('match'=>'123'), array('match'=>'456'), array('match'=>'789')); //arbitrary number of elements foreach($somearray $key=>$item){ $somearraysdouble[]=$item; } $somequery="select count(*) somecount 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 $key=>$item) { $somequery.=" somecolumn "%$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 $key1=>$item1) { foreach($probe1 $key2=>$item2) { if($item2['match']==$item1['match']) { $probe2=$item1['somecount']; $probe3=$item2['somecount']; $item1['somecount']=$item2['somecount']; $probe4=$item1['somecount']; } } }
the output html looks 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... don't understand:
- $probe2
null, expected.
- $probe3
count value last element, expected.
- $probe4
count value last element, expected.
- first loop
$probe1
produces count value each element, expected.
- second loop
$somearraysdouble
produces nothing, not expected. how can happen?
for reason i'm not sharing in order keep question concise, need have count value each element outputted via $somearraysdouble
.
i suspect line not performing expect because have not created $somearraysdouble
before entering loop:
$somearraysdouble[]=$item;
try creating empty array first, this:
$somearraysdouble = array(); // <== initialize array first $somearray = array(array('match'=>'123'), array('match'=>'456'), array('match'=>'789')); //arbitrary number of elements foreach($somearray $key=>$item){ $somearraysdouble[] = $item; }
see php array docs more info, section "creating/modifying square bracket syntax".
Comments
Post a Comment