0
votes

It is known that array_combine is used only for two arrays. For example

$name = array('John','Brian','Raj');
$salary = array('500','1000','2000');
$details = array_combine($name, $salary);
foreach($details AS $name => $salary){
    echo $name."'s salary is ".$salary."<br/>";
}

Let add 2 arrays in this list

$dpart = array('HTML','CSS','PHP');
$address = array('Floor 3','Floor 5','Floor 6');

In that case, only array_combine() is not enough, so I found array_map() is the better solution here. But how to echo array_map() result? How to access values of array generated by array_map() and fetch according individual requirement.

$details = array_map(function($item) {
    return array_combine(['name', 'salary', 'dpart', 'address'], $item);
}, array_map(null, $name, $salary, $dpart, $address));

Now requirement is to access all four arrays with individual values. For example

$name."'s salary is ".$salary.", address is ".$address.", depart is ".$dpart

3

3 Answers

1
votes

You can access result simply using foreach. Loop $details as

foreach($details as $item) {
    $name    = $item['name'];
    $salary  = $item['salary'];
    $dpart   = $item['dpart'];
    $address = $item['address'];
    echo $name."'s salary is ".$salary.", address is ".$address.", depart is ".$dpart."<br/>";
}

Or without the assignment within the loop:

foreach($details as $item)
    print "{$item['name']}'s salary is {$item['name']}, address is {$item['dpart']}, depart is {$item['address']}<br/>";
1
votes

It may be just as simple to not combine these:

<?php
$name    = array('John','Brian','Raj');
$salary  = array('500','1000','2000');
$dpart   = array('HTML','CSS','PHP');
$address = array('Floor 3','Floor 5','Floor 6');

foreach($name as $k => $v)
    printf(
        "%s's salary is %d, address is %s, depart is %s\n",
        $name[$k],
        $salary[$k],
        $dpart[$k],
        $address[$k]
    );

Output:

John's salary is 500, address is HTML, depart is Floor 3
Brian's salary is 1000, address is CSS, depart is Floor 5
Raj's salary is 2000, address is PHP, depart is Floor 6

You can loop through your array_map, and use list, or the item indexes:

foreach(array_map(null, $name, $salary, $dpart, $address) as $item) {
    list($n, $s, $d, $a) = $item;
    print "$n's salary is $s, address is $d, depart is $a\n";
}
0
votes

PHP has a native function which allows you to craft a string with placeholders and pass in an array of data to replace those placeholders -- vprint() (or its silent sibling: vsprintf()).

After "transposing" the data from the four input arrays, collect the column of data using variadic syntax (...) and feed it to the printing function.

Code: (Demo)

print_r(
    array_map(
        fn(...$col) => vsprintf("%s's salary is %d, depart is %s, address is %s", $col),
        $name, $salary, $dpart, $address
    )
);

Output:

Array
(
    [0] => John's salary is 500, depart is HTML, address is Floor 3
    [1] => Brian's salary is 1000, depart is CSS, address is Floor 5
    [2] => Raj's salary is 2000, depart is PHP, address is Floor 6
)