Multidimensional Arrays how to grab Values {SOLVED}


#1

[php]

<?php $type_ore = array( "Omber"=>array( $Trit[0] => 307, $Pyerite[1] => 123, $Isogen[2] => 307, $batch[3] => 500 ) ); ?>

[/php]

Ok with this array once Omber is selected I need to grab each value and echo them to my page how can i go about doing this. I will have many more options but just to keep it simple im testing with this. Plz help.


#2

Try the following line of code:

foreach($type_ore as $key1 => $value1)
{
     foreach($value1 as $key2 => $value2)
     {
          echo $value2;
     }
}

This might be wrong b/c I hardly every use maulti-demension arrays but in theory and in the books I’ve looked at, it was something like this.


#3

The array you created is not the correct format for a multidimensional array it should look like this:

$type_ore = array( 
     "Omber" => array(
          "trit" => 307,
          "pyerite" => 123,
          "isogen" => 307,
          "batch" => 500
     )
);

To output this array:

foreach( $type_ore as $ore ) {

     foreach( $ore as $type => $id ) {
    
          echo "$type is $id"; // Whatever you want to output
 
     }

}

#4

Thank you that really helped but I do have another question how do i go about just pulling one value out of the array. Thank you for all the help


#5

You would do the following:
[php]

<?php $var = $type_ore['Omber']['trit']; echo $var; // Or just do it strait from the array echo $type_ore['Omber']['trit']; // both will output 307 ?>

[/php]


#6

Ok i edited the code for what i needed and all the sudden now nothing is happening i have checked to see that everything looks just like your example but when i look at it nothing. I guess just need another pair of eyes. * I get a white screen. And then i use the foreach code to post to my page. It’s only when i add more code to this array that it stops working.

[php]

<?php $type_ore = array( "Arkonor" => array( "Megacyte" => 333, "Tritanium" => 300, "Zydrine" => 166, "batch" => 250 ) "Bistot" => array( "Megacyte" => 170, "Pyerite" => 170, "Zydrine" => 341, "batch" => 200 ) "Crokite" => array( "Nocxium" => 331, "Tritanium" => 331, "Zydrine" => 663, "batch" => 250 ) "Dark Ochre" => array( "Nocxium" => 500, "Tritanium" => 250, "Zydrine" => 250, "batch" => 400 ) "Omber" => array( "trit" => 307, "pyerite" => 123, "isogen" => 307, "batch" => 500 ) ); ?>

[/php]


#7

You need to add commas after each array element, unless it is the last one:
[php]

<?php $type_ore = array( "Arkonor" => array( "Megacyte" => 333, "Tritanium" => 300, "Zydrine" => 166, "batch" => 250 ), "Bistot" => array( "Megacyte" => 170, "Pyerite" => 170, "Zydrine" => 341, "batch" => 200 ), "Crokite" => array( "Nocxium" => 331, "Tritanium" => 331, "Zydrine" => 663, "batch" => 250 ), "Dark Ochre" => array( "Nocxium" => 500, "Tritanium" => 250, "Zydrine" => 250, "batch" => 400 ), "Omber" => array( "trit" => 307, "pyerite" => 123, "isogen" => 307, "batch" => 500 ) ); ?>

[/php]


#8

Hopefully last Question: This is what i need the program to do and having trouble making it do what i want. When a certain type of ore is selected from $type_ore I need to grab the value batch, but i need to make it a general variable to use in equations on a calculator that im building that way no matter what type of ore is selected it can grab the batch value. any suggestions. Also for the foreach statement I would like to know how to post like just three of the four variables if that is possible. Thanks


#9

For your first question, you will need a custom function like this:
[php]

<?php function get_batch( $ore, $list ) { $batch = isset( $list[$ore]['batch'] ) ? $list[$ore]['batch'] : false; return $batch; } ?>

[/php]

And you could get the batch number like this:
[php]

<?php $batch = get_batch( "Omber", $type_ore ); ?>

[/php]

For the second question you would do something like this:
[php]

<?php foreach( $type_ore as $name => $ore ) { foreach( $ore as $type => $value ) { if( $type == "batch" ) continue; echo "$type for $name is $value
"; } } ?>

[/php]


#10

Thank you that post the minerals i want but it also posts the minerals for every ore type. When the person makes a selection of Omber or Arkonor or something I would like it to just post the minerals for that type. So you can see what im doing to maybe understand it you can go to http://www.ascensionscorp.com/refining.php and here is the code to the page.
And again the batch is not being grabbed probably my being stupid im still trying to learn php i really like it so plz forgive me. I have notes in the code to explain my problem areas.

[php]

Ore/Refining Yield Calculator <?php function get_batch( $ore, $list ) { $batch = isset( $list[$ore]['batch'] ) ? $list[$ore]['batch'] : false; return $batch; } $batch = get_batch( "Arkonor", $type_ore ); //I would like to grab batch for every kind of ore type that would be selected $type_ore = array( "Arkonor" => array( "Megacyte" => 333, "Tritanium" => 300, "Zydrine" => 166, "batch" => 250 ), "Bistot" => array( "Megacyte" => 170, "Pyerite" => 170, "Zydrine" => 341, "batch" => 200 ) ); $ore_qual = array('+0%' => 0, '+5%' => 0.05, '+10%' => 0.10); $s_equip = array('75%' => 0.75, '50%' => 0.5, '40%' => 0.4, '35%' => 0.35, '32%' => 0.32, '30%' => 0.30); $refine_sk = array('Level 0' => 0, 'Level 1' => 1, 'Level 2' => 2, 'Level 3' => 3, 'Level 4'=> 4, 'Level 5' => 5); $ref_eff_sk = array('Level 0'=> 0, 'Level 1' => 1, 'Level 2' => 2, 'Level 3' => 3, 'Level 4'=> 4, 'Level 5' => 5); $ore_pro_sk = array('Level 0'=> 0, 'Level 1' => 1, 'Level 2' => 2, 'Level 3' => 3, 'Level 4'=> 4, 'Level 5' => 5); //Here is where the batch is calculated and then posted in a table to display the values $quant = !empty($_POST['quant']) ? $_POST['quant'] : ''; $refine = floor($quant / $batch); $unref = $quant - ( $refine * $batch ); $total = number_format((($_POST['s_equip'] + 0.375 * ( 1 + $_POST['refine_sk'] * 0.02 ) * ( 1 + $_POST['ref_eff_sk'] * 0.04 ) * ( 1 + $_POST['ore_pro_sk'] * 0.05)) * 100 ), 2, '.', ''); ?>

Ore/Refining Yield Calculator

Ore Type: <?php // Also right here for some reason it has stopped keeping the value when you calculate the page was wondering if you could explain why and how to fix foreach($type_ore as $key => $value){ echo ''.$key.''; } ?> Station Equipment: <?php foreach($s_equip as $key => $value){ echo ''.$key.''; } ?> Mineral Info: Perfect
<?php //Here is where it post all the minerals for every ore type, only want to post minerals for the selected ore. foreach( $type_ore as $name => $ore ) {
foreach( $ore as $type => $value ) { 
     
    if( $type == "batch" )    continue; 
     
    echo "$type for $name is $value <br />"; 
 
} 

}
?>

Ore Quality: <?php foreach($ore_qual as $key => $value){ echo ''.$key.''; } ?> Refining Skill: <?php foreach($refine_sk as $key => $value){ echo ''.$key.''; } ?>
Quantity: Refinery Efficiency Skill: <?php foreach($ref_eff_sk as $key => $value){ echo ''.$key.''; } ?>
Units: m3: Ore Processing Skill: <?php foreach($ore_pro_sk as $key => $value){ echo ''.$key.''; } ?>
ISK/unit: Total Yield: <?php if ($total>=100) echo "100.00"; else echo $total; ?>%
Ore Information
Batch Size: <?php echo $batch; ?>
Unrefined: <?php echo $unref; ?>(Units)
Refines: <?php echo $refine; ?>
[/php]