开发者

how to split an single array in to arrays - PHP

开发者 https://www.devze.com 2023-04-05 22:51 出处:网络
I need开发者_运维知识库 to split single array in to multiple arrays. For example: Array ( [0] => Array

I need开发者_运维知识库 to split single array in to multiple arrays.

For example:

Array
(
    [0] => Array
        (
            [pageviews] => 26
            [visits] => 20
        )

    [1] => Array
        (
            [pageviews] => 9
            [visits] => 4
        )

    [2] => Array
        (
            [pageviews] => 18
            [visits] => 9
        )
)

I need to split the array like below:

Array
(

            [ga:pageviews] => 26
            [ga:visits] => 20
)

Array
(
            [ga:pageviews] => 9
            [ga:visits] => 4
)

Array
(
            [ga:pageviews] => 18
            [ga:visits] => 9
)

How can i do this?

Any help will be thankful and grateful...

Thanks in advance..


Ok, so using foreach:

foreach ( $original as $item ) {
    var_dump( array(
        'ga:pageviews' => $item['pageviews'],
        'ga:visits' => $item['visits'],
    ) );
}


try

$array =     Array
(
    [0] => Array
        (
            [pageviews] => 26
            [visits] => 20
        )

    [1] => Array
        (
            [pageviews] => 9
            [visits] => 4
        )

    [2] => Array
        (
            [pageviews] => 18
            [visits] => 9
        )
)

for($x=0; $x<count($array); $x++){
   $newArray = $array[$x];       // that extract the second array, containing pageview and visits.
}


According to your samples, you seem to want to split one variable into multiple variables (or perhaps you used incorrect notation in the 2nd one?). If that is the case, and you know how many arrays are in the starting variable, you can do this:

list($one, $two, $three) = $originalArray;

If you don't know how many arrays are in the original array, or there is more than a handful, I have to wonder why you would want to or need to do this in the first place...

0

精彩评论

暂无评论...
验证码 换一张
取 消