note 36941 modified in function.implode by vincent

From: Date: Fri, 07 Nov 2003 21:21:15 +0000
Subject: note 36941 modified in function.implode by vincent
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-60121@lists.php.net to get a copy of this message
I found it neccesary to create a function that joins the contents of a single dimension from a 2-d array. Here's the code in case anyone else should need to do the same: <?php function join_2d($glue, $pieces, $dimension = 0){ //joins the values of a single dimension in a 2-d array $rtn = array(); foreach($piece as $key => $value){ if(isset($value[$dimension])){ $rtn[] = $value[$dimension]; } } return join($glue, $rtn); } ?> The dimension argument can be a positive integer or a named index. Here is an example: <?php $testarray = array(array(1 => 'a', 'three' => 1), array(1 => 'b', 'three' => 2), array(1 => 'c', 'three' => 3), array(1 => 'd', 'three' => 4), array(1 => 'e', 'three' => 5)); print "<pre>"; print_r($testarray); print "</pre>"; print join_2d(", ", $testarray, 1) . "<br>"; print join_2d(", ", $testarray, 'three') . "<br>"; ?> --was-- I found it neccesary to create a function that joins the contents of a single dimension from a 2-d array. Here's the code in case anyone else should need to do the same: <?php function join_2d($glue, $pieces, $dimension = 0){ //joins the values of a single dimension in a 2-d array $rtn = array(); foreach($piece as $key => $value){ if(isset($value[$dimension])){ $rtn[] = $value[$dimension]; } } return join($glue, $rtn); } ?> The dimension argument can be a positive integer or a named index. Here is an example: <?php $testarray = array(array(1 => 'a', 'three' => 1), array(1 => 'b', 'three' => 2), array(1 => 'c', 'three' => 3), array(1 => 'd', 'three' => 4), array(1 => 'e', 'three' => 5)); print "<pre>"; print_r($testarray); print "</pre>"; print join_keys(", ", $testarray, 1) . "<br>"; print join_keys(", ", $testarray, 'three') . "<br>"; ?> http://www.php.net/manual/en/function.implode.php

« previous php.notes (#60121) next »