0

I have an php array like this (var_dump) I need change one of it's element

  array (size=204)
          'Address' => 
            array (size=3)
              'City' => 
                array (size=3)
                  0 => string 'return $this->hasOne(City::className(), ['id' => 'cityId']);' 
                  1 => string 'City' (length=4)
                  2 => boolean false
              'CityDistrict' => 
                array (size=3)
                  0 => string 'return $this->hasOne(CityDistrict::className(), ['id' => 'cityDistrictId']);' (length=76)
                  1 => string 'CityDistrict' (length=12)
                  2 => boolean false
              'Contacts' => 
                array (size=3)
                  0 => string 'return $this->hasMany(Contact::className(), ['addressId' => 'id']);' 
                  1 => string 'Contact' (length=7)
                  2 => boolean true
          'City' => 
            array (size=3)
              'Addresses' => 
                array (size=3)
                  0 => string 'return $this->hasMany(Address::className(), ['cityId' => 'id']);' 
                  1 => string 'Address' (length=7)
                  2 => boolean true
              'Region' => 
                array (size=3)
                  0 => string 'return $this->hasOne(Region::className(), ['id' => 'regionId']);' (length=64)
                  1 => string 'Region' (length=6)
                  2 => boolean false
              'CityDistricts' => 
                array (size=3)
                  0 => string 'return $this->hasMany(CityDistrict::className(), ['cityId' => 'id']);' 
                  1 => string 'CityDistrict' (length=12)
                  2 => boolean true
          'CityDistrict' => 
            array (size=2)
              Addresses => 
                array (size=3)
                  0 => string 'return $this->hasMany(Address::className(), ['cityDistrictId' => 'id']);' 
                  1 => string 'Address' (length=7)
                  2 => boolean true
              'City' => 
                array (size=3)
                  0 => string 'return $this->hasOne(City::className(), ['id' => 'cityId']);'
                  1 => string 'City' (length=4)
                  2 => boolean false

How can i change value 'CityDistrict' in this loop? or 'Addresses'? using php foreach My code doesn't work please help understand what wrong!

  private static function checkExistClass($relations)
    {
        foreach ($relations as $name => $relation) {
            foreach ($relation as $functionName => $functionValue) {
                $functionNameGet = 'get' . $functionName;
                $directory = new Model;
                if (method_exists($directory, $functionNameGet)) {
                    $relation['funky_key_' . $functionName] = $functionValue;
                    unset($relation[$functionName]);
                }
            }
        }
        return $relations;
    }

3 Answers3

0

I interprete your question that you want to rename the array index Addresses to NewAddresses:

$relations['CityDistrict']['NewAddresses'] = $relations['CityDistrict']['Addresses'];
unset($relations['CityDistrict']['Addresses']);

EDIT: to do that in your foreach loop, change:

$relation['funky_key_' . $functionName] = $functionValue;
unset($relation[$functionName]);

to:

$relations[$name]['funky_key_'.$functionName] = $functionValue;
unset($relations[$name][$functionName]);
hellcode
  • 2,678
  • 1
  • 17
  • 21
0

maybe this is what you're looking

if (isset($array['n'])) {
  $array['name'] = $array['n'];
  unset($array['n']);
}

you can see the complete post in Change key in associative array in PHP

see you!

PD Sorry, for my english is not the best

Community
  • 1
  • 1
0

Your loop seems wrong. In the outer loop, $name assumes values such as 'Address', and $relation is an array such as { 'City' => ..., 'CityDistrict' => ... }.

So in the second loop $functionName assumes values such as City, CityDistrict and Contacts.

If you want to change that, you need to do something like @hellcode suggested:

if ('CityDistrict' == $functionName) {
    $relations[$name]['NewDistrict'] = $relations[$name][$functionName];
    unset($relations[$name][$functionName]);
    continue;
}

This looks like a Laravel/Eloquent problem to me. If you can state more precisely what it is that you're trying to accomplish, possibly someone could be of more use.

Also, you seem to want to create a function given its code in a string. To do this you would need create_function (or declare the function as anonymous/lambda function):

$code = "return 42;";

$array['function'] = create_function('', $code);

print $array['function']();

Note that the use of create_function is somewhat deprecated. Also you need a PHP > 5.3+ (or 5.4+ if you go lambda and require $this).

LSerni
  • 55,617
  • 10
  • 65
  • 107