0

What I'm doing is taking a basic array, with default numeric keys:

$basic_array = array("value1", "value2", "value3")

and I want to merge that array with an associative array set up like:

$associative_array = array(array("filename" => "value4"), array("filename" => "value5"), array("filename" => "value6"))

, so each numeric key in the associative array houses an extra array set.

What would be nice to do is something like:

$associative_array = array_merge(
    [combine $basic_array into temp_array set up with same 
        structure of $associative_array, and add $basic_array 
        values into temp_array]
    , $associative_array
);

It obviously would depend on what combination, if any, of array manipulation functions could copy the $associative_array structure on the fly, and fill the corresponding keys in each sub-array with values of the $basic_array, and then merge them.

I'm doing this because I want to de-dupe everything in the basic_array with the associative_array.

I'm currently doing this:

$manual_additions_filenames = array("vtlcvsp.pdf", "vtp.pdf", "vtpsai.pdf", "2990-2.pdf", "2990-8.pdf", "vtxbrl.zip", "vtfrisp.pdf", "vtp.pdf", "vtpsai.pdf", "1939-2.pdf", "cashcollateral.pdf", "cashreserves.pdf");

$associative_array_filenames = array(); //dummy one-dimensional array to store values in filename key of associative array

foreach ($associative_array as $key => $field_array)
{
    $associative_array_filenames[] = $field_array["filename"];
} //pull filename and put into one-dimensional array

$manual_additions = array_diff(
    array_unique($manual_additions_filenames), $associative_array_filenames
); //compare, to get list of unique filenames to be tacked on

foreach ($manual_additions as $value)
{
    $associative_array[]["filename"] = $value;
}

All the native php array functions are designed to eliminate all these foreach loops, or so I thought, so I'd love to clean this crap up.

hakre
  • 193,403
  • 52
  • 435
  • 836
Josh L
  • 39
  • 6
  • `foreach` is okay to use, there's nothing wrong with it. And actually it's the other way round: When the array functions were introduced, `foreach` did not exist. So then came `foreach` to make the adding of even more array functions not necessary any longer. – hakre May 04 '12 at 13:39
  • 2
    Additionally your question is very hard to understand which reduces the chance that somebody will actually show you some array function magic here. – hakre May 04 '12 at 13:39

1 Answers1

1

try this out :

$basic_array = array("value1", "value2", "value3");
$basic_array = array_unique($basic_array);
$associative_array = array_map(
    function($item) { 
        return ['file_name' => $item];
    }, 
    $basic_array);
pivox
  • 51
  • 3