php foreach with a variable - php

I'm trying to use a variable in a foreach loop (with simplexml) but am confused as to how to get it work properly.
My variable is:
$path="channel->item";
And I want my foreach to look like:
foreach ($xml->".$path." as $newsItem)
But that doesn't work - like the $path is being echo as $path rather than it's contents.
if I do:
foreach ($xml->channel->item as $newsItem)
It works fine.
I'm sure it's just a syntax issue but I can't figure it out. Thanks in advance.

You can't include pointers in the variable variable.
$members = explode('->', $path);
foreach($xml->{$members[0]}->{$members[1]} as $v) {
}
This will only work if your path stays two dimensional.
[edit]
If you do need it to work recursively you can use this. Sorry took me a minute to write it:
function pathFinder($path, $obj) {
$members = explode('->', $path);
if(is_object($obj) && count($members) && property_exists($obj, $members[0])) {
$nextMember = $members[0];
array_shift($members);
return pathFinder(implode('->', $members), $obj->{$nextMember});
} else {
return $obj;
}
}
$xml = new stdClass;
$xml->channel->item[] = 'love';
$xml->channel->item[] = 'test';
$path = 'channel->item';
$array = pathFinder($path, $xml);
print_r($array);
output:
Array(
[0] => love
[1] => test
)

Try something like this:
#Object for test
$xml = new stdClass();
$xml->channel->item[] = "banana";
$xml->channel->item[] = "abacate";
$xml->channel->item[] = "manga";
$xml->channel->item[] = "abacaxi";
$xml->channel->item[] = "morango";
list($channel, $item)= explode('->','channel->item');
foreach ($xml->{$channel}->{$item} as $newsItem) :
var_dump($newsItem);
endforeach;

Related

How to trim object properties in PHP?

I have an object $obj as
$obj->{' Property1'} = " value1";
$obj->{'Property2 '} = "value2 ";
I want to get this object $obj as
$obj->{'Property1'} = "value1";
$obj->{'Property2'} = "value2";
I am able to trim all the values using
foreach($obj as $prop => &$val)
{
$val = trim($val);
}
but doing this (below) causing an error
foreach($obj as &$prop => &$val)
{
$prop = trim($prop);
$val = trim($val);
}
Please tell a solution.
Thanks in advance.
You can't reference a key.
What you have to do is unset it, and set the trimmed version like this:
<?php
$obj = new stdClass;
$obj->{' Property1'} = " value1";
foreach($obj as $prop => $val)
{
unset($obj->{$prop});
$obj->{trim($prop)} = trim($val);
}
var_dump($obj);
A little comment to Daan's answer. In his case script will fall into infinite loop if $obj have more than one property. So a working code looks like this.
<?php
$obj = new stdClass;
$obj->{' Property1'} = " value1";
$obj->{'Property2 '} = "value2 ";
$newObj = new stdClass;
foreach($obj as $prop => $val)
{
$newObj->{trim($prop)} = trim($val);
}
$obj = $newObj;
unset($newObj);
var_dump($obj);
Because you're trying to trim the property of the object. You can't do that.
That would work for an array, but not for an object. If you need to alter the properties of an object you need to change the properties of the class.

PHP Array & XML Can't get all content

I'm tring to get all content from this xml: https://api.eveonline.com/eve/SkillTree.xml.aspx
To save it on a MySQL DB.
But there are some data missing...
Could any1 that understand PHP, Array() and XML help me, please?
This is my code to get the content:
<?php
$filename = 'https://api.eveonline.com/eve/SkillTree.xml.aspx';
$xmlbalance = simplexml_load_file($filename);
$skills = array();
for ($x=0;$x<sizeOf($xmlbalance->result->rowset->row);$x++) {
$groupName = $xmlbalance->result->rowset->row[$x]->attributes()->groupName;
$groupID = $xmlbalance->result->rowset->row[$x]->attributes()->groupID;
for ($y=0;$y<sizeOf($xmlbalance->result->rowset->row[$x]->rowset->row);$y++) {
$skills[$x]["skillID"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->attributes()->typeID;
$skills[$x]["skillName"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->attributes()->typeName;
$skills[$x]["skillDesc"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->description;
$skills[$x]["skillRank"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rank;
$skills[$x]["skillPrimaryAtr"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->requiredAttributes->primaryAttribute;
$skills[$x]["skillSecondAtr"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->requiredAttributes->secondaryAttribute;
$o = 0;
for ($z=0;$z<sizeOf($xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rowset->row);$z++) {
if ($xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rowset->attributes()->name == "requiredSkills") {
$skills[$x]["requiredSkills"]["".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rowset->row[$z]->attributes()->typeID] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rowset->row[$z]->attributes()->skillLevel;
$o++;
}
}
}
}
echo '<pre>'; print_r($skills); echo '</pre>';
?>
If you go to the original XML (link), at line 452, you will see:
<row groupName="Spaceship Command" groupID="257">
And that isn't show in my array (link)...
That is one thing that i found that is missing...
I think that probally have more content that is missing too..
Why? How to fix it, please?
Thank you!!!
You will only get a total of sizeof($xmlbalance->result->rowset->row) records. Because, in your 2nd for loop, you are basically storing your result in the same array element that is $skills[$x].
Try this (I also higly encourage you to be as lazy as you can when you write code - by lazy I mean, avoid repeating / rewriting the same code over and over if possible) :
$filename = 'https://api.eveonline.com/eve/SkillTree.xml.aspx';
$xmlbalance = simplexml_load_file($filename);
$skills = array();
foreach ($xmlbalance->result->rowset->row as $row)
{
$groupName = $row->attributes()->groupName;
$groupID = $row->attributes()->groupID;
foreach ($row->rowset->row as $subRow)
{
$skill['skillID'] = (string) $subRow->attributes()->typeID;
$skill['skillName'] = (string) $subRow->attributes()->typeName;
$skill['skillDesc'] = (string) $subRow->description;
$skill['skillRank'] = (string) $subRow->rank;
$skill['skillPrimaryAtr'] = (string) $subRow->requiredAttributes->primaryAttribute;
$skill['skillSecondAtr'] = (string) $subRow->requiredAttributes->secondaryAttribute;
foreach ($subRow->rowset as $subSubRowset)
{
if ($subSubRowset->attributes()->name == 'requiredSkills')
{
foreach ($subSubRowset->row as $requiredSkill)
{
$skill['requiredSkills'][(string) $requiredSkill->attributes()->typeID] = (string) $requiredSkill['skillLevel'];
}
}
}
$skills[] = $skill;
}
}
print_r($skills);

Foreach Array not being created correctly

I am generating an array using a foreach like so...
<?php
$docs = array();
$media = get_attached_media('image');
foreach($media as $medias) {
$docs[] = $medias->guid;
}
$images = serialize(array('docs' => $docs));
print_r($images);
?>
The output I am getting is...
a:1:{s:4:docs";a:3:{i:0;s:62:"http://www.example.com/image1.jpg";i:1;s:62:"http://www.example.com/image2.jpg";i:2;s:62:"http://www.example.com/image3.jpg";}}"
But what I need is...
a:1:{s:4:"docs";a:4:{i:0;a:1:{s:15:"property_imgurl";s:35:"http://wwww.example.com/image1.jpg";}i:1;a:1:{s:15:"property_imgurl";s:35:"http://wwww.example.com/image2.jpg";}i:2;a:1:{s:15:"property_imgurl";s:35:"http://wwww.example.com/image3.jpg";}i:3;a:1:{s:15:"property_imgurl";s:35:"http://wwww.example.com/image4.jpg";}}}
Where am I going wrong?
It looks like your expecting $medias->guid to be an array, but it's a string. I believe your going to need to provide an array value when pushing into your array. This should work for you:
$docs = array();
$media = get_attached_media('image');
foreach($media as $medias) {
$docs[] = array("property_imgurl" => $medias->guid);
}
$images = serialize(array('docs' => $docs));
print_r($images);

PHP Rename object property while maintaining its position

How can I rename an object property without changing the order?
Example object:
$obj = new stdClass();
$obj->k1 = 'k1';
$obj->k2 = 'k2';
$obj->k3 = 'k3';
$obj->k4 = 'k4';
Example rename by creating new property:
$obj->replace = $obj->k3;
unset($obj->k3);
Result:
( k1=>k1, k2=>k2, k4=>k4, replace=>k3 )
See the problem?
Simply recreating the object property causes the order to change.
I had a similar issue with arrays and came up with this solution:
Implemented solution for arrays
function replaceKey(&$array, $find, $replace)
{
if(array_key_exists($find, $array)){
$keys = array_keys($array);
$i = 0;
$index = false;
foreach($array as $k => $v){
if($find === $k){
$index = $i;
break;
}
$i++;
}
if ($index !== false) {
$keys[$i] = $replace;
$array = array_combine($keys, $array);
}
}
}
Looking for something similar to this but a function that will work on objects.
Can't seem to find any documentation on object property position indexes.
So, readd your properties
$obj = new YourClass();
$backup = [];
foreach ($obj as $key => $value)
{
$backup[$key] = $value;
unset($obj->$key);
}
replaceKey($backup, $find, $replace);
foreach ($backup as $key => $value)
{
$obj->$key = $value;
}
Since you have a function for your arrays, why dont you do something like :
$array = (array) $obj;
$replacedKeyArray = replaceKey($array);
$newObject = (object) $replacedKeyArray;

PHP string to nested array

I need to convert a string from an ldap query. I am querying my Active Directory server for user accounts. This is the string that I pulled.
"CN=Phil Robertson,OU=Users,OU=Duck Commander,OU=Department & Buildings,DC=OCSDtest,DC=local"
I would want it converted to an array that looks like this
$array['local']['OCtest']['Department & Buildings']['Duck Commander']['Users']['Phil Robertson']=1;
NOT
$array( [1]=>'local,[2]=>'OCtest',[3]='Depart',[4]='Duck Commander',[5]='Users');
So far I have
Example code ---
$dnn2 = ldap_explode_dn("CN=Phil Robertson,OU=Users,OU=Duck Commander,OU=Department & Buildings,DC=OCSDtest,DC=local",1);
unset($dnn2['count']);
echo "<pre>";
print_r(array_reverse($dnn2));
What am I needing?
Try this
$arrayvalue = array();
foreach($dnn2 as $dn)
{
$temp = explode('=',$dn);
$temp1 = substr($temp[1], 0, strpos($temp1[1], ','));
$arrayvalue[] = $temp1;
}
print_r($arrayvalue);
$dnn2 = ldap_explode_dn("CN=Phil Robertson,OU=Users,OU=Duck Commander,OU=Department & Buildings,DC=OCSDtest,DC=local",1);
unset($dnn2['count']);
$result = null;
$ref =& $result;
foreach (array_reverse($dnn2) as $dn) {
$ref = array($dn => null);
$ref =& $ref[$dn];
}
print_r($result);
Try this
$a = array();
foreach($dnn2 as $dn)
{
$arr = explode('=',$dn);
$a[] = $arr[1]; //or $a[] = array($arr[1]); for 6 dimensional array
}
print_r($a);

Categories