简体   繁体   中英

PHP: Efficient way to access nested array elements?

I am tidying up some old code i've come across on a job including various arrays which i'd like to combine into nested arrays and would like to know a simple way of looping throught the contents of a nested array (likely that there's a better way to store the data than a nested array in the first place, if so, suggestions welcome).

The following code is just an example of the original code structure, behaves in the same way just generalised output.

$someArray elements used to be defined indifvidually which seems very time consuming to manage hence why i want to change it:

$fruit['fruit']['apple']['size'] = 'small';
$fruit['fruit']['apple']['colour'] = 'red';
$fruit['fruit']['apple']['taste'] = 'bitter';
$fruit['fruit']['pear']['size'] = 'big';
$fruit['fruit']['pear']['colour'] = 'green';
$fruit['fruit']['pear']['taste'] = 'sweet'; 

Here's an example of the nested arrays that i'm building:

class someEntity 
{
    public function someFunction()
    {       

    $someArray = array
    (
    'apple' => array(
            'size' => 'small',
            'colour' => 'red',
            'taste' => 'bitter'
            ),
    'pear' => array(
            'size' => 'big',
            'colour' => 'green',
            'taste' => 'sweet'
            )
    );
    return($someArray);
    }

    public function anotherFunction()
    {
    # some other stuff
    }

}

Calling via foreach loop:

$someArray= someEntity::someFunction();
var_dump($someArray);


foreach($someArray as $key)
{   
    foreach($key as $key => $value)
    {
    print($key.': '.$value.'<br>');
    }
}


array (size=2)
  'apple' => 
    array (size=3)
      'size' => string 'small' (length=5)
      'colour' => string 'red' (length=3)
      'taste' => string 'bitter' (length=6)
  'pear' => 
    array (size=3)
      'size' => string 'big' (length=3)
      'colour' => string 'green' (length=5)
      'taste' => string 'sweet' (length=5)  

Output :

size: small colour: red taste: bitter size: big colour: green taste: sweet

Questions :

  1. What's the best way to compress the $fruits array? Is my $someArray approach incorrect?
  2. Is there a better way to call $someArray data without nested foreach loops?

Consider :

  1. The nested arrays may get more complex, possible an additional layer deep
  2. Do not want to use a database for this scenario

Thank you in advance

Updated

I've reworked as follows using an object.

class fruit 
{
    private  $_type;
    private  $_size;            
    private  $_colour;
    private  $_taste;

    public function __construct($type,$size,$colour,$taste)
    {
    $this->_type = $type;
    $this->_size = $size;   
    $this->_colour = $colour;
    $this->_taste = $taste;             
    }

    public function displayFruitData()
    {

           echo'<br>'.$this->_type.'<br>'.$this->_size.'<br>'.$this->_colour.'<br>'.$this->_taste.'<br><br>';       
    }       
}   
    $fruit1 = new fruit("apple","small","red","bitter");
    $fruit2 = new fruit("pear","medium","yellow","sweet");
    $fruit3 = new fruit("pineapple","large","brown","sweet");       

    $output = $fruit1->displayFruitData();  
    $output = $fruit2->displayFruitData();  
    $output = $fruit3->displayFruitData();  


    exit();

you can do this in easy way like this

<?php
  $fruits = array('apple', 'pear');
  $size = array('apple' => 'small', 'pear' => 'big');
  $colour = array('apple' => 'red', 'pear' => 'green');
  $taste = array('apple' => 'bitter', 'pear' => 'sweet');

  foreach($fruits as $fruit)
  {
    echo "$fruit Size is {$size[$fruit]}<br />";
    echo "$fruit Colour is {$colour[$fruit]}<br />";
    echo "$fruit Taste is {$taste[$fruit]}<br />";
    echo '<br />';
  }
?>

Output

apple Size is small
apple Colour is red
apple Taste is bitter

pear Size is big
pear Colour is green
pear Taste is sweet

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