Page 1 of 1

How can pull $key => $value and make $key a variable name?

Posted: Sat Dec 11, 2010 2:12 pm
by checkmate3001
Hello Everyone,
This is my first post. I've been doing php for only a few months now.
I work for a company that repairs cameras and lenses. We get in lenses under an invoice and each invoice has multiple service orders. We can get more than one invoice at a time. Each service order can have 1 or more lenses in it. Here is a visual example:
[text]
INVOICE: XXXXXXX
SERVICE ORDER MODEL SERIAL REPAIR TYPE COMMENT
329327 XXXXXX 932932 STANDARD DO IT REALLY FAST THE CUSTOMER IS A DICK
902308 XXXXXX 932093 MAJOR NONE
[/text]
So I've made a confusing array:
[text]
INVOICE => SERVICEORDER => 'model' => xxxxx
'serial' => xxxxx
'rep_type' => xxxxx
[/text]
The INVOICE is the $key and SERVICEORDER is the $value AND a $key and 'model', 'serial', 'repairtype' are all $keys and they each have their $values.

etc... I hope this is all making sense so far...

I've made a couple foreach loops.

Code: Select all

foreach ($invoice as $inv => $serviceOrder){
    foreach ($serviceOrder as $so_num => $data){
        foreach ($data as $key => $value){
        // Want I want here is some way to make each $key a variable name with its appropriate value, ie:
        // $model = $value;
        // $serial = $value;
        // $repair_type = $value;
        }         
    }
}
Is there a way to do this? Is there a 'good' way or a 'bad' way?
Am I going about this wrong? I've tried using an object... but I got so confused... also I can't figure out how to foreach an object so I can separate each invoice and it's service orders...

Any help would be greatly appreciated.

Thank you for your time!

Re: How can pull $key => $value and make $key a variable nam

Posted: Sat Dec 11, 2010 2:45 pm
by jankidudel
Just use multidimensional arrays like this

Code: Select all

foreach($array[$invoice] as $key => $value) {
	$key = $array[$invoice][$serviceOrder];
}

Re: How can pull $key => $value and make $key a variable nam

Posted: Sat Dec 11, 2010 2:57 pm
by checkmate3001
Thank you for your help... but I just found a function that might help me.
extract()
http://us.php.net/manual/en/function.extract.php

It took a while to find this. I will post later the code I end up with in case it may help someone.

Re: How can pull $key => $value and make $key a variable nam

Posted: Sat Dec 11, 2010 4:28 pm
by checkmate3001
I found an error in my logic. I was using the service order as an index in an array, however, in some cases that service order can have multiple pieces of equipment... therefore it was constantly getting written over in these cases...

I fixed that and used extract():

My array looks like this:
Invoice => Equipment Number => Field => Data
or as my code uses it:
$invoice[$inv][$equip]['field_name'][data]

Code: Select all

// Iterate through each invoice
foreach ($invoice as $inv => $equip){
    // Iterate through each piece of equipment
    foreach ($equip as $key => $field){
        extract($field, EXTR_OVERWRITE);
    }
}
My brain grew a new fold somewhere. ;)