-1

I had query for data in this SQL:

//query by location total post
$sql = 'SELECT ljj.job_id, count(ljj.job_id) as count, ljj.job_type FROM {local_jobs_job} ljj INNER JOIN {local_jobs_location} ljl ON ljj.job_location = ljl.location_id GROUP BY ljj.job_type';


//get the query into record
$data = $DB->get_records_sql($sql);

The output is here:

Array ( [1] => stdClass Object ( [job_id] => 1 [count] => 8 [job_type] => 0 ) [3] => stdClass Object ( [job_id] => 3 [count] => 7 [job_type] => 1 ) ) 

I need to change the value of:

[job_type] => 0 to [job_type] => 'Job' 
[job_type] => 1 to [job_type] => 'Internship'

I do not know how to get to the value as it is an object array.

How to get the value and replace it?

joun
  • 656
  • 1
  • 8
  • 25
  • You find here a solution https://stackoverflow.com/questions/5875785/how-to-access-stdclass-object-after-a-specific-key-value-pair –  Sep 19 '17 at 21:31
  • Could you give an example for my question? I do read your reference but still having problem. I'm not very good in multi arrays. Thanks if you can help. – joun Sep 19 '17 at 21:46
  • Sorry about time i was away few minutes example here ;), regards. –  Sep 19 '17 at 22:33

1 Answers1

1

Here a complet based on your example : So you got some 3 arrays in the ouput the first array add 2 childs object index 1, 3 the childs object got one array with 3 index literals (job_id, count, job_type) so array = [] and object = {}

So i cast to object (object) the 2 childs arrays and transform to an object to being a stdclass object as your example getting from database.

<?php
//Array ( [1] => stdClass Object ( [job_id] => 1 [count] => 8 [job_type] => 0 ) [3] => stdClass Object ( [job_id] => 3 [count] => 7 [job_type] => 1 ) );

$data = Array( 1=>(object)Array("job_id" => 1, "count" => 8, "job_type" => 0), 3 => (object)Array("job_id" => 3, "count" => 7, "job_type" => 1));


var_dump($data); // show your ouput

var_dump($data[1]->{'job_type'}); // index 1 to object attribute job_type = we got the value  "0"
var_dump($data[3]->{'job_id'}); // index 3 to object attribute job_type = value = "1"

//to change you have to affect your value so :
$data[1]->{'job_type'} = "Job";
$data[3]->{'job_type'} = "Internship";

var_dump($data[1]->{'job_type'}); // index 1 to object attribute job_type = we got the value  "job"
var_dump($data[3]->{'job_type'}); // index 3 to object attribute job_type = value = "Internship"
  • Thank you so much @headmax for you help and guidance. You save my day. Your answer works and I can change the value inside the object. And I also can understand better on multi arrays and objects. Many thanks to you. – joun Sep 20 '17 at 01:24
  • @joun Welcome joun ;) have a nice day. –  Sep 20 '17 at 06:24
  • 'thumbs up' :). Have a nice day too – joun Sep 20 '17 at 09:02