vinod reddy
vinod reddy

Reputation: 133

In zend pass values to view by concating array in controller

I have below array where I am getting this array by executing an MySQL query in zend. I want to concatenate all the octent and get the result as 131.208.0.0 and 141.128.0.0 to pass to view to display.

Array
(
    [0] => Array
        (
            [octet1] => 131
            [octet2] => 208
            [octet3] => 0
            [octet4] => 0
        )

    [1] => Array
        (
            [octet1] => 141
            [octet2] => 128
            [octet3] => 0
            [octet4] => 0
        )
)

With the below foreach I get all ailments how do i concatenate each octent for an array.

 foreach($arr as $external)
       {
            foreach ($external as $octent)
            {
                echo $octent."<br />";
            }
       }

Upvotes: 1

Views: 350

Answers (2)

ttzn
ttzn

Reputation: 2613

If you don't need to work with the individual octets and have access to the query for modification, you could just retrieve CONCAT(octet1, '.', octet2, '.', octet3, '.', octet4) in the SELECT clause.

Otherwise you can just do this :

// array_map applies a function to every element of an array
$concatenated_arr = array_map(function($e) { return implode('.', $e); }, $arr);

Upvotes: 1

Gianpaolo Di Nino
Gianpaolo Di Nino

Reputation: 1147

The implode function is what you are searching for:

 $results = array();
 foreach($arr as $external){
       $results[] = implode('.', $external);
 }

 print_r($results);

Upvotes: 2

Related Questions