0

I have one array, for example


$test_input= array(
  0=> array(
    "task" => 'ColumnTree Example',
    "duration" => '3 hours',
    "user"=> '',
    "uiProvider" => 'col',
    "cls" => 'master-task',
    "iconCls" => 'task-folder',
    'children'=> array(
      0 => array(
        "task" => 'Abstract rendering in TreeNodeUI',
        "duration" => '15 min',
        "user" => 'Jack Slocum',
        "uiProvider" => 'col',
        "leaf" => true,
        "iconCls" => 'task'
      ),
      array(
        "task" => 'Test and make sure it works',
        "duration" => '1 hour',
        "user" => 'Jack Slocum',
        "uiProvider" => 'col',
        "leaf" =>true,
        "iconCls" =>'task'
      )
    )
  ),
  array(
    "task" => 'Custom Field Example',
    "duration" => '2 1/2 hours',
    "user" => '',
    "uiProvider" => 'col',
    "cls" => 'master-task',
    "iconCls" => 'task-folder',
    'children'=>array(
      0 => array(
        "task" =>'Implement "Live Search" on extjs.com from Alex',
        "duration" =>'1 hour',
        "user" =>'Jack Slocum',
        "uiProvider" =>'col',
        "leaf" =>true,
        "iconCls"=> 'task'
      ),
      array(
        "task"=>'Extend TwinTrigger',
        "duration"=>'30 min',
        "user"=>'Jack Slocum',
        "uiProvider"=>'col',
        "leaf"=>true,
        "iconCls"=>'task'
      )
    )
  ),
  array(
    "task"=>'Foo Bar Item',
    "duration"=>'3 hours',
    "user"=>'',
    "uiProvider"=>'col',
    "cls"=>'master-task',
    "iconCls"=>'task-folder',
    'children'=>array(
      0 => array(
        "task"=>'Abstract rendering in TreeNodeUI',
        "duration"=>'15 min',
        "user"=>'Jack Slocum',
        "uiProvider"=>'col',
        "leaf"=>true,
        "iconCls"=>'task'
      ),
      array( 
        "task"=>'Create TreeNodeUI with column knowledge',
        "duration"=>'45 min',
        "user"=>'Jack Slocum',
        "uiProvider"=>'col',
        "leaf"=>true,
        "iconCls"=>'task'
      )
    )
  )
);

I want to do some stuff. By checking condition

if($key=="task")
 {
   something/.........
 }

So here instead of statically giving key value as 'task'. I need that should come dynamically. I am using array_map for mapping my function and my array. Please give me some examples on how to do this.

4

3 Answers 3

0

Could you use a foreach statement? http://php.net/manual/en/control-structures.foreach.php

0

you can use foreach for this. Whether you use foreach for this task but you have to check this as you are doing.

Because by using foreach you can get key and value pair but to check something you have to put condition.

0

From your data structure, it looks like, you need to do recursion. Try this code:

function walk_array($array, &$returnData, $depth = 0)
{
    $depth++;
    foreach( $array as $key=>$value)
    {
        $returnData[] = $value['task'];

        if( isset( $value['children'] ) && is_array( $value['children'] ) )
        {
            walk_array($value['children'], &$returnData, $depth);
        }
    }
}

Test it like this:

$returnData = array();
walk_array($test_input, &$returnData);
echo implode("<br />", $returnData);

One thing to note regarding recursion is, be careful when designing the code. One tiny mistake could end up looping infinitely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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