Jump to content

looping through multidimensional array -- help


Go to solution Solved by stevieontario,

Recommended Posts

Hello Freaks,

 

Much as I love puzzles, this one has me stumped. I'm trying to loop through this array:

            [Machines] => Array
                (
                    [Machine] => Array
                        (
                            [0] => Array
                                (
                                    [MachineName] => Machine1
                                    [Type] => STAMPER
                                    [Outputs] => Array
                                        (
                                            [Output] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [Hour] => 1
                                                            [Widgets] => 55
                                                        )

                                                    [1] => Array
                                                        (
                                                            [Hour] => 2
                                                            [Widgets] => 54
                                                        )

                                                )

                                        )

                                    [Capabilities] => Array
                                        (
                                            [Capability] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [Hour] => 1
                                                            [Widgets] => 100
                                                        )

                                                    [1] => Array
                                                        (
                                                            [Hour] => 2
                                                            [Widgets] => 100
                                                        )

                                                )

                                        )

I want to echo Machine1's output and capability for each of the two hours shown, each hour on the same line, like so:

MachineName	Type		Hour	Output	Capability
Machine1	STAMPER		1	55	100
Machine1	STAMPER		2	54	100

I have written a php script to do this, but so far I can't figure out how to grab both the outputs AND capabilities and put them on the same line. Here's what I've written:

<?php
		foreach ($machine as $k=>$v){

			if ($k == "MachineName"){	$unit = $v;}
			if ($k == "Type"){ $type = $v; }
		//begin grabbing outputs and capabilities//
		{
			if ($k == "Outputs"){
				foreach($v as $outputs)	{
					foreach($outputs as $k=>$v)	{
						foreach ($v as $kk=>$vv){
											if($kk=="Hour"){$hourout = $vv;}
								{if (array_key_exists("Widgets", $v)){$widgets = $vv;} else {$widgets = 0;}}
					}
					echo "<pre>";
					echo "$unit	$hourout	$electricity";
					echo "</pre>";				
					}}}//end of outputs loop//
					}}
?>

This script can get the outputs, no problem. But going back in and getting the matching capabilities for that hour? I'm just stumped. Am I trying to do something impossible?

Try

<table>
    <tr>
        <th>Machine Name</th>
        <th>Type</th>
        <th>Hour</th>
        <th>Output</th>
        <th>Capability</th>
    </tr>
<?php
foreach($machinesArray['Machines'] as $machines)
{
    foreach($machines as $machine)
    {
        $name  = $machine['MachineName'];
        $type  = $machine['Type'];

        // Loop over the Outputs, grab the array index for each output array.
        // Use the index later for referecing the Capabilities array
        //
        foreach($machine['Outputs']['Output'] as $index => $output)
        {
            $hour = $output['Hour'];
            $outputVal = $output['Widgets'];

            // get the corresponding capability for current output
            $capability = $machine['Capabilities']['Capability'][$index]['Widgets'];

            echo <<<ROW
    <tr>
        <td>$name</td>
        <td>$type</td>
        <td>$hour</td>
        <td>$outputVal</td>
        <td>$capability</td>
    </tr>
ROW;
        }


    }
}
?>
    </tr>
</table>

You should be able to use your existing variables to access the "Capabilities" part of the array. I haven't spent too much time trying to figure out the array or the loop that processes it, but you could try using the following in the echo statement:

$machine['Capabilities']['Capability'][$k];

Also note that you can get a better idea of how to work with the array, by using the following line:  

echo '<pre>' . print_r($arrayGoesHere, true) . '</pre>';
 
 
You just need to plug in the array you want to see. You could add the following lines of code somewhere in your loop after the $machine variable is created:
echo '<pre>' . print_r($machine['Capabilities'], true) . '</pre>';
echo '<pre>' . print_r($machine['Capabilities']['Capability'], true) . '</pre>';

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.