我在这个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);
输出在这里:
Array ( [1] => stdClass Object ( [job_id] => 1 [count] => 8 [job_type] => 0 ) [3] => stdClass Object ( [job_id] => 3 [count] => 7 [job_type] => 1 ) )
我需要更改:
的值[job_type] => 0 to [job_type] => 'Job'
[job_type] => 1 to [job_type] => 'Internship'
我不知道如何获取该值,因为它是一个对象数组。
如何获取值并替换它?
答案 0 :(得分:1)
这里有一个基于你的例子的complet: 所以你在输出中得到了一些 3个数组 第一个数组添加 2个子对象 索引1,3 < em> childs对象得到一个包含3个索引文字的数组( job_id,count,job_type ),所以 array = [] 和< strong> object = {}
所以我施放对象(对象) 2子阵列和转换到对象一个 stdclass对象作为从数据库获取的示例。
<?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"