Jump to content

Moving multidimensional array values


kiwis-are-cool

Recommended Posts

I'm trying print out a multidimensional array in this manner:

Image showing how i'm trying to display it

What i'm having a problem with is finding the duplicate STEAM IDs, removing the duplicate STEAM ID and then moving the data of the removed STEAM ID to the master STEAM ID. Sorry if that's a little confusing. Any ideas? 

Here is the foreach loop that i'm using

echo '"' . $r['steamid'] . '"' . "<br />";
            echo "{" . "<br />";
                echo '      "' . $r['wepid'] . '"' . '   //' . $r['wepname'] . "<br />";
                echo '{' . "<br />";
                echo '"level"' . '      "' . $r['weplvl'] . '"' . "<br />";
                echo '"quality"' . '      "' . $r['weprare'] . '"' . "<br />";

            if($r['attr1'] === 0 || $r['attr1'] === ''){
                echo '';
            }else{
                echo '"1"' . '      "' . $r['attr1'] . ' ; ' . $r['val1'] . '"' . "<br />";
            }

            if($r['attr2'] === 0 || $r['attr2'] === ''){
                echo '';
            }else{
                echo '"2"' . '      "' . $r['attr2'] . ' ; ' . $r['val2'] . '"' . "<br />";
            }

            if($r['attr3'] === 0 || $r['attr3'] === ''){
                echo '';
            }else{
                echo '"3"' . '      "' . $r['attr3'] . ' ; ' . $r['val4'] . '"' . "<br />";
            }

            if($r['attr4'] === 0 || $r['attr4'] === ''){
                echo '';
            }else{
                echo '"4"' . '      "' . $r['attr4'] . ' ; ' . $r['val4'] . '"' . "<br />";
            }

            if($r['attr5'] === 0 || $r['attr5'] === ''){
                echo '';
            }else{
                echo '"5"' . '      "' . $r['attr5'] . ' ; ' . $r['val5'] . '"' . "<br />";
            }

            if($r['attr6'] === 0 || $r['attr6'] === ''){
                echo '';
            }else{
                echo '"6"' . '      "' . $r['attr6'] . ' ; ' . $r['val6'] . '"' . "<br />";
            }

            if($r['attr7'] === 0 || $r['attr7'] === ''){
                echo '';
            }else{
                echo '"7"' . '      "' . $r['attr7'] . ' ; ' . $r['val7'] . '"' . "<br />";
            }

            if($r['attr8'] === 0 || $r['attr8'] === ''){
                echo '';
            }else{
                echo '"8"' . '      "' . $r['attr8'] . ' ; ' . $r['val8'] . '"' . "<br />";
            }

            if($r['attr9'] === 0 || $r['attr9'] === ''){
                echo '';
            }else{
                echo '"9"' . '      "' . $r['attr9'] . ' ; ' . $r['val9'] . '"' . "<br />";
            }

            if($r['attr10'] === 0 || $r['attr10'] === ''){
                echo '';
            }else{
                echo '"10"' . '      "' . $r['attr10'] . ' ; ' . $r['val10'] . '"' . "<br />";
            }

            if($r['attr11'] === 0 || $r['attr11'] === ''){
                echo '';
            }else{
                echo '"11"' . '      "' . $r['attr11'] . ' ; ' . $r['val11'] . '"' . "<br />";
            }

            if($r['attr12'] === 0 || $r['attr12'] === ''){
                echo '';
            }else{
                echo '"12"' . '      "' . $r['attr12'] . ' ; ' . $r['val12'] . '"' . "<br />";
            }

            if($r['attr13'] === 0 || $r['attr13'] === ''){
                echo '';
            }else{
                echo '"13"' . '      "' . $r['attr13'] . ' ; ' . $r['val13'] . '"' . "<br />";
            }

            if($r['attr14'] === 0 || $r['attr14'] === ''){
                echo '';
            }else{
                echo '"14"' . '      "' . $r['attr14'] . ' ; ' . $r['val14'] . '"' . "<br />";
            }

            if($r['attr15'] === 0 || $r['attr15'] === ''){
                echo '';
            }else{
                echo '"15"' . '      "' . $r['attr15'] . ' ; ' . $r['val15'] . '"' . "<br />";
            }

            if($r['attr16'] === 0 || $r['attr16'] === ''){
                echo '';
            }else{
                echo '"16"' . '      "' . $r['attr16'] . ' ; ' . $r['val16'] . '"' . "<br />";
            }



                echo "<br />";
            echo '}' . "<br />";
            echo '}' . "<br />";
            echo "<br />";

Here is my query

 if ($stmt = $mysqli->prepare($query)) {
       $stmt->execute();
        $stmt->bind_result($id, $steamid, $wepid, $weplvl, $weprare, $attr1, $attr2, $attr3, $attr4, $attr5, $attr6, $attr7, $attr8, $attr9, $attr10, $attr11, $attr12, $attr13, $attr14, $attr15, $attr16, $val1, $val2, $val3, $val4, $val5, $val6, $val7, $val8, $val9, $val10, $val11, $val12, $val13, $val14, $val15, $val16);

            while ($row = $stmt->fetch()) {



             $data[] = array(
                'steamid' => $steamid,
                'wepid' => $wepid,
                'weplvl' => $weplvl,
                'wepname' => $wep,
                'weprare' => $weprare,
                'attr1' => $attr1, 
                'attr2' => $attr2, 
                'attr3' => $attr3, 
                'attr4' => $attr4, 
                'attr5' => $attr5, 
                'attr6' => $attr6, 
                'attr7' => $attr7, 
                'attr8' => $attr8, 
                'attr9' => $attr9, 
                'attr10' => $attr10, 
                'attr11' => $attr11, 
                'attr12' => $attr12, 
                'attr13' => $attr13, 
                'attr14' => $attr14, 
                'attr15' => $attr15, 
                'attr16' => $attr16, 
                'val1' => $val1,
                'val2' => $val2,
                'val3' => $val3,
                'val4' => $val4,
                'val5' => $val5,
                'val6' => $val6,
                'val7' => $val7,
                'val8' => $val8,
                'val9' => $val9,
                'val10' => $val10,
                'val11' => $val11,
                'val12' => $val12,
                'val13' => $val13,
                'val14' => $val14,
                'val15' => $val15,
                'val16' => $val16,
             );

              array_unique($data);




             }                          
          $stmt->close();
    }
Link to comment
https://forums.phpfreaks.com/topic/281113-moving-multidimensional-array-values/
Share on other sites

you would ORDER BY steamid in your query (if its not already) so that rows having the same streamid will be adjacent in the result set. then as you loop through the results to display them, you would remember (store in a php variable) the $streamid value and detect when it changes. each time it changes, you would output the new stream id heading, then output the data under that heading.


BTW - you need to normalize your database tables to avoid writing a wall of code to display the result. i can replace all that if/else logic with one line of code by storing the attribute/value pairs in their own table, related back to the id in the current table. you would only store the actual data -

 

id, ref_id, attr, value

1   x       23    42       (where x is the id these attributes corresponds to)

2      x               97        9

 


you can also form the strings you are echoing as one piece (will eliminate a lot of typing and typo errors.)

 

for example, this - 

echo ' "' . $r['wepid'] . '"' . ' //' . $r['wepname'] . "<br />";

can be just -

echo " \"{$r['wepid']}\" //{$r['wepname']}<br />";

I have gotten a bit farther but i'm still having trouble displaying the Steam ids in the correct place. 

 

New query i'm using

$query = "SELECT * FROM weapons ORDER BY steamid";

foreach loop

 $check = null;

      foreach($data as $key => $r){
      //	echo $check. "<br>";
      	if($check)
      	{
      	echo $check . "<br>";
        echo " \"{$r['wepid']}\" //{$r['wepname']}<br />";
        	echo "{" . "<br>";
	        	echo '"level"' . '      "' . $r['weplvl'] . '"' . "<br />";
	        	echo '"quality"'  . $r['weprare'] . '"' . "<br />";
			echo "}" . "<br>";	

			echo "<br>";	
      	}
      	
      	$check = $r['steamid'];
      
      }

Output

"custom_weapons_v3"

{

STEAM_0:0:1621342 (This is the wrong steamid it should me "STEAM_0:0:16213424" the information below it is correct though, it corresponds with the  duplicate steam ids.)
"46" //Bonk! Atomic Punch
{
"level" "2"
"quality"3"
}

STEAM_0:0:16213424 
"155" //Souther Hospitality
{
"level" "1"
"quality"0"
}

}

End goal

"custom_weapons_v3"

{

STEAM_0:0:16213424 
{
 "46" //Bonk! Atomic Punch
 {
  "level" "2"
  "quality"3"
 }


 "155" //Souther Hospitality
 {
  "level" "1"
  "quality"0"
 }
}

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.