0

I have a simple php question.

I have a value like the following for 2 products. 1 product has options density, flavor and pid other product has option color and pid

product 1

density: Low 12mg, flavor: Dekang Dark USA Mix, pid: 165 

product 2

color: Silver, pid: 5 

The above 2 values comes in a single array

$items[$i]['options']

How can i convert it into an array of the following format...

product 1-   density => 'Low 12mg, flavor =>  'Dekang Dark USA Mix', pid => '165'
product 2-    color =>  'Silver', pid => '5'

and how to call it in a foreach loop so that i get values separately for 2 products ?

user3790186
  • 239
  • 6
  • 21

1 Answers1

2

Assuming options values are separated by comma:

$myArray = explode(',', $items[$i]['options']);

Then you can use that array to fill in your object. Full example:

$items[0] = array("id" => 1,  
          "options" => 'density: Low 12mg, flavor: Dekang Dark USA Mix, pid: 165');
$items[1] = array("id" => 2,
          "options" => 'color: Silver, pid: 5');

$newItems = array();
foreach ($items as $item) {

    $options = explode(',', $item['options']);

    foreach ($options as $option) {
        $values = explode(':', $option);
        $item[trim($values[0])] = trim($values[1]);
    }
    $newItems[] = $item;
}

print_r($newItems);

http://sandbox.onlinephpfunctions.com/code/1b4ccc3ee27b53340cabff8d313f4733085e01e2

spacebiker
  • 3,777
  • 4
  • 30
  • 49
  • I think the OP wants to make the `label:` an associative array of the item pieces. So: `$item['density'] => 'Low 12mg'`. – Jared Farrish Jul 20 '14 at 12:59
  • @Xabier The above code is not working...I have corrected errors in code above like missing closing bracket etc.. – user3790186 Jul 20 '14 at 13:06
  • well, it was just a quick example, you will have to test further, i am writing the full sample and post it soon – spacebiker Jul 20 '14 at 13:10
  • @Xabier This link is of similar type issue. but i am unable to understand it: http://stackoverflow.com/questions/4335963/explode-two-item-list-in-array-as-key-value – user3790186 Jul 20 '14 at 13:21
  • @Xabier I am still getting options as density: Low 12mg, flavor: Dekang Dark USA Mix, pid: 165 I require it like $item['density'] => 'Low 12mg'...for second product options are different color and pid – user3790186 Jul 20 '14 at 13:45
  • just change the options to whatever the options are in your sample, if you check the sandbox example you can see that you get `$item[density] => Low 12mg`etc.. – spacebiker Jul 20 '14 at 13:47
  • @Xabier How to call individual items like pid, density etc. Array ( [0] => Array ( [name] => Dekang 10 [quantity] => 2 [price] => 6.27 [options] => density: Low 12mg, flavor: Dekang Dark, pid: 165 [density] => Low 12mg [ flavor] => Dekang Dark [ pid] => 165 ) ) Array ( [0] => Array ( [name] => Dekang 10 [quantity] => 2 [price] => 6.27 [options] => density: Low 12mg, flavor: Dekang Dark, pid: 165 [color] => Silver [ pid] => 5 ) [1] => Array ( [name] => 2014 Battery [quantity] => 1 [price] => 23.52 [options] => color: Silver, pid: 5 [color] => Silver [ pid] => 5 ) ) – user3790186 Jul 20 '14 at 13:53
  • for example, for the first item you would access density like this: `$newItems[0]['density'];` – spacebiker Jul 20 '14 at 14:03
  • @Xabier The only value i am getting is density...echo $newItems[0]['pid']; echo $newItems[0]['density']; pid no value, flavor no value – user3790186 Jul 20 '14 at 14:11
  • and i need to call it in a for loop like echo $newItems[$i]['pid'] $i is my counter – user3790186 Jul 20 '14 at 14:15
  • 1
    @user3790186 - To a certain degree, you may need to take code that demonstrates what you need and customize it to your particular code sequence. For instance, wrap it in a function and call the function when you need it. – Jared Farrish Jul 20 '14 at 14:16
  • But unfortunately the code is displaying only density values....$newItems[$i]['density']; and no other values like $newItems[$i]['pid']; – user3790186 Jul 20 '14 at 14:21
  • @user3790186, check this code: http://sandbox.onlinephpfunctions.com/code/0ca39e43b16428b81e95bc7ffda404283009e3e8 – spacebiker Jul 20 '14 at 14:24
  • @user3790186 - Why don't you keep `options` as a sub-array and set each `$newItems` after parsing to it? That way it doesn't overwrite your primary array values? Otherwise you would need to merge the `$newItems` with your current array iteration. The code works as asked though, so you need to think about how to properly integrate it. – Jared Farrish Jul 20 '14 at 14:25
  • This is just a sample that replies the main question, of course you will have to adapt the code to your final needs. Might you consider overwriting $items elements, use it in a new array as the code provided, or anything else – spacebiker Jul 20 '14 at 14:35
  • @Xabier Thank you. It is working fine. Thank you once again for your patience. – user3790186 Jul 20 '14 at 14:47