I am trying to build an array and I am looping through the values of an XML document, I have everything pulling out great using xpath, here's my code:
function parseAccountIds($xml) {
$arr = array();
foreach($xml->entry as $k => $v) {
$acctName = $v->title;
$prop = $v->xpath('dxp:property');
foreach($prop as $k1 => $v1) {
if($v1->attributes()->name == "ga:accountId")
$acctId = (string) $v1->attributes()->value;
else if($v1->attributes()->name == "ga:profileId")
$profileId = (string) $v1->attributes()->value;
}
echo "profile id ".$profileId;
echo "<BR>";
echo "acctName ".$acctName;
echo "<BR>";
$subArray = array($acctName => $profileId);
print_r($subArray);
$arr[] = array($acctId => $subArray);
}
print_r($arr);
return json_encode($arr);
}
The most important bit is where I print_r subArray. I can see acctName and profileId print, but then subArray is empty. For Example:
profile id 45580
acctName accountName1
Array
(
)
profile id 4300
acctName accountName2
Array
(
)
profile id 4338
acctName accountName3
Array
(
)
How are these values not being inserted? I've been looking at the code for a while now, and I'm a bit confused.
Any suggestions would really help,
Thanks!
try this:
$subArray[$acctName] = $profileId;
instead of
$subArray = array($acctName => $profileId);
$v->title is actually a SimpleXMLObject still!
I forgot to cast it as a string, when I tried to make it the index in the array, it freaked out, geez I spent a whole hour on this!
Thanks for your suggestions guys :P
Related
So not a stranger to PHP or arrays even, but never had to deal with multidimensional arrays and its doing my head in.
I have the output of a PHP to a server API and need to pull all the mac address values from the (dst_mac) keys, but only on the occasion the category (catname) keys value for each element is emerging-p2p
The format of the array is like this (intermediate keys and values removed for brevity)
[1] => stdClass Object
(
[_id] => 5c8ed5b2b2302604a9b9c78a
[dst_mac] => 78:8a:20:47:60:1d
[srcipGeo] =>
[dstipGeo] => stdClass Object
(
)
[usgipGeo] => stdClass Object
(
)
[catname] => emerging-p2p
)
Any help much appreciated, i know when im out of my depth!
From your example that is an array with a std class. you can use the empty funtion.
//checks if the first key
if (!empty($array[1]->_id)) {
echo $array[1]->dst_mac;
// or do what you want.
}
This example only applies to one array. use a loop to have this dynamically done.
EDIT: My answer was based on your question. Didn't realize you have to check the catname to be 'emerging-p2p' before you get the mac address?
// loop through the array
foreach ($array as $item) {
// checks for the catname
if ($item->catname === 'emerging-p2p') {
// do what you want if the cat name is correct
echo $item->dst_mac;
}
}
Is this what you want?
for($i =0;$i<count($arr);$i++){
if(isset($arr[$i]['catname']) && $arr[$i]['catname']=='emerging-p2p'){
echo $arr[$i]['dst_mac'];
}
}
If there is only one object that has 'emerging-p2p' cat name:
foreach ($your_list_of_objects as $obj) {
if ($obj->catname == 'emerging-p2p') {
return $obj->dst_mac;
}
}
If there are many:
$result = [];
foreach ($your_list_of_objects as $obj) {
if ($obj->catname == 'emerging-p2p') {
$result[]= $obj->dst_mac;
}
}
return $result;
Use for loop.
for($i =0; $i<=count($arr); $i++){
if(arr[$i]['catname']=='emerging-p2p'){
echo arr[$i]['dst_mac'];
}
}
To fetch all the mac where catname is emerging-p2p
//Assuming $arr has array of objects
$result_array = array_filter($arr, function($obj){
if ($obj->catname == 'emerging-p2p') {
return true;
}
return false;
});
foreach ($result_array as $value) {
echo $value->dst_mac;
}
You can use array_column if you need only mac address on the basis of catname.
$arr = json_decode(json_encode($arr),true); // to cast it as array
$temp = array_column($arr, 'dst_mac', 'catname');
echo $temp['emerging-p2p'];
Working demo.
Say you have the array .
$arr = array('foo' => 'bar, 'wang' => 'chung', 'ying' => 'yang');
Now I want to loop through another array (var = $terms) to get values using foreach. If the value is any of the keys listed in $arr, I want to replace it with the value listed in $arr.
I've tried this ...
foreach($terms as $term => $arr) {
echo $term[$arr];
}
This doesn't work ... and I'm pretty stumped beyond that point. Reading through the manual on foreach ... I felt like this was on the right path - but think I'm needing a nudge in another direction.
Thoughts?
You can use array_key_exists() function for verifying whether key exists in another array or not If found then replace that with existing once. You can refer below answer,
$arr = array('foo' => 'bar', 'wang' => 'chung', 'ying' => 'yang');
$res = [];
foreach($terms as $term => $arr1) {
if( array_key_exists( $term, $arr ) ) {
$res[$term] = $arr[$term];
} else {
$res[$term] = $arr1;
}
}
echo '<pre>'; print_r($res);
I hope this will resolve your problem.
I am fairly new to PHP and have been working on looping through this array for days...sigh...
http://pastebin.com/Rs6P4e4y
I am trying to get the name and headshot values of the writers and directors array.
I've been trying to figure out the foreach function to use with it, but have had no luck.
<?php foreach ($directors as $key => $value){
//print_r($value);
}
?>
Any help is appreciated.
You looking for some such beast? You didn't write how you wanted to process them, but hopefully this helps you.
$directors = array();
foreach( $object->people->directors as $o )
$directors[] = array( 'name' => $o->name, 'headshot' => $o->images->headshot );
$writers = array();
foreach( $object->people->writers as $o )
$writers[] = array( 'name' => $o->name, 'headshot' => $o->images->headshot );
var_dump( $directors );
var_dump( $writers );
Last note, if there's no guarantee that these members are set, you use isset before any dirty work.
Hope this helps.
Use -> to access properties of the objects.
foreach ($directors as $director) {
echo 'Name: ' . $director->name . "\n";
echo "Headshot: " . $director->images->headshot . "\n";
}
Your solution has already been posted, but I want to add something:
This isn't an Array, it's an object. Actually the directors property of the object is an Array. Look up what an object is and what associative arrays are!
Objects have properties, arrays have keys and values.
$object = new stdClass();
$object->something = 'this is an object property';
$array = new array();
$array['something'] = 'this is an array key named something';
$object->arrayproperty = $array;
echo $object->arrayproperty['something']; //this is an array key named something
Good luck with learning PHP! :)
Having variable $foo, which is an object, you can access property bar using syntax:
$foo->bar
So if you have an array od directors named $directors, you can simply use it the same way in foreach:
foreach ($directors as $value){
echo $value->name." ".$value->images->headshot;
}
I have the following snippet of code that is creating the following array...
while ($stmt->fetch()) {
foreach($row as $key => $val) {
$x[$key] = $val;
}
$results[] = $x;
}
Results in the follow array:
Array ( [0] => Array ( [cap_login] => master [cap_pword] => B-a411dc195b1f04e638565e5479b1880956011badb73361ca ) )
Basically I want to extract the cap_login and cap_pword values for testing. For some reason I can't get it!
I've tried this kind of thing:
echo $results[$cap_login];
but I get the error
Undefined variable: cap_login
Can someone put me right here?
Thanks.
cap_login is in an array within $results so you would have to do $results[0]['cap_login']
You would have to do the following:
echo $x[0]['cap_login'] . '<br />';
echo $x[0]['cap_pword'];
The reson $results[$cap_login] wont work is because there isn't a variable called $cap_login, there is a string called cap login. In addition, there isn't a key in $results called $cap_login. There is a value in $results called 'cap_login'
I'm trying to make a very basic php ORM as for a school project. I have got almost everything working, but I'm trying to map results to an array. Here's a snippet of code to hopefully assist my explanation.
$results = array();
foreach($this->columns as $column){
$current = array();
while($row = mysql_fetch_array($this->results)){
$current[] = $row[$column];
print_r($current);
echo '<br><br>';
}
$results[$column] = $current;
}
print_r($results);
return mysql_fetch_array($this->results);
This works, but the while loop only works on the first column. The print_r($results); shows the following:
Array ( [testID] => Array ( [0] => 1 [1] => 2 ) [testName] => Array ( ) [testData] => Array ( ) )
Can anybody shed some light?
Thanks in advance!
It's because you already fetched every row, and the internal pointer is at the end.
The next while, mysql_fetch_array() will immediately return false.
You can reset the pointer to the first row:
mysql_data_seek($this->results, 0);
Put this just before
while($row = mysql_...
I'm not sure you can use the -> operator in a variable name. As you trying to get the key and value out of the array $columns? If so, you want something like this:
foreach($columns as $k => $v) {
//in here, $k is the name of the field, and $v is the associated value
}