Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array mit reserviertem Wort

0 views
Skip to first unread message

hamburger

unread,
Jun 28, 2009, 9:57:33 AM6/28/09
to
Hallo,
ich bekomme über eine json-Datei ein Array mit der Bezeichnung use.
Wenn ich dieses wie folgt ausgeben möchte kommt nichts.

$html .= "<tr><td><b>use:</b></td><td>" . $loan->{sector}->{'use'} .
"</td></tr>";

Frage:gibts da einen Trick? (backslash o.ä. vorstellen?)
Die exteren json-Datei kann ich nicht ändern.

Thy in advance.

Hadanite Marasek

unread,
Jun 28, 2009, 11:07:40 AM6/28/09
to
hamburger schrieb:
> Hallo,
> ich bekomme �ber eine json-Datei ein Array mit der Bezeichnung use.
> Wenn ich dieses wie folgt ausgeben m�chte kommt nichts.

>
> $html .= "<tr><td><b>use:</b></td><td>" . $loan->{sector}->{'use'} .
> "</td></tr>";
>
> Frage:gibts da einen Trick? (backslash o.�. vorstellen?)
> Die exteren json-Datei kann ich nicht �ndern.

Das sollte kein Problem sein:

$loan = new stdClass();
$loan->sector = new stdClass();
$loan->sector->use = "foo";
echo $loan->{'sector'}->{'use'};

ergibt 'foo'. (PHP 5.2.x)

Ist sector in Deinem Fall nicht ein Array? Dann $loan->sector["use"].
Ansonsten schick doch mal ein var_dump oder print_r.

Und die Syntax mit {} f�r Properties war mir bislang abgegangen...wieder
was gelernt!

Michael Meyer

unread,
Jun 28, 2009, 11:55:58 AM6/28/09
to
*** hamburger <bili...@web.de> wrote:

> ich bekomme �ber eine json-Datei ein Array mit der Bezeichnung use.
> Wenn ich dieses wie folgt ausgeben m�chte kommt nichts.


>
> $html .= "<tr><td><b>use:</b></td><td>" . $loan->{sector}->{'use'} .
> "</td></tr>";

Da du sehr sparsam mit relevanten Informationen bist, versuche ich es
mal mit raten.

<?php

$json = '{"sector":{"use":"HALLO"}}';
$loan = json_decode($json, TRUE); // TRUE, um ein assoziatives Array zu bekommen


$html .= "<tr><td><b>use:</b></td><td>" . $loan->{sector}->{'use'} . "</td></tr>";

$html1 .= "<tr><td><b>use:</b></td><td>" . $loan['sector']['use'] . "</td></tr>";

print "HTML: " . $html;
print "\n";
print "HTML1: " . $html1;

?>

Ergibt:

HTML: <tr><td><b>use:</b></td><td></td></tr>
HTML1: <tr><td><b>use:</b></td><td>HALLO</td></tr>

Micha

hamburger

unread,
Jun 29, 2009, 6:06:29 AM6/29/09
to
On 28 Jun., 17:55, Michael Meyer <m...@gmx.de> wrote:
> *** hamburger <bilidi...@web.de> wrote:
> > ich bekomme über eine json-Datei ein Array mit der Bezeichnung use.
> > Wenn ich dieses wie folgt ausgeben möchte kommt nichts.

>
> > $html .= "<tr><td><b>use:</b></td><td>" . $loan->{sector}->{'use'} .
> > "</td></tr>";
>
> Da du sehr sparsam mit relevanten Informationen bist, versuche ich es
> mal mit raten.
>
> <?php
>
>  $json = '{"sector":{"use":"HALLO"}}';
>  $loan = json_decode($json, TRUE); // TRUE, um ein assoziatives Array zu bekommen
>  $html  .= "<tr><td><b>use:</b></td><td>" . $loan->{sector}->{'use'} . "</td></tr>";
>  $html1 .= "<tr><td><b>use:</b></td><td>" . $loan['sector']['use'] . "</td></tr>";
>
>  print "HTML: " . $html;
>  print "\n";
>  print "HTML1: " . $html1;
>
> ?>
>
> Ergibt:
>
> HTML: <tr><td><b>use:</b></td><td></td></tr>
> HTML1: <tr><td><b>use:</b></td><td>HALLO</td></tr>
>
> Micha

Zunächst Danke für Eure Antwort.

$html1 .= "<tr><td><b>use:</b></td><td>" . $loan['sector']['use'] .
"</td></tr>";

funktioniert leider nicht.
Ich hole weiter aus:
hier mein json:

{"lending_actions":[

{"id":16113424,"date":"2009-06-26T16:06:13Z","loan":{"id":
118782,"name":"Emma Metuda","status":"fundraising","loan_amount":
150,"funded_amount":0,"basket_amount":0,"borrower_count":1,"image":
{"id":344096,"template_id":1},"activity":"General
Store","sector":"Retail","use":"To use as additional capital for her
sari-sari store","location":
{"country_code":"PH","country":"Philippines","town":"Pan-
ay ,clarin,misamis Occidental","geo":{"level":"town","pairs":"0
0","type":"point"}},"partner_id":
126,"posted_date":"2009-06-26T15:50:06Z","description":{"languages":
["en","en"]}},"lender":
{"lender_id":"pedro2672","name":"Pedro","whereabouts":"S. Domingos de
Rana","country_code":"PT","uid":"pedro2672"}},

{"id":16113423,"date":"2009-06-26T16:06:08Z","loan":{"id":
117887,"name":"Olena Popova","status":"fundraising","loan_amount":
2000,"funded_amount":0,"basket_amount":0,"borrower_count":1,"image":
{"id":342327,"template_id":1},"activity":"Cosmetics
Sales","sector":"Retail","use":"to increase her inventory of
cosmetics","location":
{"country_code":"UA","country":"Ukraine","town":"Kherson","geo":
{"level":"town","pairs":"46.633333 32.6","type":"point"}},"partner_id":
26,"posted_date":"2009-06-23T20:30:04Z","description":{"languages":
["en","en"]}},"lender":{"lender_id":"iyad7331","name":"Iyad","image":
{"id":240974,"template_id":1},"whereabouts":"San Jose
CA","country_code":"US","uid":"iyad7331"}}, ...

Hier mein php Code zum auslesen:

$url = "http://api.kivaws.org/v1/loans/newest.json";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($ch);
curl_close ($ch);
$results = json_decode($response);
....
$html .= "<tr><td><b>Location:</b></td><td>". $loan->
{location}->{country} . ", " . $loan->{location}->{town} . "</td></
tr>"; //geht
$html .= "<tr><td><b>Activity:</b></td><td>" . $loan->
{activity} . "</td></
tr>"; //
geht
$html .= "<tr><td><b>loan_amount:</b></td><td>" . $loan->
{loan_amount} . "</td></
tr>"; //geht
$html .= "<tr><td><b>sector:</b></td><td>" . $loan->{sector} .
"</td></
tr>"; //
geht

//$html .= "<tr><td><b>use1:</b></td><td>" . $loan->{sector}->
{use} . "</td></
tr>"; //
geht nicht Ausgabe wird ignoriert
// $html .= "<tr><td><b>use2:</b></td><td>" .$loan['sector']
['use']. "</td></
tr>"; //
Fehlermeldung Cannot use object of type stdClass as array


funktioniert alles wunderbar, bis auf use. Hier zeigt mir mein Editor
an, dass dies ein reserviertes Wort ist und die Ausgabe wird ohne
Fehlermeldung ignoriert.

Many tanks für weitere Vorschläge

hamburger

unread,
Jun 29, 2009, 6:14:19 AM6/29/09
to
> Many tanks  für weitere Vorschläge- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Sorry Fehler: use steht natürlich unter activity
$html .= "<tr><td style='vertical-align:top'><b>use1:</b></
td><td>" . $loan->{activity}->{use} . "</td></tr>";
trägt der sache aber nicht bei, ES GEHT NICHT.

Michael Meyer

unread,
Jun 29, 2009, 7:16:50 AM6/29/09
to
*** hamburger <bili...@web.de> wrote:
> $url = "http://api.kivaws.org/v1/loans/newest.json";
>
> $ch = curl_init();
> curl_setopt($ch, CURLOPT_URL, $url);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
> $response = curl_exec ($ch);
> curl_close ($ch);
> $results = json_decode($response);
> ....
> $html .= "<tr><td><b>Location:</b></td><td>". $loan->
> {location}->{country} . ", " . $loan->{location}->{town} . "</td></
> tr>"; //geht

Wo kommt bei dir nun $loan her? Naja, egal.

<?php

$url = "http://api.kivaws.org/v1/loans/newest.json";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($ch);
curl_close ($ch);
$results = json_decode($response);

foreach ($results->{'loans'} as $val) {
print $val->{'use'} . "\n";
}


?>

Ergibt:

Increasing merchandise
Investing in the purchase of material for making fishing nets
Purchase of office supplies: notebooks, paper, pens, folders, etc.
Purchase merchandise and raw materials
To purchase raw materials to manufacture additional products
[...]

Micha

hamburger

unread,
Jun 29, 2009, 10:12:14 AM6/29/09
to
On 29 Jun., 13:16, Michael Meyer <m...@gmx.de> wrote:

Hallo Micha,
Danke für die Antwort.
Hab ich leider nicht kapiert.
Wie bekomme ich das in meinen Code.
for each ... brauch ich nicht. Ich will ein bestimtes ansprechen.
Und .. warum geht ->{'use'} in Anführungzeichen bei dir und bei mir
nicht?

Michael Meyer

unread,
Jun 29, 2009, 10:34:53 AM6/29/09
to
*** hamburger <bili...@web.de> wrote:

> Wie bekomme ich das in meinen Code.

Keine Ahnung. Ich kenne deinen Code nicht. Der scheint ja irgendwie
geheim zu sein.

> for each ... brauch ich nicht. Ich will ein bestimtes ansprechen.

Welches genau das ist, willst du aber nicht verraten?

> Und .. warum geht ->{'use'} in Anf�hrungzeichen bei dir und bei mir
> nicht?

Kann ich Hellsehen? Solange dein Code so geheim ist, wird das eines von
vielen R�tseln bleiben die du einem hier so aufgibst. ;-)

"Geht nicht" ist btw. recht wenig aussagekr�ftig...

Micha

hamburger

unread,
Jun 29, 2009, 2:01:29 PM6/29/09
to
On 29 Jun., 16:34, Michael Meyer <m...@gmx.de> wrote:

> *** hamburger <bilidi...@web.de> wrote:
> > Wie bekomme ich das in meinen Code.
>
> Keine Ahnung. Ich kenne deinen Code nicht. Der scheint ja irgendwie
> geheim zu sein.
>
> > for each ... brauch ich nicht. Ich will ein bestimtes ansprechen.
>
> Welches genau das ist, willst du aber nicht verraten?
>
> > Und .. warum geht ->{'use'} in Anführungzeichen bei dir und bei mir

> > nicht?
>
> Kann ich Hellsehen? Solange dein Code so geheim ist, wird das eines von
> vielen Rätseln bleiben die du einem hier so aufgibst. ;-)
>
> "Geht nicht" ist btw. recht wenig aussagekräftig...
>
> Micha

Hallo,
verstehe nicht wieso mein Code geheim sein soll. Was m.E. relevant ist
habe ich bereits gepostet.
Wollte mich bisher nur auf das Wesentliche beschränken. Hier mehr:

function get_kiva_loans(){
global $number_of_loans;
global $image_size;
$limit = $number_of_loans;

if($limit < 1){
$limit = 1;
}
$size = $image_size;
if($size > 200){
$size = 200;
}
//$username = $options['kiva_username'];
global $username;
$content = "";
if(! $username){
$content = "<p>No user specified!</p>";
}else{
$results = "";
if(is_current_kiva_cache_available()){
$results = get_kiva_cache();
}

if($results == ""){
//$url = "http://api.kivaws.org/v1/lenders/$username/
loans.json";
$url = "http://api.kivaws.org/v1/loans/newest.json";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($ch);
curl_close ($ch);
$results = json_decode($response);

write_kiva_cache($response);
}
$loans_array = $results->{'loans'};
$total_loans = sizeof($loans_array);

if($total_loans <= 0){
$content = "<p>User has no loans!</p>";
}else{
$content = "<p>I have microlent to:</p>";

// Make this randomely choose one
$loan_count = 0;
$used = array_fill(0, $total_loans, 0);
for($i = 0; $i < $total_loans; $i++){
if($i < $limit){
$j = rand(0, $total_loans-1 );
while ($used[$j]) {
$j = rand(0, $total_loans-1 );
}
$used[$j] = 1;
$loan = $loans_array[$j];
$content .= build_html($loan, $size);
$loan_count++;
}else{
break;
}
}
}
}

$html = "<div id='kiva_loans'>";
$html .= $content;
$html .= "<center><a href='http://kiva.org/'><img
src='kiva.gif' alt='Kiva.org' class='kiva_logo' /></a></center>";
$html .= "</div>";
return $html;
}


function build_html($loan, $size){
global $kiva_cache_dir;
global $kiva_cache_path;

$html = "<link rel='stylesheet' href='kiva.css' type='text/
css' media='screen' />";
$html .= "<div class='loan'>";
$html .= "<h3>" . $loan->{name}. "</h3><br />";
echo "ich1 :".$kiva_cache_dir."<br>";

$image_path = "$kiva_cache_dir/" . $loan->{'image'}->{'id'} .
".jpg";
echo "ich2 :".$image_path."<br>";
$image_src = "$kiva_cache_path/" . $loan->{'image'}->{'id'} .
".jpg";

if(! file_exists($image_path)){
$url = "http://s3.kiva.org/img/w200h200/" . $loan->
{'image'}->{'id'} . ".jpg";
# Get and save the image


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$image = curl_exec ($ch);
curl_close ($ch);

$fh = fopen($image_path, 'w') or die("can't open file: " .
$image_path);
fwrite($fh, $image);
fclose($fh);
}else{
# Check for moved images for those empty images already
retrieved
$fh = fopen($image_path, 'r');
$image = fread($fh, filesize($image_path));
fclose($fh);

$matches = preg_match("/302 Found/s", $image);

if($matches){
$movedImage = preg_match("/href=[\"']([^\"']+)[\"']/",
$image, $movedImageURL);
if($movedImage){
# Get and save the moved image
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $movedImageURL[1]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$image = curl_exec ($ch);
curl_close ($ch);

$fh = fopen($image_path, 'w') or die("can't open
file: " . $image_path);
fwrite($fh, $image);
fclose($fh);
}
}
}

$style = "max-height:".$size."px;max-width:".
$size."px;width:expression(this.width > ".$size." ? \"".$size."px\" :
this.width); height:expression(this.height > ".$size." ? \"".$size."px
\" : this.height);";
$html .= "<a href='http://www.kiva.org/app.php?
page=businesses&action=about&id=" . $loan->{id} . "'
target='_new'><img src='$image_src' alt='". $loan->{name} . "'
style='".$style."' /></a><br />";
$html .= "<table>";
$html .= "<tr><td style='vertical-align:top'><b>Location:</b></


td><td>". $loan->{location}->{country} . ", " . $loan->{location}->
{town} . "</td></tr>";

$html .= "<tr><td style='vertical-align:top'><b>Activity:</b></


td><td>" . $loan->{activity} . "</td></tr>";

$html .= "<tr><td style='vertical-align:top'><b>loan_amount:</


b></td><td>" . $loan->{loan_amount} . "</td></tr>";

$html .= "<tr><td style='vertical-
align:top'><b>funded_amount:</b></td><td>" . $loan->{funded_amount} .
"</td></tr>";
$html .= "<tr><td style='vertical-align:top'><b>sector:</b></


td><td>" . $loan->{sector} . "</td></tr>";

$html .= "<tr><td style='vertical-align:top'><b>use1:</b></

td><td>" . $loan->{activity}->{use} . "</td></tr>";
// $html .= "<tr><td style='vertical-align:top'><b>use2:</b></


td><td>" .$loan['sector']['use']. "</td></tr>";

$html .= "<tr><td style='vertical-align:top'><b>loan_amount:</


b></td><td>" . $loan->{loan_amount} . "</td></tr>";

$html .= "</table><br />";
$html .= "</div>";

return $html;
}


Ich möchte eine Anzeige für Kiva auf meiner zukünftigen Hompepage
zeigen. www.Kiva.org vergibt microcredits und verdient es unterstützt
zu werden.

Michael Meyer

unread,
Jun 29, 2009, 2:31:48 PM6/29/09
to
*** hamburger <bili...@web.de> wrote:

> $loan->{activity}->{use}

$loan->{'use'}

Micha

hamburger

unread,
Jun 29, 2009, 4:59:12 PM6/29/09
to
On 29 Jun., 20:31, Michael Meyer <m...@gmx.de> wrote:

> *** hamburger <bilidi...@web.de> wrote:
> > $loan->{activity}->{use}
>
> $loan->{'use'}
>
> Micha

Hallo Micha,
Danke nochmals für deinen Kommentar.
Frage: Besteht dein Kommentar nur aus der einen Zeile, oder fehlt da
was?
$loan->{'use'} .. sagt mir leider nichts.
Gruß

Michael Meyer

unread,
Jun 30, 2009, 4:54:22 AM6/30/09
to
*** hamburger <bili...@web.de> wrote:

> On 29 Jun., 20:31, Michael Meyer <m...@gmx.de> wrote:
> > *** hamburger <bilidi...@web.de> wrote:
> > > $loan->{activity}->{use}
> >
> > $loan->{'use'}
> >

> $loan->{'use'} .. sagt mir leider nichts.

*seufz*.

Du m�chtest in deiner Funktion 'build_html()' '$loan->{activity}->{use}'
ersetzen mit '$loan->{'use'}'.

$html .= "<tr><td style='vertical-align:top'><b>use1:</b></td><td>" .

$loan->{'use'} . "</td></tr>";

Micha

hamburger

unread,
Jun 30, 2009, 8:24:19 AM6/30/09
to
On 30 Jun., 10:54, Michael Meyer <m...@gmx.de> wrote:
> *** hamburger <bilidi...@web.de> wrote:
> > On 29 Jun., 20:31, Michael Meyer <m...@gmx.de> wrote:
> > > *** hamburger <bilidi...@web.de> wrote:
> > > > $loan->{activity}->{use}
>
> > > $loan->{'use'}
>
> > $loan->{'use'}     .. sagt mir leider nichts.
>
> *seufz*.
>
> Du möchtest in deiner Funktion 'build_html()' '$loan->{activity}->{use}'

> ersetzen mit '$loan->{'use'}'.
>
> $html .= "<tr><td style='vertical-align:top'><b>use1:</b></td><td>" .
>           $loan->{'use'} . "</td></tr>";
>
> Micha

Hallo Micha,
seufz, im wahrsten Sinne und Scheiße wenn man doof ist.
Hab' leider übersehen, dass use einfach nur zu loan gehört. (Mein
Editor hat use als reserviertes Wort angemeckert und mich auf die
falsche Fährte geführt.)
War trotzdem schön und vor allem Produktiv mit dir zusammen zu
arbeiten.
Nochmals Danke
Gruß (im übrigen aus Düsseldorf, nicht aus Hamburg)

0 new messages