0

I have an array like:

print_r($arr);

array(2) {
  ["'type'"]=>
  array(3) {
    [0]=>
    string(17) "tell" // <----
    [1]=>
    string(6) "mobile" // <----
    [2]=>
    string(6) "address" // <----
  }
  ["'value'"]=>
  array(3) {
    [0]=>
    string(11) "+00.0000000" // tell
    [1]=>
    string(11) "12345678" // mobile
    [2]=>
    string(11) "Blah SQ." // address
  }
}

I want a final string like:

tell = +00.0000000<br />mobile = 12345678<br />address = Blah SQ.

Now it's been more than an hour I'm struggling with this but no results yet, anyone could help me with this? I would appreciate anykind of help.

Thanks

=======================================

What I have tried:

$arr is an array so I did:

foreach($arr as $values){
// here also $values is an array, so I needed another foreach to access to items:
    $i = 0;
    foreach($values as $items){
        // now making the final output
        @$output.= $items['type'][$i] . '=' . $items['value'][$i] . '<br />';
        $i++;
    }
}
10
  • 1
    Please post your best attempt and explain what the idea behind it is and how exactly it is not working. Commented Jul 15, 2014 at 10:08
  • Take a look at array_combine. Commented Jul 15, 2014 at 10:09
  • @Jon I added what I've done so far. Commented Jul 15, 2014 at 10:12
  • OK, nice. So why are you iterating over $arr itself? You know there's $arr['type'] and $arr['value'], and what you want is inside. That's what you should be targeting, not $arr. Commented Jul 15, 2014 at 10:14
  • // here also $values is an array, so I needed another foreach to access to items: Apparently $arr is also an array Commented Jul 15, 2014 at 10:14

2 Answers 2

3

I'd go for array_combine(). Basically it does what you request:

$yourArray = [
             "type"  => ["tell", "mobile", "address"], 
             "value" => ["+00.0000000", "12345678", "Blah SQ."]
             ];


$combined = array_combine($yourArray["type"], $yourArray["value"]);

will be

$combined = [
             "tell"    =>"+00.0000000",
             "mobile"  =>"12345678",
             "address" =>"Blah SQ."
            ];

Lastly, you can iterate through that array and then join the values:

$finalArray=array();

foreach($combined as $type=>$value)
      $finalArray[]="$type=$value";

$string = join("<br/>", $finalArray); // Will output tell=+00.000000<br/>mobile=12345678<br/>address=Blah SQ.

It's not the fastest method but you'll learn quite a bit about arrays.

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

Comments

0

EDIT (using array_combine by @Dencker)

foreach($arr as $values) {
// here also $values is an array, so I needed another foreach to access to items:
    $v = array_combine($values["'type'"], $values["'value'"]);

    foreach($v as $key => $val) {
        // now making the final output
        $output.= $key . '=' . $val . '<br />';
    }
}

Try this

    $arr = array(
    array(
        "'type'" => array('tell', 'mobile', 'address'),
        "'value'" => array('+00000', '123123', 'foo')
    ),
    array(
        "'type'" => array('tell', 'mobile', 'address'),
        "'value'" => array('+10000', '123123', 'bar')
    ),
    array(
        "'type'" => array('tell', 'mobile', 'address'),
        "'value'" => array('+20000', '123123', 'foobar')
    ),
);

var_dump($arr);

$output = '';

foreach($arr as $values) {
// here also $values is an array, so I needed another foreach to access to items:
    $i = 0;

    foreach($values as $items) {
        // now making the final output
        $output.= $values["'type'"][$i] . '=' . $values["'value'"][$i] . '<br />';
        $i++;
    }
}

echo $output;

You were referencing the other array in the second loop.

============================================================= EDIT:

var_dump($arr);

array(3) { 
  [0]=> array(2) { 
       ["type"]=> array(3) { 
            [0]=> string(4) "tell" 
            [1]=> string(6) "mobile" 
            [2]=> string(7) "address" 
       } 
       ["value"]=> array(3) { 
            [0]=> string(6) "+00000" 
            [1]=> string(6) "123123" 
            [2]=> string(3) "foo" 
       } 
  } 
  [1]=> array(2) { 
       ["type"]=> array(3) { 
            [0]=> string(4) "tell" 
            [1]=> string(6) "mobile" 
            [2]=> string(7) "address" 
       } 
       ["value"]=> array(3) { 
            [0]=> string(6) "+10000" 
            [1]=> string(6) "123123" 
            [2]=> string(3) "bar" 
       } 
  } 
  [2]=> array(2) { 
      ["type"]=> array(3) { 
            [0]=> string(4) "tell" 
            [1]=> string(6) "mobile" 
            [2]=> string(7) "address" 
      } 
      ["value"]=> array(3) { 
            [0]=> string(6) "+20000" 
            [1]=> string(6) "123123" 
            [2]=> string(6) "foobar" 
      } 
  } 
} 

OUTPUT:
tell=+00000
mobile=123123
tell=+10000
mobile=123123
tell=+20000
mobile=123123

1 Comment

Thanks but you've changed the question!!! the array I'm working with is in my question... .

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.