Combine arrays within array

Asked

Viewed 302 times

0

Can someone help me by doing me the favor.

I have an array in this style:

$arrayTable = array(
   0 => array(
      'idApiUm' => 123,
      'title'  => 'Teste'
   ),
   1 => array (
      'idApiDois' => 765,
      'title'  => 'Título'
   ),
   2 => array(
      'idApiUm' => 632,
      'title'  => 'Nome'
   ),
   3 => array(
      'idApiDois' => 999,
      'title'  => 'Teste'
   ),
);

And I need to merge all arrays inside it that have the same value in 'title', and leave the key this way:

0 => array(
   'idApiUm' => 123,
   'idApiDois' => 999,
   'title'   => 'Teste'
),

Is it possible? I can’t solve this problem...

2 answers

1

Try it like this...

<?php  $arrayTable = array(
    0 => array(
        'idApiUm' => 123,
        'title' => 'Teste'
    ),
    1 => array (
        'idApiDois' => 765,
        'title' => 'Título'
    ),
    2 => array(
        'idApiUm' => 632,
        'title' => 'Nome'
    ),
    3 => array(
        'idApiDois' => 999,
        'title' => 'Teste'
    ),
);

$result = array();

function test_print($item, $key)
{
    global $result;
    if(is_string($key))
        $result[$key] = $item;
    else
        $result[$key] += $item;
}

foreach ($arrayTable as $x)
    if($x['title'] == 'Teste')
       array_walk($x, 'test_print');
print_r($result);
?>

Array ( [idApiUm] => 123 [title] => Test [idApiDois] => 999 )

  • Carlos speaks, all right? First, thank you very much for the answer! So I got this result: Array ( [idApiUm] => 632 [title] => Test [idApiDois] => 999 ) However, note that the Apium ID is not showing the correct one, which should be 123...

  • I MODIFIED THE CODE to arrive at the result you are looking for! Include below foreach a condition "if($x['title'] == 'Test')"

  • In this case it works perfectly, but I would like this condition to be achieved dynamically, and not just in the case of the title "Test"...

  • In your case it is very specific! If only to recover the last value of each key would be simpler... But in the above case you want to recover the title value 'Test', so you need the validation of this key, otherwise I will hardly have precision in the result.

  • Yes, it’s very complicated! But I think that Virgilio, from the other answer, managed to solve it! Anyway, thank you very much for helping Carlos!!!!

1


First search the repeating keys that in the case is title, then its numerical indexes of the array with the data and then join them with array_merge, example:

<?php

$arrayTable = array(
   0 => array(
      'idApiUm' => 123,
      'title'  => 'Teste'
   ),
   1 => array (
      'idApiDois' => 765,
      'title'  => 'Título'
   ),
   2 => array(
      'idApiUm' => 632,
      'title'  => 'Nome'
   ),
   3 => array(
      'idApiDois' => 999,
      'title'  => 'Teste'
   ),
);

function get_items($array, $field)
{
    return array_unique(array_map(function($value) use ($field) { 
            return $value[$field]; 
        }, $array)
    );
}

function get_index($array, $field, $search) {
    return array_keys(
            array_filter($array,
                    function ($value) use ($search, $field) {
                        return (strpos($value[$field], $search) !== false);
                    }
            )
    );
}


$array_new = array();
foreach (get_items($arrayTable, 'title') as $value) 
{
    $array_index = get_index($arrayTable, 'title', $value);
    $array_index_item = array();
    foreach ($array_index as $i) 
    {
        if (count($array_index_item) == 0)
        {
            $array_index_item = $arrayTable[$i];
        }
        else
        {
            $array_index_item = array_merge($array_index_item, $arrayTable[$i]);
        }
    }
    $array_new[] = $array_index_item;
}

var_dump($array_new);

Example: IDEONE

References:

  • Jeez Virgilio, thank you so much!!!!! It worked perfectly and dynamically! It was exactly what I was looking for!! I haven’t been able to come up with that answer in days...

  • If the answer is vote for it and accept as answer to your question @Lucasmartins

  • 1

    I accepted! Again, thank you Virgilio!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.