0

I have an array:

$settings = array(
    'name' => array(
        0 => 'Large Pouch',
        1 => 'XL Pouch'
    ),
    'size' => array(
        0 => '9x14',
        1 => '12x18'
    ),
    'weight' => array(
        0 => '10',
        1 => '20'
    ),
    'metro_manila_price' => array(
        0 => '59',
        1 => '79'
    ),
    'luzvimin_price' => array(
        0 => '89',
        1 => '139'
    )
);

I wanted to convert the column-based values from that array to a flat array of formatted strings:

$shipping_options = array(
    '0' => 'Large Pouch 9x14 - $59',
    '1' => 'XL Pouch 12x18 - $79'
);

How to program this?

0

3 Answers 3

2

You could write a loop:

$shipping_options = array();
foreach ($settings['name'] as $key => $value) {
   $value = sprintf('%s(%s) - $%s', 
                    $value, 
                    $settings['size'][$key], 
                    $settings['metro_manila_price'][$key]);
   $shipping_options[$key] = $value;
}
Sign up to request clarification or add additional context in comments.

Comments

0

try this one

echo "<pre>";
$size = count($settings['name']);
$shipping_options = array();
for($i=0; $i<$size; $i++)
{
    $shipping_options[$i] = $settings['name'][$i]."(".$settings['size'][$i].") - $".$settings['metro_manila_price'][$i];
}

print_r($shipping_options);

Comments

0

You can try this

foreach ($settings['name'] as $key => $value) {
    $shipping_options[$key] = $settings['name'][$key] . " " . $settings['size'][$key] . " - $" .    $settings['metro_manila_price'][$key];
}

Comments

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.