note 35187 added to function.array-combine

From: Date: Fri, 22 Aug 2003 14:44:57 +0000
Subject: note 35187 added to function.array-combine
Groups: php.notes 
Request: Send a blank email to php-notes+get-54876@lists.php.net to get a copy of this message
I wrote this one for space saving. I store most of my data in associative arrays and write them to files through serializing. However the keys are repeated multiple times for every new entry, hence it would be better write the arrays without keys and add them after I have read the data. Anyways <?php function array_combine ( &$keys, &$values ) { if ( count ( $keys ) === 0 || count ( $keys ) !== count ( $values ) ) { return false; } $result = array ( ); for ( $i = 0; $i < count ( $keys ); $i++ ) { $result [ $keys [ $i ] ] = $values [ $i ]; } return $result; } function array_combine_assoc ( &$keys, &$values ) { $result = array ( ); if ( !count ( $values ) || ( count ( $values[0] ) !== count ( $keys ) ) ) return false; for ( $i = 0; $i < count ( $values ); $i++ ) { $result [$i] = array_combine ( $keys, $values[$i] ); } return $result; } $keys = array('green','red','yellow'); $values [] = array('avocado','apple','banana'); $values [] = array('grape','wine','something yellow'); $values [] = array('mango','blood','sun'); print_r ( array_combine_assoc ( $keys, $values ) ); ?> ---- Manual Page -- http://www.php.net/manual/en/function.array-combine.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35187 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35187&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35187&report=yes

« previous php.notes (#54876) next »