2

Edit:

Trying to avoid doing a loop outside of the $data array you see. As I need to do this a couple of times and it looks messy.


I have got an array similar to this:

$links = [
    [
        'type_id' => '1',
        'url' => ''
    ],
    [
        'type_id' => '2',
        'url' => ''
    ]
];
$types = [
    [
        'id' => 1,
        'value' => 'facebook'
    ],
    [
        'id' => 2
        'value' => 'twitter'
    ]
];
$data = [
    'primary' => [
        'address_details' => [],
        'contact_details' => [],
        'social_links' => $links
    ]
];

I need the keys within my $data['primary']['social_links'] to use the $type['value'] rather than just being 0, 1, etc...

So $data would look like:

$data = [
    'primary' => [
        'address_details' => [],
        'contact_details' => [],
        'social_links' => [
            'facebook' => [
                'type_id' => '1',
                'url' => ''
            ],
            'twitter' => [
                'type_id' => '2',
                'url' => ''
            ]
        ]
    ]
];

Is there a way I can do this with array_map or something?

4 Answers 4

3

You can just use array_combine with the output of the value column of $types (generated using array_column) as keys and the values from $links:

$data = [
    'primary' => [
        'address_details' => [],
        'contact_details' => [],
        'social_links' => array_combine(array_column($types, 'value'), $links)
    ]
];
print_r($data);

Output:

Array ( 
    [primary] => Array (
        [address_details] => Array ( )
        [contact_details] => Array ( )
        [social_links] => Array (
            [facebook] => Array (
                [type_id] => 1
                [url] =>
            )
            [twitter] => Array (
                [type_id] => 2
                [url] =>
            )
        )
    ) 
)

Demo on 3v4l.org

Update

Based on the edit to OPs question, things get a lot more complicated to give a one-line solution. This should work:

$data = [
    'primary' => [
        'address_details' => [],
        'contact_details' => [],
        'social_links' => array_map(function ($v) use ($links) { return $links[array_search($v, array_column($links, 'type_id'))]; }, array_column($types, 'id', 'value'))
    ]
];

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

3 Comments

My mistake on my example, but the $types['value'] would be the key, but I select the $type using it's id, updated my example. Can your code above be adapted to work still?
@MartynBall so the id value from $types is supposed to match the type_id value from $links?
@MartynBall I've added an update to my answer. It's a bit messy but is still a one-liner...
3

A simple for loop can do it:

https://3v4l.org/h47MG

<?php

$links = [
    [
        'type_id' => '1',
        'url' => ''
    ],
    [
        'type_id' => '2',
        'url' => ''
    ]
];
$types = [
    [
        'value' => 'facebook'
    ],
    [
        'value' => 'twitter'
    ]
];
$result = [];
for($i = 0, $len = count($types); $i < $len; $i++) {
    $result[$types[$i]['value']] = $links[$i];
}
$data = [
    'primary' => [
        'address_details' => [],
        'contact_details' => [],
        'social_links' => $result
    ]
];

var_dump($data);

3 Comments

I was trying to avoid doing a loop outside of the $data array you see. As I need to do this a couple of times and it looks messy.
@AdamWhateverson I don't understand what you mean. It creates a new $result array and adds a new key(value from $types) => value(entry from $links) to the $result array. $result[] = .. would add them without key (i.e. zero, one... indexed) which is not what OP wrote in his example output
@Xatenev Hello, ignore me, I read the code wrong, it's fine :) Weird how you get down voted here just for trying to have a chat. Have a nice day.
3

You could use a loop to modify the array directly :

for($i=0; $i < sizeof($links); $i++) {
    $links[$types[$i]['value']] = $links[$i];
    unset($links[$i]);
}

var_dump($links);

Output :

["facebook"]=> array(2) { 
    ["type_id"]=> string(1) "1" 
    ["url"]=> string(0) "" 
} 
["twitter"]=> array(2) { 
    ["type_id"]=> string(1) "2" 
    ["url"]=> string(0) "" 
}

Or by using array_combine if you do not want a loop, per your comment on another answer :

array_combine(array_column($types, 'value'), $links)

Comments

1
<pre><code>
$social_links = [];
foreach ($types as $type):
    $social_links[$type['value']] = $links[$key];
endforeach;

$data = [
    'primary' => [
        'address_details' => [],
        'contact_details' => [],
        'social_links' => $social_links
    ]
];

print_r($data);
</code></pre>

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.