0

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.

10
  • 2
    What you've written looks like it should work. Are you sure $cName is set correctly? Commented Feb 17, 2016 at 23:15
  • you're using mysqli? show the ->bind_result() part, where most likely $cName is set Commented Feb 17, 2016 at 23:16
  • Please post the actual code, especially the part that sets $cName. Commented Feb 17, 2016 at 23:16
  • Hi @Barmar. Yes, when I echo $cName; and echo gettype($cName); I correctly get the category as well as the type "string". Commented Feb 17, 2016 at 23:17
  • @Barmar, I've added the MySQLi code. Commented Feb 17, 2016 at 23:24

2 Answers 2

0

Why not make it simpler and use something like this instead:

$result = $mysqli->query("SELECT...");
if ($result->num_rows) {
    while($row = $result->fetch_assoc()){
        $arr[$row['id']][] = $row['name'];
    }
}

You will get lost very easily in all these numerous variables with undecipherable names that you make in the bind_result().

6
  • I appreciate the style tips. However, I've solved my issue. See below. Commented Feb 17, 2016 at 23:44
  • No worries. Just trying to save you some potential headache in the long term. Commented Feb 17, 2016 at 23:45
  • He's using mysqli, not PDO. Commented Feb 17, 2016 at 23:56
  • My bad. In this case, the fetch() behaviour should obviously be modified to return an associative array. Commented Feb 17, 2016 at 23:58
  • $stmt->fetch() returns a boolean, not an associative array. Commented Feb 17, 2016 at 23:59
0

I believe I've solved this. When I use var_dump($arr);, it correctly prints as an associative array. When I used print_r($arr);, it prints as an indexed array. I am not sure why this is.

EDIT: Actually, the problem was that I sorted the array using sort($arr) before printing it.

6
  • I've never seen anything like this. Commented Feb 17, 2016 at 23:55
  • Did you look at my demo on ideone.com? It uses print_r and it prints correctly as an associative array. Commented Feb 17, 2016 at 23:56
  • This would happen if you did something like print_r(array_values($arr)). But I don't believe that just switching from print_r() to var_dump(), without changing anything else, would solve it. Commented Feb 18, 2016 at 0:03
  • Answer revised. I forgot that I sorted the array before printing it, which caused my problem. Commented Feb 18, 2016 at 0:46
  • But you didn't sort the array before printing it with var_dump? Commented Feb 18, 2016 at 0:46

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.