1

I have an array.

Array
(
    [0] => min_order_level="5"
    [1] => max_order_level="10"
    [2] => step_order_level="4"
    [3] => product_box="9"
)

What I need is

Array
(
    [min_order_level] => "5"
    [max_order_level] => "10"
    [step_order_level] => "4"
    [product_box] => "9"
)

I have an idea of doing this using foreach. But without foreach is there any way to do this.

Thanks!

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Kathir
  • 1,212
  • 15
  • 25
  • You can use [array_flip()](http://www.php.net/manual/en/function.array-flip.php) [read this](http://stackoverflow.com/questions/6845037/how-to-swap-keys-with-values-in-array) – Himal Apr 29 '14 at 07:12
  • No, you can't, because the current values also include the `=` and the number. The OP wants to use the string part as the key and the number (with the quotes) after that as the value. – Amal Murali Apr 29 '14 at 07:16
  • Do you need number value like `5` or string value `"5"`(with qoutas)? – sectus Apr 29 '14 at 07:16
  • I need number value like `5` which is integer. – Kathir Apr 29 '14 at 07:39

4 Answers4

5

A simple foreach will do..

foreach($arr as $v)
{
    list($a,$b)= explode('=',$v);
    $new_arr[$a]=(int)trim($b,'"');
}
print_r($new_arr);

Working Demo


Without a foreach as requested on the question..

$new_arr = array();
array_map(function ($v) use (&$new_arr) { list($a,$b)= explode('=',$v); $new_arr[$a]=(int)trim($b,'"'); },$arr);
print_r($new_arr);

Working Demo

Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
1

One line without foreach

$array = [
    0 => 'min_order_level="5"',
    1 => 'max_order_level="10"',
    2 => 'step_order_level="4"',
    3 => 'product_box="9"'];

parse_str(implode('&', $array), $result);
var_dump($result);

array(4) {
  'min_order_level' =>
  string(3) ""5""
  'max_order_level' =>
  string(4) ""10""
  'step_order_level' =>
  string(3) ""4""
  'product_box' =>
  string(3) ""9""
}

Variant with preg_match_all

$string = implode("\n", $array);
preg_match_all('/(.*)="(.*)"/', $string, $matches);
$result = array_combine($matches[1], $matches[2]);
var_dump($result);
sectus
  • 15,605
  • 5
  • 55
  • 97
1

The simple foreach() mentioned in one of the answer should be enough, however, if you want to use array functions, you can do:

$new = array_combine(
        array_map(function($k){  $ex=explode('=',$k); return $ex[0]; }, $arr)
        ,
        array_map(function($v){  $ex=explode('=',$v); return $ex[1]; }, $arr)
);

The key point here is to break the string at = using explode(), pass all the keys and values separately using array_map() and combine them using array_combine().

DEMO

AyB
  • 11,609
  • 4
  • 32
  • 47
0

To do it without foreach, how about array_walk

$new_array = array();

array_walk($array, 'callback');

function callback($item2, $key)
{
    global $new_array;
    $parts = explode('=',$item2);
    $new_array[$parts[0]]=$parts[1];    
}

print_r($new_array);
Hassan
  • 742
  • 7
  • 13