0

how would you turn this array:

Array
(
    [0] => 234234234
    [1] => 657567567
    [2] => 234234234
    [3] => 5674332
)

into this:

Array
(
    [contacts] => Array(
            [0] => Array
                (
                            [number] => 234234234
                            [contact_status] => 2
                            [user_id] =>3 

                        )
            [1] => Array
                (
                            [number] => 657567567
                            [contact_status] => 2
                            [user_id] =>3
                        )
            [3] => Array
                (
                            [number] => 234234234
                            [contact_status] => 2
                            [user_id] =>3
                        )
            [4] => Array
                (
                            [number] => 5674332
                            [contact_status] => 2
                            [user_id] =>3
                        )
                )
)

is there a cakephp specific way how to transform this array?

thank you

1
  • Is the gap in the array’s index intended? Commented Nov 15, 2009 at 12:22

5 Answers 5

5

nicer

$contact_status = 2;
$user_id = 1;
foreach($input as $number)
    $output['contacts'][] = compact('number', 'contact_status', 'user_id');
Sign up to request clarification or add additional context in comments.

Comments

4

Try this:

$output = array('contacts'=>array());
foreach ($input as $val) {
    $output['contacts'][] = array(
        'number'         => $val,
        'contact_status' => 2,
        'user_id'        => 3
    );
}

I assume that contact_status and user_id are static since you didn’t tell anything else.

Comments

2
$input = array(...);
$arr = array();
foreach ($input as $id) {
  $arr[] = array(
    'number' => $id,
    'contact_status' => 2,
    'userid' => 3;
  );
}
$output = array('contacts' => $arr);

1 Comment

It’s contacts and not contents ;-)
0

A little bit of cleanup from sterofrog's solution. Declare the array and use array_push instead of assigning it to an empty index.

$output = array( );
$contact_stats = 2;
$user_id = 3;
foreach( $input as $number ) {
    array_push( $output[ 'contact' ], compact(
        'number',
        'contact_status',
        'user_id'
    ));
}

1 Comment

it uses array_push because ( someone correct me if I am wrong ) it is faster than assigning values to an empty index. Also, I think using array_push with compact is easier to read than the previous version. Declaring $output as an array ahead of time is simply good coding practice. It prevents php warnings and clearly indicates that output is an array.
0

You can simply use the array_map function like this:

$result = array_map(function ($n){
    return array(
        'number' => $n,
        'contact_status' => 2,
        'user_id' => 3);
}, $original);

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.