skip to Main Content

I have first array:

Array
(
    [0] => generala value 1
    [1] => specificatii value 1
)

and second array is:

   Array
(
    [0] => Array
        (
            [title] => generala title 1
            [atribute_cat_id] => 1
            [product_id] => 98
        )

    [1] => Array
        (
            [title] => specificatii title 1
            [atribute_cat_id] => 2
            [product_id] => 98
        )    
)

I want to get this array form:

Array
(
    [0] => Array
        (
            [title] => generala title 1
            [atribute_cat_id] => 1
            [product_id] => 98
            [value] => generala value 1
        )

    [1] => Array
        (
            [title] => specificatii title 1
            [atribute_cat_id] => 2
            [product_id] => 98
            [value] => specificatii value 1
        )

)

I tried with array_merge but this method put all elements from first array to each element from second array!
Every time both arrays have same number of elements!
Any ideea?
Thank you!

4

Answers


  1. Like below you can do

    $firstArray = array(
        'generala' => 'generala value 1',
        'specificatii' => 'specificatii value 1',
    );
    
    $secondArray = array(
        array(
            'title' => 'generala title 1',
            'atribute_cat_id' => 1,
            'product_id' => 98,
        ),
        array(
            'title' => 'specificatii title 1',
            'atribute_cat_id' => 2,
            'product_id' => 98,
        ),
    );
    
    $resultArray = array();
    
    foreach ($secondArray as $item) {
        $resultItem = $item;
        foreach ($firstArray as $key => $value) {
            if (is_string($key) && strpos($item['title'], $key) !== false) {
                $resultItem['value'] = $value;
                break;
            }
        }
        $resultArray[] = $resultItem;
    }
    
    echo '<pre>';
    print_r($resultArray);
    
    Login or Signup to reply.
  2. A simple iterative loop is probably the easiest to follow. However, if you’re looking for something more compact then this might suit:

    $result = $secondArray;
    array_walk(
        $result,
        fn(&$v, $k) => $v['value'] = $firstArray[$k]
    );
    
    Login or Signup to reply.
  3. Loop over one of the arrays, using the indexes to access the corresponding element in the other array.

    foreach ($first_array AS $i => $value) {
        $second_array[$i]['value'] = $value;
    }
    
    Login or Signup to reply.
  4. You can do this using a foreach loop. Keep it simple. 🙂

    $firstArray = [
        'generala value 1',
        'specificatii value 1',
    ];
    
    $secondArray = [
        [
            'title' => 'generala title 1',
            'atribute_cat_id' => 1,
            'product_id' => 98,
        ],
        [
            'title' => 'specificatii title 1',
            'atribute_cat_id' => 2,
            'product_id' => 98,
        ],
    ];
    
    // Create a copy
    $result_array = array_values($secondArray);
    foreach ($firstArray as $idx => $value) {
        $result_array[$idx]['value'] = $value;
    }
    
    

    If you prefer the destructive approach:

    // Modify the array IN-PLACE
    foreach ($firstArray as $idx => $value) {
        $secondArray[$idx]['value'] = $value;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search