Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have trouble understanding arrays and I was wondering if someone could help me reformat an existing array in php.

This is the existing array:

Array
(
[item] => Array
(
    [0] => item listing 1
    [1] => item listing 2
)

[description] => Array
(
    [0] => item testing description
    [1] => item testing description
)

[rate] => Array
(
    [0] => 1.00
    [1] => 2.00
)

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

I want it to look like this:

Array
(
[0] => Array
(
    [item] => item listing 1
    [description] => item testing description
    [rate] => 1.00
    [itemid] => 1
)
[1] => Array
(
    [item] => item listing 2
    [description] => item testing description
    [rate] => 2.00
    [itemid] => 2
)
share|improve this question
    
What have you tried yourself? Or is this homework? –  Dainis Abols Jul 19 '12 at 17:05

3 Answers 3

up vote 3 down vote accepted

If the length of all the sub-arrays in the first one are the same this should work.

Assume the first array above is in a variable $inArray; the new array is $outArray.

$outArray = array();
$iLength  = count($inArray['item']);
for($i=0; $i<$iLength; $i++) {
    $outArray[] = array(
        'item'        => $inArray['item'][$i],
        'description' => $inArray['description'][$i],
        'rate'        => $inArray['rate'][$i],
        'itemid'      => $inArray['itemid'][$i]);
}
share|improve this answer
    
+1 Looks good: ideone.com/GAEhI –  mellamokb Jul 19 '12 at 17:12
    
whoops, that's what I get for editing directly in the web page! –  quickshiftin Jul 19 '12 at 17:13
1  
thanks for your help works like a charm!! –  neoszion Jul 19 '12 at 17:38

Ok so if your master array is called $master. Then you'd do something like this:

$newArr = array();
foreach ($master as $key => $subArray) {
    foreach ($subArray as $k2 => $value) {
        $newArr[$k2][$key] = $value;
    }
}
share|improve this answer
1  
+1 Works! ideone.com/b9Gkb –  mellamokb Jul 19 '12 at 17:14
    
what's the $counter for? –  F. Orvalho Jul 19 '12 at 17:16
    
Woops I had that there when I thought I needed to keep track of the array key, then realized I had access to it from $k2 :). Moved it out to remove confusion. –  aztechy Jul 19 '12 at 17:18
1  
For what it's worth I think this should have been the accepted answer. –  F. Orvalho Jul 20 '12 at 7:56

Working for your specific use case(copy/paste on a blank php):

$master = array( 
  'itemid' => array(1, 2),
  'items' => array('item listing 1', 'item listing 2'),
  'description' => array('item testing description', 'item testing description'),
  'rate' => array(1.10, 2.10)
);

$newItems = array();
foreach($master['itemid'] as $index => $id) {
  $newItem = array(
    'itemid' => $id,
    'item' => $master['items'][$index],
    'description' => $master['description'][$index],
    'rate' => $master['rate'][$index],
  );
  $newItems[] = $newItem;
}

echo '<pre>';
print_r($newItems);
echo '</pre>';
share|improve this answer
    
Note that you can also test code and link to a runnable sample using the excellent site ideone.com. –  mellamokb Jul 19 '12 at 17:14
    
But the output is confusing, I prefer this: writecodeonline.com/php :) (however this does not work as "paste bin" but the output is more clear. –  Daniel Aranda Jul 19 '12 at 17:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.