Loop multi-dimensional PHP array? I have multi-dimensional array like this:

Array(
  0=>Array(0=>aaaaaa,
    1=>bbbbb,
    2=>ccccc), 
  1=>Array(0=>aa2ssa,
    1=>bb3242bb,
    2=>ccccc234),
  2=>Array(0=>aaa234aa,
    1=>b3242b,
    2=>cewrcc),
....
)

function new_array(index,sub__index){
  //TODO
}

If I call new_array(array(0,2),(array(1,2),array(0,2))) I will get the result:

 Array(
  0=>Array(
    1=>bbbbb,
    2=>ccccc), 
  2=>Array(0=>aaa234aa,
    2=>cewrcc)
)

and if u call this new_array(array(2),(array(0,2)))

Array(
      2=>Array(
        0=>aaa234aa,
        1=>b3242b)
    )

Anybody know how to do this? Please help

thanks

link|improve this question

Are you looking to populate the items passed by the function with just random data? – Brad Christie Dec 15 '10 at 20:51
need new_array that depend param passed. – QueueOverflow Dec 15 '10 at 21:14
wheres the data coming from? The Clearer your question, the more precise your answers... – Jakub Dec 15 '10 at 21:16
feedback

3 Answers

up vote 2 down vote accepted

An alternate solution to @Orbling's is this:

function populateData() // CHANGE ME to populate the info how you please
{
  $seed = 'abcdefghijklmnopqrstuvwxyz0123456789';
  $_ = ''; $length = rand(5,8);
  for ($__ = 0; $__ < $length; $__++) $_ .= substr($seed,rand(0,strlen($seed)),1);
  return $_;
}

function new_array()
{
  $args = func_num_args();
  if ($args == 0)
    return FALSE; // flag error if no arguments are passed

  $template = func_get_arg(0);
  if ($template == null || !is_array($template) || $args < (count($template)+1))
    return FALSE; // flag error if we don't have enough information

  $resultAry = Array();
  $arg = 1;
  foreach ($template as $t)
  {
    $resultArySub = Array();

    $templateSub = func_get_arg($arg++);
    if ($templateSub == FALSE || !is_array($templateSub)) 
      return FALSE; // error checking for valid input

    foreach ($templateSub as $tS)
      $resultArySub[$tS] = populateData();

    $resultAry[$t] = $resultArySub;
  }
  return $resultAry;
}

header('Content-Type: text/plain');
echo "your request (or so i understood):\r\n";
$test = new_array(array(0,2),array(1,2),array(0,2));
var_dump($test);

echo "\r\nextra array on end is ignored:\r\n";
$test = new_array(array(4,2),array(1,2),array(0,2),array(3,5));
var_dump($test);

echo "\r\nno data is a FALSE:\r\n";
$test = new_array();
var_dump($test);

echo "\r\ntoo few arguments for what was supplied in first argument is a FALSE:\r\n";
$test = new_array(array(1,2,3),array(4,5),array(6,7));
var_dump($test);

echo "\r\nas long as there's as \"array argument\" for every element of the \"first argument\", this will work:\r\n";
$test = new_array(array(1,2,3,4,5,6,7),array(1),array(2),array(3),array(4),array(5),array(6),array(7));
var_dump($test);

echo "\r\nall arguments must be an array\r\n";
$test = new_array(array(1,2),'not','arrays');
var_dump($test);

Results in an array with random entries. The outcome of the above would be:

your request (or so i understood):
array(2) {
  [0]=>
  array(2) {
    [1]=>
    string(8) "mjdfsmda"
    [2]=>
    string(8) "qg2bzsj6"
  }
  [2]=>
  array(2) {
    [0]=>
    string(7) "345plm8"
    [2]=>
    string(7) "1exlla6"
  }
}

extra array on end is ignored:
array(2) {
  [4]=>
  array(2) {
    [1]=>
    string(5) "0ngei"
    [2]=>
    string(5) "q6tmg"
  }
  [2]=>
  array(2) {
    [0]=>
    string(7) "4enz61q"
    [2]=>
    string(6) "6bojtn"
  }
}

no data is a FALSE:
bool(false)

too few arguments for what was supplied in first argument is a FALSE:
bool(false)

as long as there's as "array argument" for every element of the "first argument", this will work:
array(7) {
  [1]=>
  array(1) {
    [1]=>
    string(7) "ndulmi9"
  }
  [2]=>
  array(1) {
    [2]=>
    string(7) "jip402j"
  }
  [3]=>
  array(1) {
    [3]=>
    string(5) "3bn0d"
  }
  [4]=>
  array(1) {
    [4]=>
    string(8) "b80le1jh"
  }
  [5]=>
  array(1) {
    [5]=>
    string(5) "x31sw"
  }
  [6]=>
  array(1) {
    [6]=>
    string(8) "x8e3dge7"
  }
  [7]=>
  array(1) {
    [7]=>
    string(8) "vcpf997y"
  }
}

all arguments must be an array
bool(false)
link|improve this answer
It remains to be seen where the data is to be sourced, your source is as sensible as mine. – Orbling Dec 15 '10 at 21:26
1  
It was a shot in the dark at best. oh well, bit of a language barrier and lack of details. Gave it my best uninformed attempt. – Brad Christie Dec 15 '10 at 21:32
Well he gave you the accept, so I guess it was random data, lol. ;-) – Orbling Dec 15 '10 at 21:36
1  
@orbling: Heh, and here I thought you had it. I made the comment back to you and saw you got the acceptance. I revisit, and I have it. My apologies for stealing your thunder. I promise I will honor the torch. ;-) – Brad Christie Dec 16 '10 at 16:10
LOL, I don't know what happened or which of us was right. Thank you for honouring my memory. ;-) – Orbling Dec 16 '10 at 16:12
feedback

Assuming that you wish the function to process an existing array and filter out the data, as seems to be the case then you could do this:

function new_array($original, $topKeys, $subKeys) {
    if (count($topKeys) != count($subKeys)) {
        return $original;
    }

    $newArray = array();
    foreach ($topKeys as $cTopKey) {
        $cSubKeys = array_shift($subKeys);

        if (array_key_exists($cTopKey, $original)) {
            $newArray[$cTopKey] = array();
            foreach ($cSubKeys as $cSubKey) {
                if (array_key_exists($cSubKey, $original[$cTopKey])) {
                    $newArray[$cTopKey][$cSubKey] = $original[$cTopKey][$cSubKey];
                }
            }
        }
    }

    return $newArray;
}

If you have PHP v5.1+ and the indexes are guaranteed to be available, and in order, then I believe you can do it more simply:

function new_array($original, $topKeys, $subKeys) {
    $newArray = array_intersect_key($original, array_flip($topKeys));
    foreach ($newArray as $cKey => $cSub) {
        $cSubKeys = array_shift($subKeys);
        $newArray[$cKey] = array_intersect_key($cSub, $cSubKeys);
    }

    return $newArray;
}

The danger in that is I do not know if array_intersect_key() is defined to keep the original ordering of elements. If not, then further code would need to be added to match the sub keys with the original, ideally the subkeys would be sub arrays of the first parameter anyhow.

link|improve this answer
Thank for your answer and understand my question. – QueueOverflow Dec 15 '10 at 21:31
feedback

why not just

$a = array('0' => 
               array('1' => 'bbbb', 
                     '2' => 'ccccc'), 
           '2' => 
               array('0' => 'aaaa', 
                     '2' => 'cewrcc')
           );

Why use a function to do the same?

link|improve this answer
need new_array that depend param passed. – QueueOverflow Dec 15 '10 at 21:14
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.