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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
checkmate3001
Forum Newbie
Posts: 4
Joined: Sun Oct 17, 2010 9:53 pm

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

Post 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!
Last edited by checkmate3001 on Sat Dec 11, 2010 4:43 pm, edited 2 times in total.
jankidudel
Forum Commoner
Posts: 91
Joined: Sat Oct 16, 2010 4:30 pm
Location: Lithuania, Vilnius

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

Post by jankidudel »

Just use multidimensional arrays like this

Code: Select all

foreach($array[$invoice] as $key => $value) {
	$key = $array[$invoice][$serviceOrder];
}
Last edited by Benjamin on Sat Dec 11, 2010 2:48 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
checkmate3001
Forum Newbie
Posts: 4
Joined: Sun Oct 17, 2010 9:53 pm

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

Post 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.
checkmate3001
Forum Newbie
Posts: 4
Joined: Sun Oct 17, 2010 9:53 pm

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

Post 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. ;)
Post Reply