0

I have an array $categories in php as follows.

Array
(
    [item_name] => Array
        (
            [0] => I-Phone
            [1] => samsung
            [2] => nokia
            [3] => htc
        )

    [item_price] => Array
        (
            [0] => 30.00
            [1] => 20
            [2] => 10
            [3] => 15
        )
)

And I want to transpose its element as,

Array
(
    [0] => Array
        (
            [item_name] => I-Phone
            [item_price] => 30.00
        )

    [1] => Array
        (
            [item_name] => samsung
            [item_price] => 20
        )
    [2] => Array
        (
            [item_name] => nokia
            [item_price] => 10
        )

    [2] => Array
        (
            [item_name] => htc
            [item_price] => 15
        )
)

I've tried using foreach loop but not working.

$count=0;
foreach ($categories as $key=> $category)
{
        $categories[$count] = $category[$key];
        $categories[$count] = $category[$key];
        $count++;
}
Ahmed Syed
  • 1,179
  • 2
  • 18
  • 44

5 Answers5

5

Here's an approach:

$categories = array(
    'item_name' => array('I-Phone', 'samsung', 'nokia','htc'),
    'item_price' => array('30.00', '20', '10', '15')
);

$out = array();
foreach($categories as $key => $a){
    foreach($a as $k => $v){
        $out[$k][$key] = $v;
    }
}
echo '<pre>';
print_r($out);
echo '</pre>';
larsAnders
  • 3,813
  • 1
  • 15
  • 19
  • 1
    Sure, the outer foreach runs twice, once for item_name and once for item_price. On the first pass, it creates four new array items `$out[0]['item_name']` through `$out[3]['item_name']`. On the second pass, the item_prices are filled in. Note that if the two arrays must have the same number of elements. If they have different numbers of elements, this will not work as expected. – larsAnders Apr 08 '16 at 07:00
  • The explanation should inside the question. Please edit your answer then delete the comment. – mickmackusa Jun 23 '20 at 13:12
2

Simple solution using array_keys and array_map functions:

$keys = array_keys($categories);
$transposed = array_map(function($a, $b) use($keys){
    return [$keys[0] => $a, $keys[1] => $b];
}, $categories['item_name'], $categories['item_price']);

print_r($transposed);

The output:

Array
(
    [0] => Array
        (
            [item_name] => I-Phone
            [item_price] => 30
        )

    [1] => Array
        (
            [item_name] => samsung
            [item_price] => 20
        )

    [2] => Array
        (
            [item_name] => nokia
            [item_price] => 10
        )

    [3] => Array
        (
            [item_name] => htc
            [item_price] => 15
        )    
)
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
1

Something like this:

$categories = array();  // your array
$ctg_names = array_keys($categories);   // get names of source array keys
$new_ctgs = array();   // result array

// iterate over first key values to get indexes
foreach ($categories[$ctg_names[0]] as $k => $v) {
    $ctg = array();
    // iterate over all key names
    foreach ($ctg_names as $name) {
        $ctg[$name] = $categories[$name][$k];
    }
    $new_ctgs[] = $ctg;
}

print_r($new_ctgs);
u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

Try this:

<?php

$array = Array
(
    'item_name' => Array
        (
            '0' => 'I-Phone',
            '1' => 'samsung',
            '2' => 'nokia',
            '3' => 'htc',
        ),

    'item_price' => Array
        (
            '0' => 30.00,
            '1' => 20,
            '2' => 10,
            '3' => 15,
        )
);

$keys = array_keys($array);
$result = array();

for($i = 0; $i < count($array['item_name']); $i++)
{
    $tempArray = array();
    foreach($keys as $key)
        $tempArray[$key] = $array[$key][$i];

    $result[] = $tempArray;
}

Output:

Array
(
    [0] => Array
        (
            [item_name] => I-Phone
            [item_price] => 30
        )

    [1] => Array
        (
            [item_name] => samsung
            [item_price] => 20
        )

    [2] => Array
        (
            [item_name] => nokia
            [item_price] => 10
        )

    [3] => Array
        (
            [item_name] => htc
            [item_price] => 15
        )

)
aslawin
  • 1,981
  • 16
  • 22
-1
$result = [];
foreach($categories['Item_name'] as $key => $data){
    $result[$key]['category'] = $categories['Item_name']['$key'];
    $result[$key]['price'] = $categories['price'][$key]; 
}
dd($result);
Sushant Yadav
  • 726
  • 1
  • 13
  • 28