I need an associative array with this structure (keys are strings, values are indexed arrays), and I'm appending elements dynamically.
Array(
["itemCategory"]=> Array("itemName", "itemName"...)
["itemCategory"]=> Array("itemName", "itemName"...)
...
)
However, I cannot seem to form an array within an associative array when I do this (as requested, I've added the MySQLi code):
if (!($stmt = $mysqli->prepare(
"SELECT b.id, b.type, b.name, b.street, b.city, b.state, b.zipcode, b.phone, b.website, b.hours, b.latitude, b.longitude, c.id, c.name, i.id, i.name
FROM business b LEFT JOIN business_category_item bci ON bci.bid = b.id
LEFT JOIN category c ON c.id=bci.cid LEFT JOIN item i ON i.id = bci.iid"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
if(!$stmt->bind_result($bID,$bT,$bN,$bStr,$bC,$bSta,$bZ,$bP,$bW,$bH,$bLat,$bLng,$cI,$cN,$iI,$iN)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqlii->connect_error;
}
$arr=array();
while($row=$stmt->fetch()){
$arr[$cN][]=$iN; //gettype($cN) returns "string"
}
I have also tried array_push() without luck (undefined index error):
array_push($arr[$cN],$iN);
When I print my array, it always prints like this:
print_r($arr, true);
Array
(
[0] => Array
(
[0] => "MP3 player"
[1] => "Computers"
)
[1] => Array
(
[0] => "Tank tops"
[1] => "Blankets"
[2] => "Shirts"
)
)
instead of what I need, which is:
Array
(
["electronics"] => Array
(
[0] => "MP3 player"
[1] => "Computers"
)
["clothing"] => Array
(
[0] => "Tank tops"
[1] => "Blankets"
[2] => "Shirts"
)
)
Any help would be appreciated.
$cName
is set correctly?mysqli
? show the->bind_result()
part, where most likely$cName
is set$cName
.echo $cName;
andecho gettype($cName);
I correctly get the category as well as the type "string".