1

Hi Guys Please help me in creating an array like below

array (
  array ( "5555776741" , "Don0454545" , "Draper" ),
  array ( "5551112239" , "betty777777" , "Smith" ),
  array ( "9999999999", "test", "name")
)

From an array $contacts which prints as below with print_r($contacts) as:

Array
(
[0] => Array
    (
        [phone_mobile] => +16046799329
        [first_name] => 
        [last_name] => test
    )

[1] => Array
    (
        [phone_mobile] => 7326751700
        [first_name] => Ralph
        [last_name] => OBrien
    )

[2] => Array
    (
        [phone_mobile] => 3204937568
        [first_name] => Chris
        [last_name] => Barth
    )

)

Im trying to achieve from the below code : but the value passed is not assigned from $contact to $record the Print_r($contact) prints empty arrays.

foreach ($contacts as $contact)
{
    $record =$contact;
}
4
  • $record is a flat variable you need the [] after it to "push" to it Commented Sep 20, 2017 at 15:20
  • Just $record[] = $contact; ? let me try that Commented Sep 20, 2017 at 15:23
  • That will keep the associative keys, which you dont want. You'll end up with the same thing Commented Sep 20, 2017 at 15:23
  • Yes You are right i dont want keys Commented Sep 20, 2017 at 15:29

4 Answers 4

4

You can do this, so you won't have to specify each array keys.

$data = [];
foreach ($contacts as $contact) {
    $data[] = array_values($contact);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry to bother you Im new Can you help me how to do that?
2

The following isn't tested, but should work for you.

$data = [];
foreach ($contacts as $contact)
{
    $data[] = [ $contact['phone_mobile'], $contact['first_name'], $contact['last_name'] ];
}

2 Comments

Here's the answer, this won't add the keys
Thanks I will try this
1

You could also use array_values() within a Loop for that like so:

<?php
    $result = [];
    $arr    = [
            [
            'phone_mobile'  => '+16046799329',
            'first_name'    => '',
            'last_name'     => 'test'
            ],
            [
            'phone_mobile'  => '7326751700',
            'first_name'    => 'Ralph',
            'last_name'     => 'OBrien'
            ],
            [
            'phone_mobile'  => '3204937568',
            'first_name'    => 'Chris',
            'last_name'     => 'Barth'
            ],
    ];

    foreach($arr as $i=>$data){
        $result[]   = array_values($data);
    }
    var_dump($result);

Comments

1

In addition to directly iterating, you can also get the result you want by mapping array_values over your contacts array.

$record = array_map('array_values', $contacts);

It's basically the same thing as the foreach answer from Sam, just a bit more condensed.

1 Comment

Thanks For that I really love you guys for helping me out Thanks All :)

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.