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!

4
  • You can use array_flip() read this Commented Apr 29, 2014 at 7: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. Commented Apr 29, 2014 at 7:16
  • Do you need number value like 5 or string value "5"(with qoutas)? Commented Apr 29, 2014 at 7:16
  • I need number value like 5 which is integer. Commented Apr 29, 2014 at 7:39

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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

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.