Array Functions

Table of Contents

  • array_change_key_case — Changes the case of all keys in an array
  • array_chunk — Split an array into chunks
  • array_column — Return the values from a single column in the input array
  • array_combine — Creates an array by using one array for keys and another for its values
  • array_count_values — Counts all the values of an array
  • array_diff_assoc — Computes the difference of arrays with additional index check
  • array_diff_key — Computes the difference of arrays using keys for comparison
  • array_diff_uassoc — Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_diff_ukey — Computes the difference of arrays using a callback function on the keys for comparison
  • array_diff — Computes the difference of arrays
  • array_fill_keys — Fill an array with values, specifying keys
  • array_fill — Fill an array with values
  • array_filter — Filters elements of an array using a callback function
  • array_flip — Exchanges all keys with their associated values in an array
  • array_intersect_assoc — Computes the intersection of arrays with additional index check
  • array_intersect_key — Computes the intersection of arrays using keys for comparison
  • array_intersect_uassoc — Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_intersect_ukey — Computes the intersection of arrays using a callback function on the keys for comparison
  • array_intersect — Computes the intersection of arrays
  • array_key_exists — Checks if the given key or index exists in the array
  • array_keys — Return all the keys or a subset of the keys of an array
  • array_map — Applies the callback to the elements of the given arrays
  • array_merge_recursive — Merge two or more arrays recursively
  • array_merge — Merge one or more arrays
  • array_multisort — Sort multiple or multi-dimensional arrays
  • array_pad — Pad array to the specified length with a value
  • array_pop — Pop the element off the end of array
  • array_product — Calculate the product of values in an array
  • array_push — Push one or more elements onto the end of array
  • array_rand — Pick one or more random entries out of an array
  • array_reduce — Iteratively reduce the array to a single value using a callback function
  • array_replace_recursive — Replaces elements from passed arrays into the first array recursively
  • array_replace — Replaces elements from passed arrays into the first array
  • array_reverse — Return an array with elements in reverse order
  • array_search — Searches the array for a given value and returns the corresponding key if successful
  • array_shift — Shift an element off the beginning of array
  • array_slice — Extract a slice of the array
  • array_splice — Remove a portion of the array and replace it with something else
  • array_sum — Calculate the sum of values in an array
  • array_udiff_assoc — Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc — Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_udiff — Computes the difference of arrays by using a callback function for data comparison
  • array_uintersect_assoc — Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_uintersect_uassoc — Computes the intersection of arrays with additional index check, compares data and indexes by a callback functions
  • array_uintersect — Computes the intersection of arrays, compares data by a callback function
  • array_unique — Removes duplicate values from an array
  • array_unshift — Prepend one or more elements to the beginning of an array
  • array_values — Return all the values of an array
  • array_walk_recursive — Apply a user function recursively to every member of an array
  • array_walk — Apply a user function to every member of an array
  • array — Create an array
  • arsort — Sort an array in reverse order and maintain index association
  • asort — Sort an array and maintain index association
  • compact — Create array containing variables and their values
  • count — Count all elements in an array, or something in an object
  • current — Return the current element in an array
  • each — Return the current key and value pair from an array and advance the array cursor
  • end — Set the internal pointer of an array to its last element
  • extract — Import variables into the current symbol table from an array
  • in_array — Checks if a value exists in an array
  • key_exists — Alias of array_key_exists
  • key — Fetch a key from an array
  • krsort — Sort an array by key in reverse order
  • ksort — Sort an array by key
  • list — Assign variables as if they were an array
  • natcasesort — Sort an array using a case insensitive "natural order" algorithm
  • natsort — Sort an array using a "natural order" algorithm
  • next — Advance the internal array pointer of an array
  • pos — Alias of current
  • prev — Rewind the internal array pointer
  • range — Create an array containing a range of elements
  • reset — Set the internal pointer of an array to its first element
  • rsort — Sort an array in reverse order
  • shuffle — Shuffle an array
  • sizeof — Alias of count
  • sort — Sort an array
  • uasort — Sort an array with a user-defined comparison function and maintain index association
  • uksort — Sort an array by keys using a user-defined comparison function
  • usort — Sort an array by values using a user-defined comparison function
add a note add a note

User Contributed Notes 54 notes

up
4
peanutpad at msn dot com
6 years ago
heres a function from http://www.linksback.org  Feedback welcome, of course!  Public domain, yadda yadda.

function mySort(&$array,$key) {
    if (!is_array($array) || count($array) == 0) return true;
    $assocSortCompare  = '$a = $a["'.$key.'"]; $b = $b["'.$key.'"];';

    if (is_numeric($array[0][$key])) {
      $assocSortCompare.= ' return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);';
    } else {
      $assocSortCompare.= ' return strcmp($a,$b);';
    }

    $assocSortCompare = create_function('$a,$b',$assocSortCompare);
    return usort($array,$assocSortCompare);
}
up
5
csorfab at gmail dot com
6 years ago
Here's a function I wrote to combine the values in an array in all different ways, and which returns a two-dimensional array as a byref result.

E.g.

if called:
comb(array(1, 2, 3), $r);

then $r would be:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

<?php

   
function comb($arr, &$rarr, $vtemp = array()){   
        foreach(
$arr as $key => $value){
           
$vtemp2 = $vtemp;
           
$vtemp2[] = $value;

           
$atemp = $arr;           
            unset(
$atemp[$key]);
           
            if(
count($atemp) > 0){
               
comb($atemp, $rarr, $vtemp2);
            } else {
               
$t = array();
           
                foreach(
$vtemp2 as $val){
                   
$t[] = $val;
                }
               
               
$rarr[] = $t;
            }
        }
    }

?>
up
3
za at byza dot it
7 years ago
Updated functions for moving element between associative arrays using a numeric index.

Sample:
$a=array("04"=>"alpha",4=>"bravo","c"=>"charlie","d"=>"delta");
$b=array_move($a,4,2);

Move element "bravo" after "delta" keeping keys.
Old function probably goes wrong moving "alpha": caused by array_search that match numeric index 4 with "04" (string).

Follow:

<?php
// array functions - 20/07/2006 12.28

if(!defined("ARRAY_FUNCS")) {   
   
// Swap 2 elements in array preserving keys.
   
function array_swap(&$array,$key1,$key2) {
       
$v1=$array[$key1];
       
$v2=$array[$key2];
       
$out=array();
        foreach(
$array as $i=>$v) {
            if(
$i===$key1) {
               
$i=$key2;
               
$v=$v2;
            } else if(
$i===$key2) {
               
$i=$key1;
               
$v=$v1;
            }
           
$out[$i]=$v;
        }
        return
$out;
    }
   
   
//  Get a key position in array
   
function array_kpos(&$array,$key) {
       
$x=0;
        foreach(
$array as $i=>$v) {
            if(
$key===$i) return $x;
           
$x++;
        }
        return
false;
    }
   
   
// Return key by position
   
function array_kbypos(&$array,$pos) {
       
$x=0;
        foreach(
$array as $i=>$v) {
            if(
$pos==$x++) return $i;
        }
        return
false;
    }

   
// Move an element inside an array preserving keys
    // $relpos should be like -1, +2...
   
function array_move(&$array,$key,$relpos) {
        if(!
$relpos) return false;
       
$from=array_kpos($array,$key);
        if(
$from===false) return false;
       
$to=$from+$relpos+($relpos>0?1:0);
       
$len=count($array);
        if(
$to>=$len) {
           
$val=$array[$key];
            unset(
$array[$key]);
           
$array[$key]=$val;
        } else {
            if(
$to<0) $to=0;
           
$new=array();       
           
$x=0;
            foreach(
$array as  $i=>$v) {
                if(
$x++==$to) $new[$key]=$array[$key];
                if(
$i!==$key) $new[$i]=$v;
            }
           
$array=$new;
        }
        return
$array;
    }

   
define("ARRAY_FUNCS",true);
}
?>
up
1
chaos dot global dot net at gmail dot com
6 years ago
if some one will need to convert array to php code use this function:

DEFINE('OFFSET_DELIMETER', "\t");

function array2php($array, $offset = OFFSET_DELIMETER)  {
    $text = '';
    foreach($array as $k => $v) {
        if (is_array($v)) {
            $text .= "{$offset}'{$k}' => array(\n".array2php($v, $offset.OFFSET_DELIMETER)."$offset)";
        } else {
            $text .= "{$offset}'{$k}' => ".(is_string($v)? "'$v'": $v);
        }
        $text .= ",\n";
    }
    if ($len = strlen($text)) $text[$len - 2] = " ";
    return $text;
}
up
1
sid dot pasquale at gmail dot com
6 years ago
<?php
/* This function allow you to transform a multidimensional array
   in a simple monodimensional array.
   Usage: array_walk($oldarray, 'flatten_array', &$newarray);
   For example, this code below shows to you:
        Array
        (
            [1] => Array
                (
                    [0] => 1
                    [1] => 2
                )
       
            [2] => Array
                (
                    [0] => 3
                    [1] => 4
                )
       
        )
       
        Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )
*/

function flatten_array($value, $key, &$array) {
    if (!
is_array($value))
       
array_push($array,$value);
    else
       
array_walk($value, 'flatten_array', &$array);
 
}
 
$oldarray = array(
   
1 => array(1,2),
   
2 => array(3,4)
);
$newarray = array();
array_walk($oldarray, 'flatten_array', &$newarray);
echo
"<pre>";
print_r($oldarray);
print_r($newarray);
echo
"</pre>";
?>
up
1
sergio at {NO_SPAM_PLEASE]inservibile dot org
7 years ago
In a pratical problem, I was involved in a system of queries giving the behaviour of all combinations of some parameters. How to write those queries?
The problem was to generate automatically every possible combination of those parameters. I didn't find a function and I wrote it.
(Naturally, a different way could be to build a binary sequence, but I find this function more compact and useful).
So, consider an array of objects and suppose to need all possibile combinations of those objects. Here is the function, that could be useful for some folk.
Enjoy.

<?php
function combinations($elements) {
         if (
is_array($elements)) {
           
/*
            I want to generate an array of combinations, i.e. an array whose elements are arrays
            composed by the elements of the starting object, combined in all possible ways.
            The empty array must be an element of the target array.
            */
           
$combinations=array(array()); # don't forget the empty arrangement!
            /*
            Built the target array, the algorithm is to repeat the operations below for each object of the starting array:
            - take the object from the starting array;
            - generate all arrays given by the target array elements merged with the current object;
            - add every new combination to the target array (the array of arrays);
            - add the current object (as a vector) to the target array, as a combination of one element.
            */
           
foreach ($elements as $element) {
                   
$new_combinations=array(); # temporary array, see below
                   
foreach ($combinations as $combination) {
                           
$new_combination=array_merge($combination,(array)$element);
                           
# I cannot merge directly with the main array ($combinations) because I'm in the foreach cycle
                            # I use a temporary array
                           
array_push($new_combinations,$new_combination);
                            }
                   
$combinations=array_merge($combinations,$new_combinations);
                    }
            return
$combinations;
            } else {
            return
false;
            }
         }
?>

To test the function:

<?php
$elements
=array('bitter','sour','salty','sweet');
print_r(combinations($elements));
?>

The exemple was suggested in 6th century BC.
See why on: http://en.wikipedia.org/wiki/Combinatorics#Overview_and_history
up
1
ms419 at freezone dot co dot uk
6 years ago
This function takes an array, a key and a value. If the key is not an array, it acts just like $array[$key] =& $value; If the key is an array, it recurses in the array, creating nested arrays as necessary.

Example:

$array = array('A' => array('B' => 'phi'));
$key = array('A', 'Z');
$value = 'gamma';

arraySet($array, $key, $value);

$array should now be:

array('A' => array('B' => 'phi', 'C' => 'gamma'));

This is useful for constructing nested arrays from sets of filesystem paths (e.g. 'A/B/C') or structured variable names (e.g. 'A.B.C' or 'A[B][C]')

Example:

arraySet($array, preg_split('/\//', $path, -1, PREG_SPLIT_NO_EMPTY), $value);

function arraySet(array &$array, $key, &$value)
{
    if (is_array($key)) {
        $keyComponent = array_shift($key);

        if (empty($key)) {
            $array[$keyComponent] =& $value;
            return;
        }

        if (!is_array($array[$keyComponent])) {
            $array[$keyComponent] = array();
        }

        PHP_CodeSniffer_arraySet($array[$keyComponent], $key, $value);
        return;
    }

    $array[$key] =& $value;
}
up
1
Mark Lindeman
7 years ago
Very simple solution for changing the key of an associative array, of course it will fail in a lot of scenarios, but will do the trick in simple arrays:

<?php
function replace_key(&$input, $from_key, $to_key)
{
   
$input = unserialize(str_replace(':"'.$from_key.'";', ':"'.$to_key.'";',serialize($input)));
}

$array =array("a"=>1, "B"=>2, "c"=>3);
replace_key($array, "B", "b");
print_r($array);
?>
Output:
Array
(
    [a] => 1
    [b] => 2
    [c] => 3
)
up
1
q*bist
1 year ago
Fill an array with items from another array, wrapping

<?php
function array_window(array $array, $window = 1) {
   
//return array of size $window filled with elements from $array.
    // if count($array) is smaller than $window,
    // elements will be repeated in the returned array

   
$count = count($array);
    if (
$count >= $window) {
       
$ret = array_slice($array, 0, $window);
    } else {
       
//wraparound       
       
$ret = array();
        for (
$i = 0; $i < $window; ++$i) {
           
$crsr = $i;
            if (
$crsr >= count($array)) {
               
$crsr = $crsr - count($array);
            }
           
$ret[] = $array[$crsr];
        }
    }

    return
$ret;
}
?>

Usage:

<?php
$test
= array('one', 'two', 'three', 'four', 'five', 'six');
print_r(array_window($test, 4));
?>

will print

Array ( [0] => one [1] => two [2] => three [3] => four )

<?php
$test
= array('one', 'two', 'three', 'four', 'five', 'six');
print_r(array_window($test, 9));
?>

will print

Array ( [0] => one [1] => two [2] => three [3] => four [4] => five [5] => six [6] => one [7] => two [8] => three )
up
1
dennis at DONTSPAMME dot born05 dot nl
6 years ago
I haven't completely figured out when or why an array gets referenced or copied, but it gave me quite the headache..

So i wrote a function which recursively copies an array while preserving keys and also clones objects if encountered

<?php
   
/**
     * make a recursive copy of an array
     *
     * @param array $aSource
     * @return array    copy of source array
     */
   
function array_copy ($aSource) {
       
// check if input is really an array
       
if (!is_array($aSource)) {
            throw new
Exception("Input is not an Array");
        }
       
       
// initialize return array
       
$aRetAr = array();
       
       
// get array keys
       
$aKeys = array_keys($aSource);
       
// get array values
       
$aVals = array_values($aSource);
       
       
// loop through array and assign keys+values to new return array
       
for ($x=0;$x<count($aKeys);$x++) {
           
// clone if object
           
if (is_object($aVals[$x])) {
               
$aRetAr[$aKeys[$x]]=clone $aVals[$x];
           
// recursively add array
           
} elseif (is_array($aVals[$x])) {
               
$aRetAr[$aKeys[$x]]=array_copy ($aVals[$x]);
           
// assign just a plain scalar value
           
} else {
               
$aRetAr[$aKeys[$x]]=$aVals[$x];
            }
        }
       
        return
$aRetAr;
    }
?>

I hope i can save someone else's aspirine with this ;)
up
1
mo dot longman at gmail dot com
6 years ago
to 2g4wx3:
i think better way for this is using JSON, if you have such module in your PHP. See json.org.

to convert JS array to JSON string: arr.toJSONString();
to convert JSON string to PHP array: json_decode($jsonString);

You can also stringify objects, numbers, etc.
up
1
ob at babcom dot biz
7 years ago
Here are two functions Array2String() and String2Array() based on functions posted below by daenders AT yahoo DOT com.

An improvement handling NULL values correctly was posted by designatevoid at gmail dot com.

My version also solves the NULL-value-problem plus keeps support of multidimensional arrays.

<?php
// convert a multidimensional array to url save and encoded string
// usage: string Array2String( array Array )

function Array2String($Array) {
 
$Return='';
 
$NullValue="^^^";
  foreach (
$Array as $Key => $Value) {
    if(
is_array($Value))
     
$ReturnValue='^^array^'.Array2String($Value);
    else
     
$ReturnValue=(strlen($Value)>0)?$Value:$NullValue;
   
$Return.=urlencode(base64_encode($Key)) . '|' . urlencode(base64_encode($ReturnValue)).'||';
  }
  return
urlencode(substr($Return,0,-2));
}
?>

<?php
// convert a string generated with Array2String() back to the original (multidimensional) array
// usage: array String2Array ( string String)

function String2Array($String) {
 
$Return=array();
 
$String=urldecode($String);
 
$TempArray=explode('||',$String);
 
$NullValue=urlencode(base64_encode("^^^"));
  foreach (
$TempArray as $TempValue) {
    list(
$Key,$Value)=explode('|',$TempValue);
   
$DecodedKey=base64_decode(urldecode($Key));
    if(
$Value!=$NullValue) {
     
$ReturnValue=base64_decode(urldecode($Value));
      if(
substr($ReturnValue,0,8)=='^^array^')
       
$ReturnValue=String2Array(substr($ReturnValue,8));
     
$Return[$DecodedKey]=$ReturnValue;
     }
    else
     
$Return[$DecodedKey]=NULL;
  }
  return
$Return;
}
?>
up
1
nick
7 years ago
This little function will move an array element up or down. Unlike the similar function in a previous comment this will work for associative arrays too.

Because it uses current to traverse the array it will fail if a value is false (or 0). It could probably be rewritten to use each() but I couldn't work it out.
<?php
function array_move_element($array, $value, $direction = 'up') {
   
   
$temp = array();
   
    if(
end($array) == $value && $direction == 'down') {
        return
$array;
    }
    if(
reset($array) == $value && $direction == 'up') {
        return
$array;
    }

    while (
$array_value = current($array)) {
       
       
$this_key = key($array);

        if (
$array_value == $value) {
            if(
$direction == 'down') {
               
$next_value = next($array);
               
$temp[key($array)] = $next_value;
               
$temp[$this_key] = $array_value;
            } else {
               
$prev_value = prev($array);
               
$prev_key = key($array);
                unset(
$temp[$prev_key]);
               
$temp[$this_key] = $array_value;
               
$temp[$prev_key] = $prev_value;
               
next($array);
               
next($array);
            }
            continue;
        } else {
           
$temp[$this_key] = $array_value;
        }

       
next($array);
    }
    return
$temp;
   
}
?>
up
1
za at lombardiacom dot it
8 years ago
<?php
   
// Swap 2 elements in array preserving keys.
   
function array_swap(&$array,$key1,$key2) {
       
$v1=$array[$key1];
       
$v2=$array[$key2];
       
$out=array();
        foreach(
$array as $i=>$v) {
            if(
$i==$key1) {
               
$i=$key2;
               
$v=$v2;
            } else if(
$i==$key2) {
               
$i=$key1;
               
$v=$v1;
            }
           
$out[$i]=$v;
        }
        return
$out;
    }

   
// Move an element inside an array preserving keys.
   
function array_move(&$array,$key,$position) {
       
$from=array_search($key,array_keys($array));
       
$to=$from+$position;   
       
$tot=count($array);
        if(
$position>0) $to++;
        if(
$to<0) $to=0;
        else if(
$to>=$tot) $to=$tot-1;
       
$n=0;
       
$out=array();
        foreach(
$array as $i=>$v) {
            if(
$n==$to) $out[$key]=$array[$key];
            if(
$n++==$from) continue;
           
$out[$i]=$v;
        }       
        return
$out;
    }
?>
up
0
shadiadiph at yahoo dot com
4 years ago
This is a useful function to remove an array value from an array.

<?php

function rem_array($array,$str){
foreach (
$array as $key => $value) {
if (
$array[$key] == "$str") unset($array[$key]);
}
return
$array;
}

$selected='';
$selected ="three";
$numbers=array();
$numbers=array('one','two','three','four');
print_r($numbers);

// returns  Array ( [0] => one [1] => two [2] => three [3] => four )

// now we are going to call the function to see if the number three the $selected number is in the array $numbers and remove it.

$numbers = rem_array($numbers,$selected);

print_r ($numbers);
// returns  Array ( [0] => one [1] => two [3] => four )

?>

I am finding it usefull for drop down lists on forms where you do not want the selected value duplicated.
up
0
jan at hooda dot de
5 years ago
Two simple functions to add and delete values from arrays:

<?php
     
function array_add (&$array, $val) {
       
array_push ($array, $val);
      }

      function
array_del (&$array, $str)
      {
        if (
in_array($str,$array)==true) {
          foreach (
$array as $key=>$value) {
            if (
$value==$str) unset($array[$key]);
          }
        }
      } 
?>
up
0
renatonascto at gmail dot com
5 years ago
Big arrays use a lot of memory possibly resulting in memory limit errors. You can reduce memory usage on your script by destroying them as soon as you´re done with them. I was able to get over a few megabytes of memory by simply destroying some variables I didn´t use anymore.
You can view the memory usage/gain by using the funcion memory_get_usage(). Hope this helps!
up
0
Anonymous
6 years ago
This is the shorter way to flatten a array:
<?php
 
function array_flatten($a) {
    foreach(
$a as $k=>$v) $a[$k]=(array)$v;
    return
call_user_func_array(array_merge,$a);
  }
?>
up
0
Hayley Watson
6 years ago
Regarding cyberchrist at futura dot net's function. It makes an unnecessary array_merge(); the elements of $b that are merged with those of $a are immediately removed again by the array_diff(). The "limiting to known values" is entirely unnecessary, in other words: arrays already only contain "known values".

Also, the description and function only address the issue of whether $a is a subset of $b, not whether it is a proper subset. For $a to be a proper subset of $b, it must also be the case that $b is not a subset of $a.

Taking those points into account (and a personal aesthetic dislike of "if(test) return true; else return false;" gives:

<?php
function is_subset($a, $b)
{
    return
count(array_diff($a,$b))==0;
}

function
is_proper_subset($a, $b)
{
    return
is_subset($a, $b) && !is_subset($b, $a);
}
?>
up
0
Kazuyoshi Tlacaelel
6 years ago
<?php
/**
 * converts a multidimensional array to a flat array
 *
 * trying to keep the original names of the keys
 * if repeated keys are found a hash will be added to the
 * keys trying to keep as much as possible of the original
 * key context
 *
 * september 30 2007
 *
 * PHP version 5
 *
 * @license         GPL
 *
 */

$array = array ( 0 => array ( 0 => 1, 1 => 2, 2 => array ( 0 => 3, 1 => 4, 2 =>
array (
0 => 5, 1 => 6, 2 => array ( 0 => 7, 1 => 8,),),), 3 => array (
   
0 => array ( 0 => 9, 1 => 10, 2 => array ( 0 => 11, 1 => 12,
   
2 => array ( 0 => 13, 1 => 14, 2 => array ( 0 => 15, 1 => 16,),),),),
   
1 => array ( 0 => 17, 1 => 18,),),), 1 => array ( 0 => 19, 1 => 20,),
   
2 => array ( 0 => array ( 0 => 21, 1 => 22, 2 => array ( 0 => 23, 1 => 24,
   
2 => array ( 0 => 25, 1 => 26, 2 => array ( 0 => 27, 1 => 28,),),),),
   
1 => array ( 0 => 29, 1 => 30,),),);

/**
 * transforms a multidimensional array to a flat array
 *
 * the parameter is referenced
 * so no returning value is needed
 * @param array $array the multidimensional array to flat
 * @return void
 */
function array_flatten(&$array)
{
    function
has_arrays($array)
    {
        foreach (
$array as $item) {
            if (
is_array($item)) {
                return
true;
            }
        }
        return
false;
    }

    function
copy_array(&$array, $array_key)
    {
       
$array2 = $array[$array_key];
        unset(
$array[$array_key]);
        foreach (
$array2 as $subkey => $subvalue) {
            if (
array_key_exists($subkey, $array)) {
               
$array[generate_unique_key($subkey)] = $subvalue;
            } else {
               
$array[$subkey] = $subvalue;
            }
        }
    }

    function
generate_unique_key($key)
    {
        if (
strlen($key)>8) {
           
$key = $key[0] . $key[1] . $key[2];
        }
       
$id = $key . '_';
       
$uid = uniqid();
       
$len = strlen($uid);
       
$max = (9 - strlen($key));
        for (
$c = $len; ; $c --) {
           
$id .= $uid[$c];
            if (
$c == ($len - $max)) {
                break;
            }
        }
        return
$id;
    }

    function
get_array_indexes($array)
    {
       
$ret_array = array();
        foreach (
$array as $key => $value) {
            if (
is_array($value)) {
               
$ret_array[] = $key;
            }
        }
        return
$ret_array;
    }

    while(
has_arrays($array)) {
        foreach (
get_array_indexes($array) as $key) {
           
copy_array($array, $key);
        }
    }
}

   
array_flatten($array);
   
array_multisort($array);
   
var_export($array);

   
/**
     *  OUTPUT
     *
     *  array (
     *    0 => 1,
     *    '1_403767b6' => 2,
     *    '0_793767b6' => 3,
     *    '1_8a3767b6' => 4,
     *    '0_454767b6' => 5,
     *    '1_564767b6' => 6,
     *    '0_035767b6' => 7,
     *    '1_345767b6' => 8,
     *    '0_e74767b6' => 9,
     *    '1_f84767b6' => 10,
     *    '0_855767b6' => 11,
     *    '1_a65767b6' => 12,
     *    '0_4e5767b6' => 13,
     *    '1_6f5767b6' => 14,
     *    '0_566767b6' => 15,
     *    '1_876767b6' => 16,
     *    '0_5b4767b6' => 17,
     *    '1_6c4767b6' => 18,
     *    '0_d43767b6' => 19,
     *    1 => 20,
     *    '0_4e3767b6' => 21,
     *    '1_5f3767b6' => 22,
     *    '0_ad4767b6' => 23,
     *    '1_ce4767b6' => 24,
     *    '0_485767b6' => 25,
     *    '1_695767b6' => 26,
     *    '0_116767b6' => 27,
     *    '1_426767b6' => 28,
     *    '0_814767b6' => 29,
     *    '1_924767b6' => 30,
     *  )
     */
?>
up
0
webdev at svbeatrix dot com
6 years ago
Bugs happen, but how can people post functions that WON'T EVEN COMPILE!  I truly detest finding a cool code snippet or function and then having to debug them. Sorry for the rant, but I have experienced this scenario a number of times.  TEST YOUR CODE, THEN POST!

Here is a revised and corrected previously posted function ArrayDepth, which had 3 bugs and yes, would not compile.

function ArrayDepth($Array,$DepthCount=-1) {
// Find maximum depth of an array
// Usage: int ArrayDepth( array $array )
// returns integer with max depth
// if Array is a string or an empty array it will return 0
  $DepthArray=array(0);
  $DepthCount++;
  $Depth = 0;
  if (is_array($Array))
    foreach ($Array as $Key => $Value) {
      $DepthArray[]=ArrayDepth($Value,$DepthCount);
    }
  else
    return $DepthCount;
  return max($DepthCount,max($DepthArray));
}
up
0
info at dyflexis dot nl
7 years ago
A modernized version of the flatten_array() functies written by

davidj at boundlessgallery dot DISLIKESPAM dot com
on 02-Apr-2004 03:10

This function is able to work with associative arrays

<?php
function flatten_array($array) {
    
$size=sizeof($array);
    
$keys=array_keys($array);
    for(
$x = 0; $x < $size; $x++) {
        
$element = $array[$keys[$x]];

        if(
is_array($element)) {
           
$results = flatten_array($element);
               
$sr = sizeof($results);
               
$sk=array_keys($results);
            for(
$y = 0; $y < $sr; $y++) {
               
$flat_array[$sk[$y]] = $results[$sk[$y]];
            }
        } else {
           
$flat_array[$keys[$x]] = $element;
        }
    }

    return
$flat_array;
}
?>
up
0
info at joolee dot nl
7 years ago
A slight modification in the arraytostring function, posted below. This function lists an array the same way you would define it in PHP.

<?PHP
function arraytostring($array, $depth = 0)
{
   if(
$depth > 0)
     
$tab = implode('', array_fill(0, $depth, "\t"));
  
$text.="array(\n";
  
$count=count($array);

   foreach (
$array as $key=>$value)
   {
      
$x++;

       if (
is_array($value))
       {
           if(
substr($text,-1,1)==')')    $text .= ',';
          
$text.=$tab."\t".'"'.$key.'"'." => ".arraytostring($value, $depth+1);
           continue;
       }

      
$text.=$tab."\t"."\"$key\" => \"$value\"";

       if (
$count!=$x) $text.=",\n";
   }

  
$text.="\n".$tab.")\n";

   if(
substr($text, -4, 4)=='),),')$text.='))';

   return
$text;
}
?>
up
0
elkabong at samsalisbury dot co dot uk
7 years ago
An improvement to the array_deep_copy function I posted ages ago which takes a 'snapshot' of an array, making copies of all actual values referenced... Now it is possible to prevent it traversing the tree forever when an array references itself. You can set the default $maxdepth to anything you like, you should never call this function with the $depth specified!
<?php
/* Make a complete deep copy of an array replacing
references with deep copies until a certain depth is reached
($maxdepth) whereupon references are copied as-is...  */
function array_deep_copy (&$array, &$copy, $maxdepth=50, $depth=0) {
    if(
$depth > $maxdepth) { $copy = $array; return; }
    if(!
is_array($copy)) $copy = array();
    foreach(
$array as $k => &$v) {
        if(
is_array($v)) {        array_deep_copy($v,$copy[$k],$maxdepth,++$depth);
        } else {
           
$copy[$k] = $v;
        }
    }
}

# call it like this:

array_deep_copy($array_to_be_copied,$deep_copy_of_array,$maxdepth);
?>
Hope someone finds it useful!
up
0
ob at babcom dot biz
7 years ago
Regarding my own posting 2 postings down about the function ArrayDepth() to find the maximum depth of a multidimensional array:

A number of functions for counting array dimensions have been posted. And I tried using them. Yet, if at all they only return the depth of the first branch of the array. They can not handle arrays where a later path holds a more dimension than the first.

My version will check all paths down the array and return the maximum depth. That's why I posted it.
up
0
ob at babcom dot biz
7 years ago
Here is a function to find out the maximum depth of a multidimensional array.

<?php
// return depth of given array
// if Array is a string ArrayDepth() will return 0
// usage: int ArrayDepth(array Array)

function ArrayDepth($Array,$DepthCount=-1,$DepthArray=array()) {
 
$DepthCount++;
  if (
is_array($Array))
    foreach (
$Array as $Key => $Value)
     
$DepthArray[]=ArrayDepth($Value,$DepthCount);
  else
    return
$DepthCount;
  foreach(
$DepthArray as $Value)
   
$Depth=$Value>$Depth?$Value:$Depth;
  return
$Depth;
}
?>
up
0
ob at babcom dot biz
7 years ago
Regarding the function of spam at madhermit dot net from January 9th 2006:

That function only preserves the deepest keys and values.
If you try to flatten an array with that function where the deepest instance of keys might be the same where as keys in the "key-path" are different, values will be overwritten.

So here is a function that preserves the whole key-path and the keys of the flattened array will be string keys consisting of the key-path separated by $Separator.

<?php
// flatten multidimensional array to one dimension
// preserves keys by generating a key for the flattened array which consists of the
// key-path of the multidimensional array separated by $Separator
// usage: array ArrayFlatten( array Array [, string Separator] )

function ArrayFlatten($Array,$Separator="_",$FlattenedKey='') {
 
$FlattenedArray=Array();
  foreach(
$Array as $Key => $Value) {
    if(
is_Array($Value))
     
$FlattenedArray=Array_merge($FlattenedArray,
                                 
ArrayFlatten($Value,$Separator,
                                               (
strlen($FlattenedKey)>0
                                               
?$FlattenedKey.$Separator
                                               
:"").$Key)
                                               );
    else
     
$FlattenedArray[$FlattenedKey.$Separator.$Key]=$Value;
  }
  return
$FlattenedArray;
}
?>
up
0
jonathan at sharpmedia dot net
8 years ago
/**
 * Flattens a multimentional array.
 *
 * Takes a multi-dimentional array as input and returns a flattened
 * array as output. Implemented using a non-recursive algorithm.
 * Example:
 * <code>
 * $in = array('John', 'Jim', array('Jane', 'Jasmine'), 'Jake');
 * $out = array_flatten($in);
 * // $out = array('John', 'Jim', 'Jane', 'Jasmine', 'Jake');
 * </code>
 *
 * @author        Jonathan Sharp <jonathan@sharpmedia.net>
 * @var            array
 * @returns        array
 */
function array_flatten($array)
{
    while (($v = array_shift($array)) !== null) {
        if (is_array($v)) {
            $array = array_merge($v, $array);
        } else {
            $tmp[] = $v;
        }
    }
   
    return $tmp;
}
up
0
stalker at ruun dot de
8 years ago
I had some problems while selecting sub-arrays from multi-dimensional arrays (like the SQL-WHERE clause), so i wrote the following function:

<?php
function selectMultiArray($__multiarray,$__key,$__value) {
  foreach(
$__multiarray as $multipart) {
    if(
$multipart[$__key] == $__value) {
     
$__return[] = $multipart;
    }
  }
  if(empty(
$__return)) {
    return
FALSE;
  }
  return
$__return;
}
?>

hope someones finding this helpful. If you have better was for getting to this, please answer.

greets,
St4Lk3R
up
0
msajko at gmail dot com
8 years ago
array_to_string and sister function string_to_array with multi dimensional array support.

// Converts an array to a string that is safe to pass via a URL
function array_to_string($array) {
   $retval = '';
   $null_value = "^^^";
   foreach ($array as $index => $val) {
       if(gettype($val)=='array') $value='^^array^'.array_to_string($val);    else $value=$val;
       if (!$value)
           $value = $null_value;
       $retval .= urlencode(base64_encode($index)) . '|' . urlencode(base64_encode($value)) . '||';
   }
   return urlencode(substr($retval, 0, -2));
}
 
// Converts a string created by array_to_string() back into an array.
function string_to_array($string) {
   $retval = array();
   $string = urldecode($string);
   $tmp_array = explode('||', $string);
   $null_value = urlencode(base64_encode("^^^"));
   foreach ($tmp_array as $tmp_val) {
       list($index, $value) = explode('|', $tmp_val);
       $decoded_index = base64_decode(urldecode($index));
       if($value != $null_value){
           $val= base64_decode(urldecode($value));
           if(substr($val,0,8)=='^^array^') $val=string_to_array(substr($val,8));
           $retval[$decoded_index]=$val;
          }
       else
           $retval[$decoded_index] = NULL;
   }
   return $retval;
}
up
0
Domenic Denicola
8 years ago
Another JavaScript conversion, this time to objects instead of arrays. They can be accessed the same way, but are declared much shorter, so it saves some download time for your users:

<?
function PhpArrayToJsObject($array, $objName)
{
    return 'var ' . $objName . ' = ' . PhpArrayToJsObject_Recurse($array) . ";\n";
}

function PhpArrayToJsObject_Recurse($array)
{
    // Base case of recursion: when the passed value is not a PHP array, just output it (in quotes).
    if(! is_array($array) )
    {
        // Handle null specially: otherwise it becomes "".
        if ($array === null)
        {
            return 'null';
        }
       
        return '"' . $array . '"';
    }
   
    // Open this JS object.
    $retVal = "{";

    // Output all key/value pairs as "$key" : $value
    // * Output a JS object (using recursion), if $value is a PHP array.
    // * Output the value in quotes, if $value is not an array (see above).
    $first = true;
    foreach($array as $key => $value)
    {
        // Add a comma before all but the first pair.
        if (! $first )
        {
            $retVal .= ', ';
        }
        $first = false;
       
        // Quote $key if it's a string.
        if (is_string($key) )
        {
            $key = '"' . $key . '"';
        }
       
        $retVal .= $key . ' : ' . PhpArrayToJsObject_Recurse($value);
    }

    // Close and return the JS object.
    return $retVal . "}";
}
?>

Difference from previous function: null values are no longer "" in the object, they are JavaScript null.

So for example:

<?
$theArray = array("A" => array("a", "b", "c" => array("x")), "B" => "y");
echo PhpArrayToJsObject($theArray, "myArray");
?>

Gives:

var myArray = {"A" : {0 : "a", 1 : "b", "c" : {0 : "x"}}, "B" : "y"};

You can still access them just like arrays, with myArray["A"][0] or myArray["A"]["c"][0] or whatever. Just shrinks your pages.
up
0
m227(a)poczta.onet.pl
9 years ago
How to count dimensions in multi-array? (corrected)

previous version didn't work when called more than one time.
($dimcount was preserved from previous call)

This is the way I corrected this:

function countdim($array)
  {
    if (is_array(reset($array)))  
      $return = countdim(reset($array)) + 1;
    else
      $return = 1;
 
    return $return;
  }

This function will return int number of array dimensions.
up
-1
pragash_jey at yahoo dot com
6 years ago
Java Like Array Implementation!

<?php
/*
 * Author : Pragash Jeyaratnam
 * Vector Class
 * PHP Version 5.2.5
 * Contact : pragash_jey@yahoo.com.
 */
class Vector
{
   
/*
     * The number of valid components in this Vector object.
     */
   
protected $elementCount;
   
/*
     * The array buffer into which the components of the vector are stored.
     */
   
protected $elementData;
   
   
         
   
/**Default vector Constructor*/
   
function __construct()
    {
       
$this->elementData = array();
       
$this->elementCount = count($this->elementData);
    }
   
   
   
   
/**Append the parameter element to the vector*/
   
public function add($object)
    {
        if(!empty(
$object))
        {
           
$this->elementData[] = $object;
           
$this->elementCount = count($this->elementData);
            return
true;
        }
        else
            return
false;
           
    }
   
   
/*
     * Returns true of Vector is empty false otherwise
     */
   
public function isEmpty()
    {
        return(
$this->elementCount==0);
    }
   
   
 
/*
   * Returns the current size of this Vector
   */
  
public function size()
   {
       return
$this->elementCount;
   }
   
  
/*
    *  Searches for the first occurence of the given argument
    **/
  
public function indexOf($object)
   {
          if((
$index =array_search($object,$this->elementData)) !==false)
              return
$index;
          else
              return -
1;
   }
  
  
/*
    * This function will retain TRUE if $object is contained
    * within the vector else FALSE
    */
  
  
public function contains($object)
   {
           return (
$this->indexOf($object)>=0);
   }
   
  
/*
    * Returns Vector Object at index $index
    * Error : Null is returned
    */
     
  
public function get($index)
   {
           if(
$this->checkBound($index))
           {
               return (
$this->elementData[$index]);
           }
           return
NULL;
   }
  
  
/*
    * Sets the object at $index to be $object
    **/
  
  
public function set($index,$object)
   {
           if(
$this->checkBound($index))
           {
              
$this->elementData[$index] =$object ;
               return
true;
           }
           return
false;
   }
  
 
/*
   * Removes element at $index
   **/
  
     
public function removeAt($index)
      {
       if (
$this->checkBound($index))
           {
                 for (
$i = $index; $i != $this->elementCount - 1; $i++)
                  {
                  
$this->elementData[$i] = $this->elementData[$i +1];
             }
      
array_pop($this->elementData);
      
$this->elementCount = count($this->elementData);
           
       return
true;
       }
       else {
            return
false;
            }
      }
   
   
/**
     * Removes all elements from the Vector.  Note that this does not
     * resize the internal data array.
     */
  
public function removeAll()
   {
         if(
$this->elementCount ==0)
             return;
         for(
$i=0;$i<$this->size();$i++)
         {
            
$this->set($i,NULL);
         }
   }
   
   
  
/*
    * This function checks whether index
    * is within the array bound
    **/
  
  
private function checkBound($index)
   {
         if(
$index > $this->elementCount-1 || $index < 0)
         {
             throw new
Exception('Array Index Out Of Bound Exception');
             return
false;
         }
         return
true;
   }
  
   
/*
     *Removes the first element from the Vector
     */
   
   
public function removeFirstElement()
    {
        if(
$this->elementCount==0)
        {
            throw new
Exception('No Such Element');
        }
       
$test =array_shift($this->elementData);
        if(!empty(
$test))
           
$this->elementCount = count($this->elementData);
    }
  
}
?>
up
-1
rune at zedeler dot dk
7 years ago
Notice that keys are considered equal if they are "=="-equal. That is:

<?
$a = array();
$a[1] = 'this is the first value';
$a[true] = 'this value overrides the first value';
$a['1'] = 'so does this one';
?>
up
-1
Nitrogen
3 years ago
I made a simple little function to make an array (containing readable data) presentable in human form.
I added some error checking, too.. (if it's not human-readable, don't include it).
<?php
function Combine($Input) {
  if(!
is_array($Input)) // I only want arrays.
   
return(false);

 
// remove named index (only numeric index)
 
$Input=array_values($Input);

 
// read the array, if it's not a string or number, remove it!
 
for($i=0;$i<count($Input);$i++)
    if(!
is_string($Input[$i])&&!is_numeric($Input[$i]))
      unset(
$Input[$i]);

 
// if an element got removed in the middle of the array, it leaves holes in the
  // array index, so we use array_values to reset them again.
 
$Input=array_values($Input);

 
// several array counts used asead.
 
$Count=count($Input);

  if(
$Count>1)
    return(
implode(', ',array_splice($Input,0,$Count-1)).' and '.$Input[0]);
  if(
$Count==1)
    return(
$Input[0]);
  if(
$Count==0)
    return(
'');
 
// nothing should happen beyond this point.
}

Combine(array('A')); // A
Combine(array('A','B')); // A and B
Combine(array('A','B','C')); // A, B and C
Combine(array('A','B','9',4,5)); // A, B, 9, 4 and 5
Combine(array(3,false,5,'nine','?!?!')); // 3, 5, nine and ?!?!
// and so on...

?>

Hope this helps someone,
Nitrogen.
up
-1
phektus at gmail dot com
4 years ago
This is how I solved my array combinations problem. My particular situation is that I needed to project combinations of certain results and analyse each combination based from a consolidation of one of their attributes. The following example does not show the analysis part but it shows how I came up with my combinations ($traits).

This is actually an improvement on: http://ask.metafilter.com/64554/Finding-combinations-in-PHP
as it deals with arrays of strings, while I need to combine arrays within an array (you guessed it, database results).

Hope somebody finds this useful or teaches me a simpler way [by email] to do this. Public domain etc.

<?php
    header
('Content-Type: text/plain');
   
    class
Test {
       
// holds the combinations
       
var $combinations= array();               
   
        function
getCombinations($batch, $elements, $i)  {
            if (
$i >= count($elements)) {
               
$this->combinations[] = $batch;
            } else {     
                foreach (
$elements[$i] as $element) {
                   
$this->getCombinations(array_merge($batch, $element), $elements, $i + 1);
                }                       
            }
        }
    }       
   
   
   
// Test it!
   
   
$traits = array (
        array(
            array(
'Happy'),
            array(
'Sad'),
            array(
'Angry'),
            array(
'Hopeful')
        ),
        array(
            array(
'Outgoing'),
            array(
'Introverted')
        ),
        array(
            array(
'Tall'),
            array(
'Short'),
            array(
'Medium')
        ),
        array(
            array(
'Violent'),
            array(
'Quiet'),
            array(
'Psychotic')
        ),
        array(
            array(
'Handsome'),
            array(
'Plain'),
            array(
'Ugly')
        )
    );

   
$test = new Test();
   
$start = array();
   
$test->getCombinations($start, $traits, 0);   
   
print_r($test->combinations);           
   
?>
up
-1
vinaur at gmail dot com
7 years ago
Regarding the array to string (parse_line) and string to array (parse_array) functions posted below by Kevin Law.

The functions will not work correctly if the array being parsed contains values that include commas and possibly parentheses.

To solve this problem I added urlencode and urldecode functions and the result looks like this:

<?php
function parse_line($array){
   
$line = "";
    foreach(
$array AS $key => $value){
        if(
is_array($value)){
           
$value = "(". parse_line($value) . ")";
        }
        else
        {
           
$value = urlencode($value);
        }
       
$line = $line . "," . urlencode($key) . ":" . $value . "";           
    }
   
$line = substr($line, 1);
    return
$line;
}

function
parse_array($line){
  
$q_pos = strpos($line, ":");
  
$name = urldecode(substr($line,0,$q_pos));
  
$line = trim(substr($line,$q_pos+1));
  
$open_backet_pos = strpos($line, "(");

   if(
$open_backet_pos===false || $open_backet_pos>0){
      
$comma_pos = strpos($line, ",");
       if(
$comma_pos===false){
          
$result[$name] = urldecode($line);
          
$line = "";
       }else{
          
$result[$name] = urldecode(substr($line,0,$comma_pos));
          
$result = array_merge($result, parse_array(substr($line,$comma_pos+1)));
          
$line = "";
       }
   }else if (
$open_backet_pos==0){
      
$line = substr($line,1);
      
$num_backet = 1;
      
$line_char_array = str_split($line);
       for(
$index = 0; count($line_char_array); $index++){
           if(
$line_char_array[$index] == '('){
              
$num_backet++;
           }else if (
$line_char_array[$index] == ')'){
              
$num_backet--;
           }
           if(
$num_backet == 0){
               break;
           }
       }
      
$sub_line = substr($line,0,$index);
      
$result[$name] = parse_array($sub_line);
      
$line = substr($line,$index+2);
   }
   if(
strlen($line)!=0){
      
$result = array_merge($result, parse_array($line));
   }

   return
$result;
}
?>
up
-1
ducnm0 at gmail dot com
4 years ago
A function to convert php array into JS object, supporting both object and array:

<?php
static function is_assoc($array) {
        foreach (
array_keys ( $array ) as $k => $v ) {
            if (
$k !== $v)
                return
true;
        }
        return
false;
    }
   
    static function
arrayPHPToJS($data) {
        if (
is_null($data)) return 'null';
        if (
is_string($data)) return "'" . $data . "'";
        if (
self::is_assoc($data)) {
           
$a=array();
            foreach (
$data as $key => $val )
               
$a[]="'" . $key . "' :" .self::arrayPHPtoJS($val);
            return
"{" . implode ( ', ', $a ) . "}";
        }
        if (
is_array($data)) {
           
$a=array();
            foreach (
$data as $val )
               
$a[]=self::arrayPHPtoJS($val);
            return
"[" . implode ( ', ', $a ) . "]";
        }
        return
$data;
    }
?>
up
-1
christian dot reinecke at web dot de
6 years ago
Here are two more functions handling string2array and access array by string. It's useful if you want to access an array in xpath-style, such as parse_ini_file return values with $process_sections = TRUE.

<?php
function createArrayByString($string, $separator, $value = NULL)
{
    if (
strlen($string) > 0) {
       
$splitter = explode($separator, $string);
       
$index = array_shift($splitter); // get first element
       
$function = __FUNCTION__;
        return array(
$index => $function(implode($separator, $splitter), $separator, $value));
    }
    return
$value;
}

function
accessArrayByString($array, $string, $separator, $default = NULL)
{
    if (!
is_array($array)) {
        return empty(
$string) ? $array : $default;
    }
    @list (
$key, $rest) = explode($separator, $string, 2); // @ needed for last access
   
   
$function = __FUNCTION__;
    return
array_key_exists($key, $array)
         ?
$function($array[$key], $rest, $separator, $default)
         :
$default;
}

$string = "one.two.three.four";
$separator = ".";

$array = createArrayByString($string, $separator, "standard value");
$return = accessArrayByString($array, $string, $separator, "element not found");

var_dump($array); // should return array("one" => array("two" => array("three" => array("four" => "standard value"))))
var_dump($return); // should return "standard value";
?>

change the function name to what ever you want, the recursive call uses __FUNCTION__ for re-call.
up
-1
g4wx3
6 years ago
I needed a function to convert a php array into a javascript array.
No problem i found it on "the net".

But the function i found wasn't good enough, instead of return a string with javascript-array it echoed directly everything.
I wanted to write the string to a file, when calling the function out of my function libary.

Secondly, there where minor "bugs" in the script, when you're original array contained characters like line breaks(\r\n,..), or quotes('), it would hack up the javascript array

Sow, i changed the function and fixed the bug.

<?php
//SUPER COOL : http://www.communitymx.com/content/article.cfm?page=3&cid=7CD16
//Checkout:  REVERSE: http://www.hscripts.com/tutorials/php/jsArrayToPHP.php
//Convert a PHP array to a JavaScript one (rev. 4)
//Changlog by g4wx3: echo replaced by $output, added function output
function output($string) //make javascript ready
   
{
   
$string = str_replace( array( '\\' , '\'' ), array('\\\\', '\\\'') , $string); //-> for javascript array
   
$string = str_replace(  array("\r\n", "\r", "\n") , '<br>' , $string);    //nl2br
   
return $string;
    }
function
arrayToJS4($array, $baseName ) {
   
//Write out the initial array definition
//v4    echo ($baseName . " = new Array(); \r\n ");
   
$output = $baseName . " = new Array(); \r\n ";

   
//Reset the array loop pointer
   
reset ($array);

   
//Use list() and each() to loop over each key/value
    //pair of the array
   
while (list($key, $value) = each($array)) {
        if (
is_numeric($key)) {
       
//A numeric key, so output as usual
       
$outKey = "[" . $key . "]";
        } else {
       
//A string key, so output as a string
       
$outKey = "['" . $key . "']";
        }
     
        if (
is_array($value)) {
       
//The value is another array, so simply call
        //another instance of this function to handle it
       
$output .= arrayToJS4($value, $baseName . $outKey);
        } else {

           
//Output the key declaration
//v4            echo ($baseName . $outKey . " = ");     
           
$output .= $baseName . $outKey . " = ";
           
           
//Now output the value
           
if (is_string($value)) {
               
//Output as a string, as we did before      
//v4                echo ("'" . output($value) . "'; \r\n ");
               
$output .= "'" . output($value) . "'; \r\n ";
            } else if (
$value === false) {
               
//Explicitly output false
//v4                echo ("false; \r\n");
               
$output .= "false; \r\n";
            } else if (
$value === NULL) {
               
//Explicitly output null
//v4                echo ("null; \r\n");
               
$output .= "null; \r\n";
            } else if (
$value === true) {
               
//Explicitly output true
//v4                echo ("true; \r\n");
               
$output .= "true; \r\n";
            } else {
           
//Output the value directly otherwise
//v4            echo ($value . "; \r\n");
           
$output .= $value . "; \r\n";
            }
        }
    }
return
$output;
}
?>
You can use this for printing $_GET array, for example
up
-1
kroczu at interia dot pl
7 years ago
<?
//A little function to convert array to simle xml:

function array_xml($array, $num_prefix = "num_")
{
    if(!is_array($array)) // text
    {
        return $array;
    }
    else
    {
        foreach($array as $key=>$val) // subnode
        {
            $key = (is_numeric($key)? $num_prefix.$key : $key);
            $return.="<".$key.">".array_xml($val, $num_prefix)."</".$key.">";
        }
    }

    return $return;
}

//example:

$array[0][0]                    = 1;
$array[0]['test']               = "test";
$array['test1']['test2']        = "test";
$array['test'][0]               = "test";
$array['test'][1]['test_x']     = $array;

print_r($array);

print"<xml>";

print array_xml($array);

print"</xml>";

/*

print_r($array) previev:

Array
(
    [0] => Array
        (
            [0] => 1
            [test] => test
        )
    [test1] => Array
        (
            [test2] => test
        )
    [test] => Array
        (
            [0] => test
            [1] => Array
                (
                    [test_x] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 1
                                    [test] => test
                                )
                            [test1] => Array
                                (
                                    [test2] => test
                                )
                            [test] => Array
                                (
                                    [0] => test
                                )
                        )
                )
        )
)

result xml in firefox preview:

-<xml>
    -     <num_0>
            <num_0>1</num_0>
            <test>test</test>
        </num_0>
    -    <test1>
            <test2>test</test2>
        </test1>
    -    <test>
            <num_0>test</num_0>
        -    <num_1>
            -    <test_x>
                -    <num_0>
                        <num_0>1</num_0>
                        <test>test</test>
                    </num_0>
                -    <test1>
                        <test2>test</test2>
                    </test1>
                -    <test>
                        <num_0>test</num_0>
                    </test>
                </test_x>
            </num_1>
        </test>
 </xml>

*/
?>
up
-1
elkabong at samsalisbury dot co dot uk
8 years ago
Hello all! I've just been working on a system to automatically manage virtualhosts on an Apache box and I needed to duplicate some multidimensional arrays containing references to other multidimensional array some of which also contained references. These big arrays are defaults which need to be overwritten on a per-virtualhost basis, so copying references into the virtualhost arrays was not an option (as the defults would get corrupted).

After hours of banging me head on the wall, this is what I've come up with:
<?PHP # Tested on PHP Version 5.0.4
# Recursively set $copy[$x] to the actual values of $array[$x]
function array_deep_copy (&$array, &$copy) {
    if(!
is_array($copy)) $copy = array();
    foreach(
$array as $k => $v) {
        if(
is_array($v)) {
           
array_deep_copy($v,$copy[$k]);
        } else {
           
$copy[$k] = $v;
        }
    }
}
# To call it do this:
$my_lovely_reference_free_array = array();
array_deep_copy($my_array_full_of_references, $my_lovely_reference_free_array);
# Now you can modify all of $my_lovely_reference_free_array without
# worrying about $my_array_full_of_references!
?>
NOTE: Don't use this on self-referencing arrays! I haven't tried it yet but I'm guessing an infinate loop will occur...

I hope someone finds this useful, I'm only a beginner so if there's any fatal flaws or improvements please let me know!
up
-1
alessandronunes at gmail dot com
8 years ago
The Ninmja sugestion plus multidiomensional array search (recursive):

function cleanArray($array) {
   foreach ($array as $index => $value) {
        if(is_array($array[$index])) $array[$index] = cleanArray($array[$index]);
        if (empty($value)) unset($array[$index]);
   }
   return $array;
}
up
-1
michael at imreo com
8 years ago
Reply to array_cartesian_product of skopek at mediatac dot com, 13-Oct-2004 12:44:

Your function does not work in my configuration (WinXP, apache 2.0, php 4.3.11).
This part of code:
...
} else { //if next returns false, then reset and go on with previuos array...
  reset($arrays[$j]);
}
...
cause infinite loop.
Replacing to
...
} elseif (isset($arrays[$j])) {
  reset($arrays[$j]);
}
...
works good.

My complete working function (additionaly strings as keys are allowed):
<?php
function array_cartesian_product($arrays)
{
   
$result = array();
   
$arrays = array_values($arrays);
   
$sizeIn = sizeof($arrays);
   
$size = $sizeIn > 0 ? 1 : 0;
    foreach (
$arrays as $array)
       
$size = $size * sizeof($array);
    for (
$i = 0; $i < $size; $i ++)
    {
       
$result[$i] = array();
        for (
$j = 0; $j < $sizeIn; $j ++)
           
array_push($result[$i], current($arrays[$j]));
        for (
$j = ($sizeIn -1); $j >= 0; $j --)
        {
            if (
next($arrays[$j]))
                break;
            elseif (isset (
$arrays[$j]))
               
reset($arrays[$j]);
        }
    }
    return
$result;
}
?>
up
-1
callmeanaguma at gmail dot com
1 year ago
If you need to flattern two-dismensional array with single values assoc subarrays, you could use this function:

<?php
function arrayFlatten($array) {
       
$flattern = array();
        foreach (
$array as $key => $value){
           
$new_key = array_keys($value);
           
$flattern[] = $value[$new_key[0]];
        }
        return
$flattern;
}
?>
up
-1
oliverSPAMMENOT at e-geek dot com dot au
4 years ago
Function to pretty print arrays and objects. Detects object recursion and allows setting a maximum depth. Based on arraytostring and u_print_r from the print_r function notes. Should be called like so:

<?php
egvaluetostring
($value)   //no max depth, or
egvaluetostring($value, $max_depth)   //max depth set

function egvaluetostring($value, $max_depth, $key = NULL, $depth = 0, $refChain = array()) {
  if(
$depth > 0)
   
$tab = str_repeat("\t", $depth);
 
$text .= $tab . ($key !== NULL ? $key . " => " : "");
 
  if (
is_array($value) || is_object($value)) {
   
$recursion = FALSE;
    if (
is_object($value)) {
      foreach (
$refChain as $refVal) {
        if (
$refVal === $value) {
         
$recursion = TRUE;
          break;
        }
      }
     
array_push($refChain, $value);
    }
   
   
$text .= (is_array($value) ? "array" : "object") . " ( ";
   
    if (
$recursion) {
     
$text .= "*RECURSION* ";
    }
    elseif (isset(
$max_depth) && $depth >= $max_depth) {
     
$text .= "*MAX DEPTH REACHED* ";
    }
    else {
      if (!empty(
$value)) {
       
$text .= "\n";
        foreach (
$value as $child_key => $child_value) {
         
$text .= egvaluetostring($child_value, $max_depth, (is_array($value) ? "[" : "") . $child_key . (is_array($value) ? "]" : ""), $depth+1, $refChain) . ",\n";
        }
       
$text .= "\n" . $tab;
      }
    }
   
   
$text .= ")";
   
    if (
is_object($value)) {
     
array_pop($refChain);
    }
  }
  else {
   
$text .= "$value";
  }

  return
$text;
}
?>
up
-2
kolkabes at googlemail dot com
1 year ago
Short function for making a recursive array copy while cloning objects on the way.

<?php
function arrayCopy( array $array ) {
       
$result = array();
        foreach(
$array as $key => $val ) {
            if(
is_array( $val ) ) {
               
$result[$key] = arrayCopy( $val );
            } elseif (
is_object( $val ) ) {
               
$result[$key] = clone $val;
            } else {
               
$result[$key] = $val;
            }
        }
        return
$result;
}
?>
up
-2
florian at egliselasauzaie dot fr
6 years ago
I've created a class which is comparable to a Vector in C++;
<?php
class Vector {
    public
$nb_elements;
    public
$liste;

    public function
Vector() {
       
$this->liste = array ();
       
$this->nb_elements = count($this->liste);
    }

    public function
push_back($valeur) {
        if (!empty (
$valeur)) {
           
$this->liste[] = $valeur;
           
$this->nb_elements = count($this->liste);
            return
true;
        } else {
            return
false;
        }
    }
    public function
insert_at($indice, $valeur) {
        if (
$indice > 0 && $indice < $this->nb_elements) {
            for (
$i = $this->nb_elements; $i != $indice; $i--) {
               
$this->liste[$i] = $this->liste[$i -1];
            }
           
$this->liste[$indice] = $valeur;
           
$this->nb_elements = count($this->liste);
            return
true;
        } else {
            return
false;
        }
    }
    public function
remove_at($indice) {
        if (
$indice > 0 && $indice < $this->nb_elements) {
            for (
$i = $indice; $i != $this->nb_elements - 1; $i++) {
               
$this->liste[$i] = $this->liste[$i +1];
            }
           
array_pop($this->liste);
           
$this->nb_elements = count($this->liste);
            return
true;
        } else {
            return
false;
        }
    }
    public function
pop() {
       
$value = array_pop($this->liste);
       
$this->nb_elements = count($this->liste);
        return
$value;
    }
    public function
shift() {
       
$value = array_shift($this->liste);
       
$this->nb_elements = count($this->liste);
        return
true;
    }
}
?>
up
-2
Jck_true (leave out the &#39;_&#39; at gmail dot com)
6 years ago
A usefull function that returns a flat array.
I use it in a template system. Let the user pass a multidimensional array. Convert it using my function. Then use
<?php
$array
= flatten($array,'','{$','}','->');
echo
str_replace(array_keys($array),array_values($array),$template)
/**
* Flattens out an multidimension array
* Using the last parameters you can define the new key based on the old path.
* @param array $array A multidimension array
* @param string $prefix Internal perfix parameter - leave empty.
* @param string $start_string What string should start the final array key?
* @param string $end_string What string should end the final array key?
* @param string $seperator The string that should seperate the piecies in final array key path
* @return array Returns the flat array
*/
function flatten($array, $start_string= '{$',$end_string= '}',$seperator='->',$prefix="") {
 
$return = array();
  foreach(
$array as $key=>$value) {
    if (
is_array($value)) {
     
$return = array_merge($return, Parser_method_replace::flatten($value, $prefix.$key.$seperator,$start_string,$end_string,$seperator));
    } else
     
$return [$start_string.$prefix.$key.$end_string] = $value;
  }
  return
$return;
}
}
?>
Example:
$template = 'My string with replacement {$test->subkey}';
{$test->subkey} will get replaced with $array['test']['subkey']
up
-2
dave at davidhbrown dot us
2 years ago
While PHP has well over three-score array functions, array_rotate is strangely missing as of PHP 5.3. Searching online offered several solutions, but the ones I found have defects such as inefficiently looping through the array or ignoring keys.

The following array_rotate() function uses array_merge and array_shift to reliably rotate an array forwards or backwards, preserving keys. If you know you can trust your $array to be an array and $shift to be between 0 and the length of your array, you can skip the function definition and use just the return expression in your code.

<?php
function array_rotate($array, $shift) {
    if(!
is_array($array) || !is_numeric($shift)) {
        if(!
is_array($array)) error_log(__FUNCTION__.' expects first argument to be array; '.gettype($array).' received.');
        if(!
is_numeric($shift)) error_log(__FUNCTION__.' expects second argument to be numeric; '.gettype($shift)." `$shift` received.");
        return
$array;
    }
   
$shift %= count($array); //we won't try to shift more than one array length
   
if($shift < 0) $shift += count($array);//handle negative shifts as positive
   
return array_merge(array_slice($array, $shift, NULL, true), array_slice($array, 0, $shift, true));
}
?>
A few simple tests:
<?php
$array
=array("foo"=>1,"bar"=>2,"baz"=>3,4,5);

print_r(array_rotate($array, 2));
print_r(array_rotate($array, -2));
print_r(array_rotate($array, count($array)));
print_r(array_rotate($array, "4"));
print_r(array_rotate($array, -9));
?>
up
-2
really quick function
6 years ago
function array_flatten($a){ //flattens multi-dim arrays (distroys keys)
    $ab = array(); if(!is_array($a)) return $ab;
    foreach($a as $value){
        if(is_array($value)){
            $ab = array_merge($ab,array_flatten($value));
        }else{
            array_push($ab,$value);
        }
    }
    return $ab;
}
up
-2
dieter peeters
8 years ago
in response to: Domenic Denicola

I reworked your function a bit and thought i just as well could post it.

Below is the cleaner version, just cut and paste ;) The third parameter is of little use to the coder, unless javascript declaration of variables changes at some point in the future - who knows.

Only minor point is the added parameter which probably gets copied every recursive call with an empty value, though i don't know the exact ways how php handles recursion internally. Most of the time php is pretty smart when optimizing code and an empty string shouldn't take much memory anyway :)

<?php
function phpArrayToJsArray($name,$array,$prePend='var ')
{
    if (
is_array($array)) { // Array recursion
       
$result = $name.' = new Array();'."\n";
        foreach (
$array as $key => $value) {
          
$result .= phpArrayToJsArray($name.'["'.$key.'"]',$value,'');
        }
    } else { 
// Base case of recursion
       
$result = $name.' = "'.$array.'";'."\n";
    }
    return
$prePend.$result;
}
?>
up
-3
cyberchrist at futura dot net
6 years ago
Lately, dealing with databases, I've been finding myself needing to know if one array, $a, is a proper subset of $b.

Mathematically, this is asking (in set theory) [excuse the use of u and n instead of proper Unicode):
 
( A u B ) n ( ~ B )

What this does is it first limits to known values, then looks for anything outside of B but in the union of A and B (which would be those things in A which are not also in B).

If any value exists in this set, then A is NOT a proper subset of B, because a value exists in A but not in B.  For A to be a proper subset, all values in A must be in B.

I'm sure this could easily be done any number of ways but this seems to work for me.  It's not got a lot of error detection such as sterilizing inputs or checking input types.

// bool array_subset( array, array )
// Returns true if $a is a proper subset of $b, returns false otherwise.

function array_subset( $a, $b )
{
    if( count( array_diff( array_merge($a,$b), $b)) == 0 )
        return true;
    else
        return false;
}
up
-2
seva dot lapsha at gmail dot com
4 years ago
Arrays are good, but inapplicable when dealing with huge amounts of data.

I'm working on rewriting some array functions to operate with plain Iterators - map, reduce, walk, flip et cetera are already there.

In addition I'm going to implement simulation of comprehensions (generators) in PHP (http://en.wikipedia.org/wiki/List_comprehension).

See the source code, examples and documentation at http://code.google.com/p/php-iterator-utils/
To Top