0

this may seem a rather trivial question, please excuse my ignorance. Still getting the hang of array manipulation...

I have a CakePHP app that is posting an array to my controller to be saved. I need to somehow reformat the sent array so that it may be processed properly by Cake's Save behaviour.

The array posted is:

Array ( 
        [788] => Array ( [id] => 788 )  
        [787] => Array ( [id] => 787 )  
        [786] => Array ( [id] => 0 )  
        [785] => Array ( [id] => 0 )  

        [value_1] => 0 

        [analysed_date] => Array ( 
           [month] => 08 
           [day] => 16 
           [year] => 2011 
        )  

        [job_id] => 34 
) 

Desired Array:

Array ( 
    [0] => Array ( 
        [id] => 788 
        [value_1] => 0 
        [analysed_date] => Array ( 
            [month] => 08 
            [day] => 16 
            [year] => 2011 
        )  
    )  
    [1] => Array ( 
        [id] => 787 
        [value_1] => 0 
        [analysed_date] => Array ( 
            [month] => 08 
            [day] => 16 
            [year] => 2011 
        )  
    )  
)

Thanks for taking the time to look.


EDIT:

I've just realised I omitted the fact that if the array has an [id] => 0 that it needs to be ignored. This was my primary stumbling block. Apologies. I hope the edit clarifies my problem better.

SOLVED

Thank you for your help guys. I was able to come up with the solution by myself. Here is what I came up with.

foreach($org_array as $key => $value){
    if(is_array($value)){
        if(isset($value['id'])){
            if($value['id'] != 0) {
                $data[$i] = array(
                    'id' => $value['id'],
                    'value_1'=> $value_1,
                    'analysed_date' => $date
                );
                $i++;
            }
        }   
    }
}   

2 Answers 2

1

Something like this should work, but just for your example:

$array_keys = array_keys($org_array);
$new_array = array();
foreach ($array_keys as $key)
{
  if (is_int($key))
  {
    $new_array[] = array(
        "id" => $key,
        "value1" => $org_array["value1"],
        "analysed_date" => $org_array["analysed_date"]
      );
    // you might want to loop throught the original array to get all non-integer key values instead of hard-coding it
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1
$main = Array ( 
        [788] => Array ( [id] => 788 )  
        [787] => Array ( [id] => 787 )  
        [786] => Array ( [id] => 786 )  
        [785] => Array ( [id] => 785 )  

        [value_1] => 0 

        [analysed_date] => Array ( 
           [month] => 08 
           [day] => 16 
           [year] => 2011 
        )  

        [job_id] => 34 
) 

$analysed_date = $main['analysed_date'];
$value1 = $main['value_1'];

$result = array();
$i=0;
foreach($main as $key=>$value)
{
if( is_numeric($key)
 {
   $result[$i]=array();
   $result[$i]['id']=$key;
   $result[$i]['value_1']=$value1;
   $result[$i]['analysed_date']=$analysed_date;
   $i++;
 }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.