1

hello i am thinking if how to combine a array with their same key value pairs

Array
(
    [category_id] => Array
        (
            [0] => 1
            [1] => 2
        )

    [product] => Array
        (
            [0] => Apple
            [1] => Cow
        )

    [category] => Array
        (
            [0] => Fruit
            [1] => Meat
        )
)

i already used array_combine but the expecting output was not like that output.

the output i want to see is like this.

1 , Apple , Fruit
2 , Cow , Meat

i dont know if how to do this. hopefully you can help me. thank you very much

Ray Paras
  • 185
  • 11
  • [array_column()](https://php.net/array_column) then [implode()](https://php.net/implode)? – Syscall Apr 15 '21 at 15:47
  • 1
    Side note: it's better to share the output of `var_export` than `print_r`, because then anyone can easily copy the contents to their editor for testing purposes. – El_Vanja Apr 15 '21 at 15:49

3 Answers3

2

Direct Output

If you just need to output your array directly to the page, you could do something like this:

foreach($original_array['category_id'] as $key => $categoryid) {
    $product = $original_array['product'][$key];
    $category = $original_array['category'][$key];
    
    echo "{$categoryid} , {$product} , {$category}\n";
}

This is pretty simple and only requires a single foreach loop. This is useful if you do not need to use this data multiple times in different places.


Formatted Array Output

If you may need to use this later in code, I would create a new array with the data in the loop above. This is probably overkill if you don't ever need to access this data again.

$output = [];
foreach($original_array['category_id'] as $key => $categoryid) {
    $product = $original_array['product'][$key];
    $category = $original_array['category'][$key];
    
    $output[] = ['category_id' => $categoryid, 'product' => $product, 'category' => $category];
}

This would make $output be something like

Array
(
    [0] => Array
        (
            [category_id] => 1
            [product] => Apple
            [category] => Fruit
        )

    [1] => Array
        (
            [category_id] => 2
            [product] => Cow
            [category] => Meat
        )

)

Which, of course, is trivial to output in your required format.

foreach($output as $values) {
    echo "{$values['categoryid']} , {$values['product']} , {$values['category']}";
}
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
1

A simple foreach loop seems the best approach in this case

$original = [
    'category_id' => [1, 2],
    'product' => ["Apple", "Cow"],
    'category' => ["Fruit", "Meat"]
];

foreach ( $original['category_id'] as $key => $v) {
    printf("%d, %s, %s\n", $v, $original['product'][$key], $original['category'][$key]);
}

RESULT

1, Apple, Fruit
2, Cow, Meat
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

Another dynamic way to do this, is to use array_column() and implode().

$array = [
    'category_id' => [1,       2],
    'product'     => ['Apple', 'Cow'],
    'category'    => ['Fruit', 'Meat'],
];

// Gets the number of items in the first subarray:
$numColumns = count(reset($array));

// Loop around the columns:
for ($idx = 0; $idx < $numColumns; $idx++)
{
    // Print the column $idx, separated by ', ':
    echo implode(', ', array_column($array, $idx)) . "\n";
}

Output:

1, Apple, Fruit
2, Cow, Meat
Syscall
  • 19,327
  • 10
  • 37
  • 52