0

Hi I am working on very complex array operations.

I have $temp variable which stores pipe separated string like Height=10|Width=20

I have used explode function to convert into array and get specific output.

Below code i have try :

$product_attributes = explode("|",$temp)

//below output i get after the explode.

$product_attributes

Array(
       [0]=>Height=10
       [1]=>width=20
)

But i want to parse this array to separate one.

My expected output :

Array (
    [0]=>Array(
        [0] => Height
        [1] => 10
        )
    [1]=>Array(
        [0]=>Width
        [1]=>20     
        )

    )

Which function i need to used to get the desire output ?

Before downvoting let me know if i have made any mistake

3
  • The mistake I see that could grant you a downvote is that you have not included any code or attempts on how to solve it. Commented Jun 16, 2017 at 18:18
  • @Andreas I have done explode one time but second time how to use that i dont know Commented Jun 16, 2017 at 18:19
  • That is true. Sorry about that. (I have not downvoted or have any intention of doing so) Commented Jun 16, 2017 at 18:24

4 Answers 4

2

You could try the below code. I've tested this and it outputs the result you've shown in your post.

$temp = 'Height=10|Width=20';
$product_attributes = explode('|', $temp);
$product_attributes2 = array();
foreach ($product_attributes as $attribute) {
    $product_attributes2[] = explode('=', $attribute);
}
print_r($product_attributes2);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help
1

Try Below code

<?php

$temp = "Height=10|Width=20";

$product_attributes = explode("|", $temp);

foreach ($product_attributes as $k => $v) {
    $product_attributes[$k] = explode('=', $v);
}
echo '<pre>';
print_r($product_attributes);
?>

check running answer here

1 Comment

glad to help you.
1

Process your result by this:

$f = function($value) { return explode('=', $value); }
$result = array_map($f, $product_attributes);

Comments

1

One more option is to split the values in to one array and then build them from there.

$str = "Height=10|Width=20";
$arr = preg_split("/\||=/", $str);

$arr2= array();
$j=0;
for($i=0;$i<count($arr);$i++){
    $arr2[$j][]= $arr[$i];
    $arr2[$j][]= $arr[$i+1];
    $i++;
    $j++;

}
var_dump($arr2);

The output will be:

$arr = array(4){
         0  =>  Height
         1  =>  10
         2  =>  Width
         3  =>  20
        }

$arr2 = array(2) {
      [0]=>
      array(2) {
        [0]=>
        string(6) "Height"
        [1]=>
        string(2) "10"
      }
      [1]=>
      array(2) {
        [0]=>
        string(5) "Width"
        [1]=>
        string(2) "20"
      }
    }

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.