Home > Blockchain >  PHP, Changing values in an multidimensional array
PHP, Changing values in an multidimensional array

Time:05-26

I got the following Array in PHP:

$data[] = array('Slug' => 'jan', 'Name' => 'Jan', 'Alter' => '39', 'Jahrgang' => '1981', 'ID' => '3');
$data[] = array('Slug' => 'kjell', 'Name' => 'Kjell', 'Alter' => '4', 'Jahrgang' => '2018', 'ID' => '0');
$data[] = array('Slug' => 'bjarne', 'Name' => 'Bjarne', 'Alter' => '6', 'Jahrgang' => '2015', 'ID' => '2');
$data[] = array('Slug' => 'monika', 'Name' => 'Monika', 'Alter' => '72', 'Jahrgang' => '1950', 'ID' => '1');

How can I output the Value of "Jahrgang" where "Slug = Jan"?

How can I change the "Jahrgang" where "Slug = Jan" from 1981 to 1982?

Is there any way without a foreach?

Thank you so much!

CodePudding user response:

It can be done with pair array_search and array_column like this:

$key = array_search('jan', array_column($data, 'Slug'));

array_column returns single dimension array only with given key, in this case Slug, and then array_search returns key of this value in reduced array with should be same as in main one. So for questions it would be:

echo $data[array_search('jan', array_column($data, 'Slug'))]['Jahrgang'];

$data[array_search('jan', array_column($data, 'Slug'))]['Jahrgang'] = '1982';

CodePudding user response:

I would opt to slightly restructure the data for easier access, by setting the array key to the Slug value.

$data['jan'] = array('Name' => 'Jan', 'Alter' => '39', 'Jahrgang' => '1981', 'ID' => '3');
$data['kjell'] = array('Name' => 'Kjell', 'Alter' => '4', 'Jahrgang' => '2018', 'ID' => '0');
$data['bjarne'] = array('Name' => 'Bjarne', 'Alter' => '6', 'Jahrgang' => '2015', 'ID' => '2');
$data['monika'] = array('Name' => 'Monika', 'Alter' => '72', 'Jahrgang' => '1950', 'ID' => '1');

This would allow you to access or set any values using that key.

//access
$info = $data['jan']; //array('Name' => 'Jan', 'Alter' => '39', 'Jahrgang' => '1981', 'ID' => '3')

//set
$data['jan']['Name'] = 'Not Jan';

//loop
foreach($data as $slug => $info) {
    //$info = array('Name' => 'Jan', 'Alter' => '39', 'Jahrgang' => '1981', 'ID' => '3')
}

Another plus side to this method in your case is it makes sure each "Slug" is unique, because arrays cannot contain duplicate keys.

CodePudding user response:

You could use "function" instead of "nested foreach" like this:

        function replace($data,$index){
        
        // count array length
        $num = count($data);
    
        if($index < $num){
            if($data[$index]['Slug'] === 'jan'){
                
                $a = [ $index => [ "Jahrgang"  => "1982" ] ] ;
            
                // replace new array with new values
                $re = array_replace($data,$a);
                
                return $re;
    
            }else{
    
                $index   ;
                check($data,$index);
            }
        }
                
    
    }
    
    replace($data,0);

  • Related