2017-09-19 2 views
-1

hatte ich Abfrage für Daten in dieser SQL:Wie ändert man den Wert in Array-Objekten?

//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); 

Der Ausgang ist hier:

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

Ich weiß nicht, wie:

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

ich den Wert ändern müssen um zu dem Wert zu kommen, wie es ein Objekt-Array ist.

Wie bekomme ich den Wert und ersetze es?

+0

Sie finden hier eine Lösung https://stackoverflow.com/questions/5875785/how-to-access-stdclass-object-after-a-specific-key-value-pair –

+0

Können Sie ein Beispiel geben für meine Frage? Ich lese Ihre Referenz, habe aber immer noch Probleme. Ich bin nicht sehr gut in Multi-Arrays. Danke, wenn du helfen kannst. – joun

+0

Sorry über die Zeit Ich war weg einige Minuten Beispiel hier;), Grüße. –

Antwort

1

hier ein complet basierend auf Ihrem Beispiel: Also haben Sie einige 3 Arrays in der ouput der erste Array hinzufügen 2 Kinder ObjektIndex 1, 3 die Childs Objekt bekam ein Array mit 3 Index Literale (job_id, zählen, job_type) so array = [] und Object = {}

So i gegossen(Objekt) das 2 Kinder Arrays Objekt und zu sein ein stdClass auf ein Objekt verwandeln Objekt als Beispiel aus der Datenbank zu bekommen.

<?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" 
+0

Vielen Dank @headmax für Sie Hilfe und Anleitung. Du sparst meinen Tag. Ihre Antwort funktioniert und ich kann den Wert innerhalb des Objekts ändern. Und ich kann auch besser auf Multi-Arrays und Objekten verstehen. Vielen Dank an dich. – joun

+0

@joun Willkommen joun;) Einen schönen Tag noch. –

+0

"Daumen hoch" :). Haben Sie auch einen schönen Tag – joun

Verwandte Themen