1

I have spent the whole day since morning to handle this without the solution.

I have these data comes from database. I use PHP PDO connection.

Array (
 [0] => stdClass Object ( [testing_id] => 4 [testing_name] => please [testing_location] => kjnkdsnkdnskjndkjsndjknskdnsk )
 [1] => stdClass Object ( [testing_id] => 3 [testing_name] => please [testing_location] => jknds ndns )
 [2] => stdClass Object ( [testing_id] => 2 [testing_name] => please [testing_location] => be done to me ) )

I want to rename keys in objects instead of testing_id to be just id, testing_name to be name etc.

I have write number of functions like this below

function remove_keys($arr, $table) {
  $object = new stdClass();
  foreach ($arr as $key => $val) {
    $x = (array) $val;
    foreach ($x as $key2 => $value) {
      $new_key = str_replace($table, '', $key2);
      $object->$new_key = $value;
    }
  }
  return $object;
}

and this

function replaceKey(&$array,$table) {    
  $x = array();
  foreach($array as $k => $v){           
    $new_key = str_replace($table, '', $k);
    array_push($x, $new_key);
  } 
  $array = array_combine($x, $array);
  return $array;
}

In all cases, I get only one object result instead of renaming the whole object

stdClass Object ( [id] => 2 [name] => please [location] => be done to me )

How can I rename each index in object and get the full object renamed? Any help please

I need the output to be like this

Array ( 
  [0] => stdClass Object ( [id] => 4 [name] => please [location] => kjnkdsnkdnskjndkjsndjknskdnsk )
  [1] => stdClass Object ( [id] => 3 [name] => please [location] => jknds ndns ) 
  [2] => stdClass Object ( [id] => 2 [name] => please [location] => be done to me ) )

I have searched here without any similar solution

3
  • PDO supports selecting data into your own class. You should create your class with correct field names (eg. id,name,location) and use $statement>setFetchMode(PDO::FETCH_CLASS, 'your_class');. See This for a complete example. Commented Aug 10, 2014 at 21:38
  • If changing to FETCH_CLASS is not an option (for whatever reason) you can also change your query to rename fields, for example SELECT testing_id as id,testing_name as name,testing_location as location FROM testing_table WHERE [...]; Commented Aug 10, 2014 at 21:45
  • Thanks, but can't I handle it in php codes. I have a database with more than 30 tables, create class for each one will be a great pain Commented Aug 10, 2014 at 21:45

1 Answer 1

1

You are overwriting the object value in the foreach:

$object->$new_key = $value;

$object is always the same variable, try something like this:

function remove_keys($arr, $table) {
  $temp_array = array();
  foreach ($arr as $key => $val) {
    $object = new stdClass();
    $x = (array) $val;
    foreach ($x as $key2 => $value) {
      $new_key = str_replace($table, '', $key2);
      $object->$new_key = $value;
    }
    $temp_array[] = $object;
  }
  return $temp_array;
}

This will give you back an array of objects.

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

1 Comment

Greeeeet, thanks Ende Neu, It just work fine.... I can't believe if it was just a trick like that...Thanks sana

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.