简体   繁体   中英

Create multi-dimensional array from another multi-dimensional array PHP

Lets say I have a multi-dimensional array like this :

$results = array(
    0 => array(
        'fruit' => 'apple',
        'colour' => 'green',
        'amount' => 50
    ),
    1 => array(
        'fruit' => 'orange',
        'colour' => 'orange',
        'amount' => 25
    ),
    2 => array(
        'fruit' => 'banana',
        'colour' => 'yellow',
        'amount' => 7
    )
);

And I want to create a new multi-dimensional array only using specific objects :

$newarray = array(
    0 => array(
        'fruit' => 'apple',
        'amount' => 50
    ),
    1 => array(
        'fruit' => 'orange',
        'amount' => 25
    ),
    2 => array(
        'fruit' => 'banana',
        'amount' => 7
    )
);

How would I do this? I've read a few different things and it seems like array_map or array_column might be the answer but I haven't found an example that fits my situation.

I've got as far as :

$newarray = array();
foreach ($results as $result) {
    if (!empty($result['fruit'])) {
      // create new array here but how do I specify the key => values?
    }
}

This is suitable if the keys are fixed and you don't have too many of them:

$results = array(
    0 => array(
        'fruit' => 'apple',
        'colour' => 'green',
        'amount' => 50
    ),
    1 => array(
        'fruit' => 'orange',
        'colour' => 'orange',
        'amount' => 25
    ),
    2 => array(
        'fruit' => 'banana',
        'colour' => 'yellow',
        'amount' => 7
    )
);

$new = array();
foreach($results as $value){
    $new[] = array(
        'fruit' => $value['fruit'],
        'amount' => $value['amount']
    );
}
var_dump($new);

Gives:

array (size=3)
  0 => 
    array (size=2)
      'fruit' => string 'apple' (length=5)
      'amount' => int 50
  1 => 
    array (size=2)
      'fruit' => string 'orange' (length=6)
      'amount' => int 25
  2 => 
    array (size=2)
      'fruit' => string 'banana' (length=6)
      'amount' => int 7

in any case this is what you wan't correct?

foreach ($results as $key) {
    unset ($results[$key]['colour']);
}

print_r($results);

Output:

Array
(
[0] => Array
    (
        [fruit] => apple
        [amount] => 50
    )

[1] => Array
    (
        [fruit] => orange
        [amount] => 25
    )

[2] => Array
    (
        [fruit] => banana
        [amount] => 7
    )

)

Set your keys on "$keys_to_search". Is better than use unset.

$keys_to_search = ['fruit' => '','colour' => ''];

$results = array(
    0 => array(
        'fruit' => 'apple',
        'colour' => 'green',
        'amount' => 50
    ),
    1 => array(
        'fruit' => 'orange',
        'colour' => 'orange',
        'amount' => 25
    ),
    2 => array(
        'fruit' => 'banana',
        'colour' => 'yellow',
        'amount' => 7
    )
);




foreach($results as $key => $value){
    $result[] = array_intersect_key($value, $keys_to_search);  
}

print_r($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM