3

I have an array: $aPerfparse as 2-dimensional array where index ranges from 0 to n-1,

* aPerfparse[index]['label']         -   label of the perfdata  
*                  ['value']         -   actual perfdata  
*                  ['uom']           -   unit of measurement (might be NULL)

Need to iterate through each item and set each indexes 'value' and 'label' to a sep. variable based-on the index.

Without a loop, it would be:

$value0 = $aPerfdata[0]['value'];  
$value1 = $aPerfdata[1]['value'];

What is correct/incorrect about this?:

foreach ( $aPerfdata as $key => $value ) {  
    $value$key = $aPerfdata[$key]['value'];  
    $label$key = $aPerfdata[$key]['label'];   
}

Similarly, I need to take those stored $value and $label variables and reference them later in a foreach loop.

Without a loop, it would look like:

ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+2, $oShadow, $fontFile, $label0 . ":" . " " . $value0);  
ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+40, $oShadow, $fontFile, $label1 . ":" . " " . $value1);

What is correct/incorrect about this?:

foreach ( $aPerfdata as $key => $value ) {  
    ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+$sz, $oShadow, $fontFile, $label$key . ":" . " " . $value$key);  
    sz=$sz+40;  
} 

Thank you!

====

After everyone's help, I have the following working:

foreach ( $aPerfdata as $key => $value ) 
{
    ${'label'.$key} = $aPerfdata[$key]['label'];  
    ${'value'.$key} = $aPerfdata[$key]['value'];  
}

foreach ( $aPerfdata as $key => $value )
{
    ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+$sz, $oShadow, $fontFile, ${'label'.$key} . ":" . " " . ${'value'.$key});
    $sz=$sz+40;
}

I don't really have a need to flatten the array anymore. I tried the method mentioned by Mark, but the ImageTTFText function doesn't execute.

2
  • $label$key => ${'label'.$key} Commented Sep 21, 2010 at 1:52
  • What do you mean it doesn't execute? Do you get an error? A warning? Can you show us more of your code? The section shown is correct. Commented Sep 21, 2010 at 12:36

4 Answers 4

1

Firstly:

$label$key

is wrong. Should be:

$label.$key

You need to concatenate the variables with a dot(.).

Didn't understand the second part of the question. Can you just paste a var_dump of the array so we can get a clearer understanding of the structure?

Sign up to request clarification or add additional context in comments.

1 Comment

I "clearly don't understand the question" as well. Maybe it's your question, though.
1

It looks to me like you might be confusing foreach with a simple for loop.

If you have an array that looks like this:

Array
(
    [0] => Array
        (
            [label] => red
            [value] => 8
            [uom] => cm
        )

    [1] => Array
        (
            [label] => green
            [value] => 6
            [uom] => m
        )

    [2] => Array
        (
            [label] => blue
            [value] => 34
            [uom] => m
        )

)

You can access iterate over the keys/values as follows.


foreach($arr as $array_key => $array_value)
{
        // obviously you don't need to assign these. it's just for demonstration
        $key = $array_key;
        $label = $array_value['label'];
        $value = $array_value['value'];
        $uom = $array_value['uom'];
}

Hope that helps.

Comments

0

Why not just do

foreach ( $aPerfdata as $value ) {  
    ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+$sz, $oShadow, $fontFile, $value['label'] . ":" . " " . $value['value']);  
    $sz=$sz+40;  
} 

Among other things I think your use of variable variables is incorrect. You should really have something like:

${'value'.$key}

But really you should just be using an array rather than variable variables - and since you already have an array there's no real need to flatten it (as I show you above).

2 Comments

I like this, didn't do it initially because I needed each var for a height/width calculation, but I am circumventing that now. Unfortunately, this doesn't generate the proper images that the function (ImageTTFText) is supposed too. Not sure why.
@user then you need to provide a little more detail on what it means not to work
0

You have a two dimensional array. The foreach statements iterates through the first dimension for you. This should work:

foreach ( $aPerfdata as $item ) {  
    $value = $item['value'];  
    $label = $item['label'];   
}

Also, I don't think you should include $ in your variable name, other than of course in front of the name.

3 Comments

You clearly don't understand the question or variable variables, suggest you reread or conduct further research.
@Mark; Yes, I mistunderstood the question as I read it fast. You got me there. But it isn't your place to tell me what I need to research; what makes you think you can insult my intelligence like that?
I didn't insult your intelligence, I merely suggested you conduct one of two activities: reading more closely (seems perfectly reasonable) or researching your answer further -- the latter option of which could be trivial, and is just that, an option.

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.