2

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
)
0

3 Answers 3

3

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]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

whoops, that's what I get for editing directly in the web page!
2

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;
    }
}

2 Comments

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.
For what it's worth I think this should have been the accepted answer.
0

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>';

2 Comments

Note that you can also test code and link to a runnable sample using the excellent site ideone.com.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.