Creating an associative array

Im trying to figure out how to create an associative array andadd items to it.
I have

			$sql = "SELECT title,trip_rating FROM circuit_breakers WHERE pdu_id = ".$pdu_id;
			
			$circuit_breakers = array();
			
			$cb_result = mysqli_query($conn, $sql);
			
				if (mysqli_num_rows($cb_result) > 0) {
					
					while($breaker = mysqli_fetch_assoc($cb_result)) {
						
						array_push($circuit_breakers,$breaker['title']);
			
					}
				}
				
				echo '<pre>';print_r($circuit_breakers);echo '</pre>';

which results in

Array
(
    [0] => Happy Boi
    [1] => Snow White
    [2] => Idiot
    [3] => Redrum
)

But am stuck on how to create an associate array instead (so the key would be the title, and the value would be the trip_rating), so the result would be

Array
(
    [Happy Boi] => 44
    [Snow White] => 77
    [Idiot] => 55
    [Redrum] => 33
)

dang, where did the solution go?

ok, tried to use that

			$sql = "SELECT title,trip_rating FROM circuit_breakers WHERE pdu_id = ".$pdu_id;
			
			$circuit_breakers = array();
			
			$cb_result = mysqli_query($conn, $sql);
		
			$circuit_breaker = mysqli_fetch_assoc($cb_result);

				foreach ($circuit_breaker as $key => $value) {
					$circuit_breakers[$key] = $value;
				}

				echo '<pre>';print_r($circuit_breakers);echo '</pre>';

I tried to follow exactly but am getting only first result

Array
(
    [title] => Happy Boi
    [trip_rating] => 30
)

@lurtnowski,

Your missing the iterations through the while loop.

if (mysqli_num_rows($cb_result) > 0) {

Edit:

Thanks to @benanamen,
Above is incorrect and should have been:

while ($cb_result = mysqli_query($conn, $sql) { 
// build array 
}

Tapped on a tablet and not tested

ok, tried a few things and am getting strange results.

if (mysqli_num_rows($cb_result) > 0) {
					
	//while($circuit_breaker = mysqli_fetch_assoc($cb_result)) {
						
	foreach ($circuit_breaker as $key => $value) {
		$circuit_breakers[$key] = $value;
	}
	
      //}
}

Results in

Array
(
    [title] => Happy Boi
    [trip_rating] => 30
)

The first record
If I remove the comments

Array
(
    [title] => Redrum
    [trip_rating] => 55
)

It seems like the second way is looping, but also overwriting, shouldn’t it just be adding to the array
The last record

Is someone deleting replies from benanamen?

Thats not it @John_Betong.

Here is a tested PDO version

<?php declare (strict_types = 1);

$host = '127.0.0.1';
$db = 'dct';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $options);

$data = $pdo->query("SELECT title,trip_rating FROM circuit_breakers WHERE pdu_id = 1")->fetchAll();

foreach ($data as $value) {
    $arr[$value['title']] = $value['trip_rating'];
}

print_r($arr);
1 Like

Tested Mysqli Version

<?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'dct';

$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$sql = 'SELECT title,trip_rating FROM circuit_breakers WHERE pdu_id = 1';
$result = mysqli_query($conn, $sql);

    foreach ($result as $value) {
        $arr[$value['title']] = $value['trip_rating'];
    }

print_r($arr);
2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.