0

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?
    }
}
11
  • Why are you using ->? you need to use [] in arrays Commented Apr 12, 2018 at 22:40
  • The $results array is a json_decoded object. I just wrote it as a straight up php array to try and get across what I was trying to achieve. I've changed it so it's less confusing. Commented Apr 12, 2018 at 22:41
  • @MehdiBounya No what he does is perfectly fine. Well if he uses PHP 5.3 ... short array syntax is fully supported since PHP 5.6 ... but the => operator is still fine to assign values Commented Apr 12, 2018 at 22:43
  • I'm actually using 7.1 ;) Commented Apr 12, 2018 at 22:44
  • @sietse85 I'm not talking about =>, I'm talking about using -> to access elements in arrays (check the edit history) Commented Apr 12, 2018 at 22:44

3 Answers 3

2

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
    )

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

8 Comments

Okay, great that's nice and simple. The only issue is that my actual $results array contains about 50 objects and I only want to use 3. It would be faster if I could specify which ones I wanted instead of unsetting 47 of them. How would I do that?
can you explain this a little bit more in depth? because i am not sure
@Mike I totally agree and apologies. But if I had posted an array with 50 objects it would have looked like a pigs ear.
@sietse The $results array is massive, it contains about 50 objects of which I only want to use 3 in the new array. Does that make sense?
@spice I agree posting the whole thing would be unnecessary, but if you're not going to use your actual code, you should describe it as well as you can.
|
2

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

2 Comments

This is exactly what I was after. Thanks bro! As mentioned below I will have to give the correct answer to @sietse85 as he answered my question correctly. I just wasn't clear enough about my particular use case which you have solved for me, so thanks again.
0

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.