PHP在foreach中创建key => value对

时间:2021-08-26 22:09:06

I want to create a key-value pairs in an array within a foreach. Here is what I have so far:

我想在foreach中的数组中创建键值对。这是我到目前为止:

function createOfferUrlArray($Offer) {
    $offerArray = array();

    foreach ($Offer as $key => $value) { 
        $keyval = array($key => $value[4] );

        array_push($offerArray,$keyval);
    }

    return $offerArray;
}   

If I declare the array within the foreach, it will overwrites it on each iteration, but defining it outside the foreach doesn't work either and causes triplets:

如果我在foreach中声明数组,它将在每次迭代时覆盖它,但是在foreach之外定义它也不起作用并导致三元组:

array[0] => key => value
array[1] => key => value 

How do I make it so I only get key-value pairs like this?

我怎么做到这样我只得到像这样的键值对?

key => value
key => value

5 个解决方案

#1


39  

Something like this?

像这样的东西?

foreach ($Offer as $key => $value) { 
  $offerArray[$key] = $value[4];
}

#2


3  

Create key-value pairs within a foreach like this:

在foreach中创建键值对,如下所示:

function createOfferUrlArray($Offer) {
    $offerArray = array();

    foreach ($Offer as $key => $value) {
        $offerArray[$key] = $value[4];
    }

    return $offerArray;
}

#3


3  

Create key value pairs on the phpsh commandline like this:

在phpsh命令行上创建键值对,如下所示:

php> $keyvalues = array();
php> $keyvalues['foo'] = "bar";
php> $keyvalues['pyramid'] = "power";
php> print_r($keyvalues);
Array
(
    [foo] => bar
    [pyramid] => power
)

Get the count of key value pairs:

获取键值对的计数:

php> echo count($offerarray);
2

Get the keys as an array:

获取键作为数组:

php> echo implode(array_keys($offerarray));
foopyramid

#4


1  

In PHP >= 5.3 it can be done like this:

在PHP> = 5.3中,可以这样做:

$offerArray = array_map(function($value) {
    return $value[4];
}, $offer);

#5


-1  

function createOfferUrlArray($Offer) {
    $offerArray = array();
    foreach ($Offer as $key => $value) { 
        $offerArray[$key] = $value[4];
    }
    return $offerArray;
}

or

要么

function createOfferUrlArray($offer) {
    foreach ( $offer as &$value ) {
        $value = $value[4];
    }
    unset($value);
    return $offer;
}

#1


39  

Something like this?

像这样的东西?

foreach ($Offer as $key => $value) { 
  $offerArray[$key] = $value[4];
}

#2


3  

Create key-value pairs within a foreach like this:

在foreach中创建键值对,如下所示:

function createOfferUrlArray($Offer) {
    $offerArray = array();

    foreach ($Offer as $key => $value) {
        $offerArray[$key] = $value[4];
    }

    return $offerArray;
}

#3


3  

Create key value pairs on the phpsh commandline like this:

在phpsh命令行上创建键值对,如下所示:

php> $keyvalues = array();
php> $keyvalues['foo'] = "bar";
php> $keyvalues['pyramid'] = "power";
php> print_r($keyvalues);
Array
(
    [foo] => bar
    [pyramid] => power
)

Get the count of key value pairs:

获取键值对的计数:

php> echo count($offerarray);
2

Get the keys as an array:

获取键作为数组:

php> echo implode(array_keys($offerarray));
foopyramid

#4


1  

In PHP >= 5.3 it can be done like this:

在PHP> = 5.3中,可以这样做:

$offerArray = array_map(function($value) {
    return $value[4];
}, $offer);

#5


-1  

function createOfferUrlArray($Offer) {
    $offerArray = array();
    foreach ($Offer as $key => $value) { 
        $offerArray[$key] = $value[4];
    }
    return $offerArray;
}

or

要么

function createOfferUrlArray($offer) {
    foreach ( $offer as &$value ) {
        $value = $value[4];
    }
    unset($value);
    return $offer;
}