manipulation les données d'un array avec foreach et for
Bonjour,
Je travaille sur avec un array que je ne connais pas son contenu.
Alors j'ai fait
Code:
print_r($this->info);
pour savoir ce qu'il y a dedans et j'obtiens :
Code:
1 2 3 4 5 6 7 8
| Array (
[0] => stdClass Object (
[id] => 16508AA
[quantity] => 1
[name] => toto
)) |
alors je vais récupérer cela sous forme :
Citation:
[{
'id': '16508AA',
'quantity': 1
'name': 'toto'
}]
et je fais ainsi :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| $construction1 = " [";
$construction2 = "{";
foreach($this->info as $product)
{
$construction2.= "
'id': '" . $membret->visite_code .
"',
'quantity': " . $membret->visite_name .
",
'name': '" . $membret->visite_name .
"'
";
}
$construction2.= "
}";
$construction3= "]
}];";
echo $construction1 . $construction2 . $construction3; |
et tout va bien je l'obtiens comme je voulais.:D
Citation:
[{
'id': '16508AA',
'quantity': 1,
'name': 'toto'
}]
par contre lorsqu'il y deux enregistrements, il faut que je mette "},{" entre les deux :8O
ou bien
Code:
$entreDeuxEngregistrement = " },{ ";
Pour ne pas avoir ::oops:
Citation:
[{
'id': '16508AA',
'quantity': 1,
'name': 'toto'
'id': '16509AA',
'quantity': 10,
'name': 'tata'
}]
mais je m'aimerais obtenir :8O
Citation:
[{
'id': '16508AA',
'quantity': 1,
'name': 'toto'
},{
'id': '16509AA',
'quantity': 10,
'name': 'tata'
}]
Alors j'ai mis une boucle for dans ma boucle foreach mais:?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| foreach($this->info as $product)
{
$construction2.= "
'id': '" . $membret->visite_code .
"',
'quantity': " . $membret->visite_name .
",
'name': '" . $membret->visite_name .
"'
";
$entreDeuxEngregistrement = "";
$combienArticle = sizeof($this->info);
for($i=1;
$i<$combienArticle;
$i++)
{
$entreDeuxEngregistrement = "},{";
}
}
$construction2.= $entreDeuxEngregistrement . "
}";
$construction3= "]
}];";
echo $construction1 . $construction2 . $construction3; |
mais j'obtiens :
Citation:
[{
'id': '16508AA',
'quantity': 1,
'name': 'toto'
'id': '16509AA',
'quantity': 10,
'name': 'tata'
},{
}]
Comment peux-je les mettre entre deux enregistrements ?:roll:
Merci