I have this array $node->taxonomy which puts out these data, if I var_dump() it:
array(1) {
[4]=> object(stdClass)#21 (5) {
["tid"]=> string(1) "4"
["vid"]=> string(1) "4"
["name"]=> string(9) "Marketing"
["description"]=> string(0) ""
["weight"]=> string(1) "1"
}
}
how can I extract the element "name" from it?
This way:
$node->taxonomy[4]->name
$node->taxonomy[4]->name
<?php
echo $node->taxonomy[4]->name;
or if you don't know the number (or the number changes) but its the first element in the array you could use:
<?php
$x = current($node->taxonomy);
echo $x->name;
As simple as:
$node->taxonomy[4]->name
Related
I have two arrays that I want to merge. I created the first one through a for() loop so that it includes the last seven days (only three here to keep it short):
array(7) {
[0]=>
array(2) {
["created_at"]=>
string(10) "2017-08-15"
["errorCount"]=>
string(1) "0"
}
[1]=>
array(2) {
["created_at"]=>
string(10) "2017-08-16"
["errorCount"]=>
string(1) "0"
}
[2]=>
array(2) {
["created_at"]=>
string(10) "2017-08-17"
["errorCount"]=>
string(1) "0"
}
}
The other array includes data from a DB:
array(2) {
[0]=>
array(2) {
["created_at"]=>
string(10) "2017-08-15"
["errorCount"]=>
string(1) "4"
}
[1]=>
array(2) {
["created_at"]=>
string(10) "2017-08-16"
["errorCount"]=>
string(1) "12"
}
}
I want to merge these two together so that errorCount in the first array is overwritten whenever created_at is identical. I tried it with array_merge() directly but it only adds the rows of the second array to the end of the first one.
Any suggestions on how to solve this? Or is there a different way to approach it?
Filter result by following statement:
$finArray = array_values(array_combine(array_map(function ($value) {
return $value['created_at'];
}, $mergedArray), $mergedArray));
I am new to PHP.
I want to convert this array;
array(2) {
[0]=> object(stdClass)#24 (1) {
["item"]=> string(1) "2"
}
[1]=> object(stdClass)#25 (1) {
["item"]=> string(1) "1"
}
}
to string like this
string(1) "2", string(2)"1", string(3)"0"...
What is the way to do this?
Note: i try add to "row()" in php code. but always single result.
for example: only string(1)"2"
You are asking something weird. But here is an answer:
let say $arr is your
array(2) { [0]=> object(stdClass)#24 (1) { ["item"]=> string(1) "2" }
[1]=> object(stdClass)#25 (1) { ["item"]=> string(1) "1" } }
then your code to convert it into that string you mentioned will be:
$str=''; $idx=0;
foreach ($arr as $obj) {
$str.=($idx==0?'':', ').'string ('.$idx++.') "'.$obj->item.'"';
}
Now you have your weird string in $str var.
Or if my first weird guess is wrong. Here is another that has more sense to me, but not asked by you:
foreach ($arr as $obj) {
echo $obj->item; // or do whatever you want with this value which is string type
}
Looks you have an array object, you need to use a foreach loop here, and your array values are "2", "1". Not string(1) "2" , string(1) "1", You use a var_dump so the arrays shows with the type and length.
array(2) {
[0]=> object(stdClass)#24 (1) {
["item"]=> string(1) "2"
}
[1]=> object(stdClass)#25 (1) {
["item"]=> string(1) "1"
}
}
To get each value using foreach, see the below example, let your array name is $arr.
foreach($arr as $val){
echo $val->item;
}
This will display the array values as: "2", "1".
I am trying to create an associative array with the keys being email addresses and the values being passwords. It is reading from an XML database to get the information. Here is my code:
$data = simplexml_load_file("Treasury.xml");
//Add in all passwords
for ($i = 0; $i < count($data->Member); $i++) {
$key = $data->Member[$i]->Email + '';
$USERS[$key] = $data->Member[$i]->Pin;
}
The problem comes in the for loop. It gets a correct count of the members (I had that print out) but the key is always being labeled as the number 0, resulting in only the last pin being stored in an array on length 1. Is there something syntactically that I am doing wrong?
Thanks in advance.
EDIT: I did a var_dump of the first user in the XML document. Here it is (Sorry for how long it is):
object(SimpleXMLElement)#4 (5) { ["Name"]=> string(19) "Mackenzie Daugherty" ["PC"]=> object(SimpleXMLElement)#2 (0) { } ["Email"]=> string(16) "dau53688#obu.edu" ["Pin"]=> string(4) "0000" ["Payments"]=> object(SimpleXMLElement)#3 (1) { ["Payment"]=> array(2) { [0]=> object(SimpleXMLElement)#5 (7) { ["Type"]=> string(4) "Dues" ["Description"]=> string(18) "Dues for Fall 2013" ["DateIssued"]=> string(7) "8/26/13" ["DateEnd"]=> string(6) "9/9/13" ["Owed"]=> string(2) "55" ["Paid"]=> string(2) "55" ["Plan"]=> object(SimpleXMLElement)#7 (5) { ["InPlan"]=> string(1) "0" ["PlanDescription"]=> object(SimpleXMLElement)#8 (0) { } ["Intervals"]=> string(1) "0" ["Completed"]=> string(1) "0" ["PerInterval"]=> string(1) "0" } } [1]=> object(SimpleXMLElement)#6 (7) { ["Type"]=> string(19) "Tiger Tunes Tickets" ["Description"]=> string(18) "Two Saturday Night" ["DateIssued"]=> string(7) "8/26/13" ["DateEnd"]=> string(7) "8/26/13" ["Owed"]=> string(2) "30" ["Paid"]=> string(2) "30" ["Plan"]=> object(SimpleXMLElement)#7 (5) { ["InPlan"]=> string(1) "0" ["PlanDescription"]=> object(SimpleXMLElement)#8 (0) { } ["Intervals"]=> string(1) "0" ["Completed"]=> string(1) "0" ["PerInterval"]=> string(1) "0" } } } } }
As clearly stated in the documentation that I'm sure you've been studying carefully, the PHP concatenation operator is ., not +.
Your code takes two operands, and attempts to perform arithmetic addition on them. Since they are not [meaningful] numbers, you end up with 0.
(I couldn't give a more detailed assessment without knowing the precise values of your operands, which you did not provide.)
Your code should read:
$key = $data->Member[$i]->Email . '';
// ^
// (is the concatenation necessary at all?
// isn't Email already a string?)
Make the same correction elsewhere.
This one's been bugging me for the past day now. Here's the code:
public function getUserCredits()
{
$dbl = new databaseManager();
$dbl->connect_simp();
$ret = $dbl->queryDB("SELECT * FROM users WHERE `USER_ID` = ".$this->userId);
$this->userCredits = $ret['USER_CREDITS'];
return $this->userCredits;
}
Now when I try to run this code, I get an undefined offset error. Nothing too strange about it when I first saw it, but now it's happening more and more and I can't figure out why.
I can use var_dump(); and var_export(); and it displays the contents of the returned array absolutely fine.
EDIT:
array(1) {
[0]=>
array(50) {
["USER_ID"]=>
string(10) "0000000001"
[0]=>
string(10) "0000000001"
["USER_USERNAME"]=>
string(8) "SampleUsername"
[1]=>
string(8) "SampleUsername"
["USER_PASSWORD"]=>
string(32) "5f4dcc3b5aa765d61d8327deb882cf99"
[2]=>
string(32) "5f4dcc3b5aa765d61d8327deb882cf99"
["USER_EMAIL"]=>
string(0) ""
[3]=>
string(0) ""
["USER_LEGION"]=>
string(1) "1"
[4]=>
string(1) "1"
["USER_ENERGY"]=>
string(4) "2812"
[5]=>
string(4) "2812"
["USER_MAX_ENERGY"]=>
string(4) "2812"
[6]=>
string(4) "2812"
["USER_SHIELD"]=>
string(2) "20"
[7]=>
string(2) "20"
["USER_MAX_SHIELD"]=>
string(2) "20"
[8]=>
string(2) "20"
["USER_HULL"]=>
string(2) "60"
[9]=>
string(2) "60"
["USER_MAX_HULL"]=>
string(2) "60"
[10]=>
string(2) "60"
["USER_CREDITS"]=>
string(19) "9223372036854775807"
try
$ret[0]['USER_CREDITS']
instead of
$ret['USER_CREDITS']
You gotta fetch a row first (I assume you extend mysqli).
$result = $dbl->queryDB("SELECT * FROM users WHERE `USER_ID` = ".$this->userId);
$row = $dbl->fetch_assoc($result) ;
$this->userCredits = $row['USER_CREDITS'];
At first you should watch the result record. As you are getting the details in an array of your database record fetched and it is in array format with numeric keys. Use what #AbuOmar said.
AS your var_dump($ret), Try this to assign user credit otherwise if it is not set, assign 0 value.
if(isset($ret[0]['USER_CREDITS'])){
$this->userCredits = $ret[0]['USER_CREDITS'];
}
else{
$this->userCredits = 0;
}
This is my string that I need to work with parse_str:
type%3Dbasic%26WhatPropType%3DSingle%2BFamily%2BResidential%26WhatDistrict%255B0%255D%3DHaiku%26WhatStartPrice%3D90000%26WhatEndPrice%3D30000000%26WhatStartBed%3D%26WhatStartBath%3D0%26DaysAgo%3D%26WhatStartIntArea%3D%26WhatSortDirection1%3DDESC%26start%3D0%26WhatNumber%3D10
How can i get this to work?
You firstly need to decode your url, then pass it to parse_str. The second parameter for parse_str is what will be populated with your data. So you can do the following:
parse_str(urldecode('type%3Dbasic%26WhatPropType%3DSingle%2BFamily%2BResidential%26WhatDistrict%255B0%255D%3DHaiku%26WhatStartPrice%3D90000%26WhatEndPrice%3D30000000%26WhatStartBed%3D%26WhatStartBath%3D0%26DaysAgo%3D%26WhatStartIntArea%3D%26WhatSortDirection1%3DDESC%26start%3D0%26WhatNumber%3D10'), $array);
var_dump($array);
will output
array(12) {
["type"]=>
string(5) "basic"
["WhatPropType"]=>
string(25) "Single Family Residential"
["WhatDistrict"]=>
array(1) {
[0]=>
string(5) "Haiku"
}
["WhatStartPrice"]=>
string(5) "90000"
["WhatEndPrice"]=>
string(8) "30000000"
["WhatStartBed"]=>
string(0) ""
["WhatStartBath"]=>
string(1) "0"
["DaysAgo"]=>
string(0) ""
["WhatStartIntArea"]=>
string(0) ""
["WhatSortDirection1"]=>
string(4) "DESC"
["start"]=>
string(1) "0"
["WhatNumber"]=>
string(2) "10"
}
parse_str(urldecode($yourString));
as the same as our friends say decode the string
parse_str(urldecode('type%3Dbasic%26WhatPropType%3DSingle%2BFamily%2BResidential%26WhatDistrict%255B0%255D%3DHaiku%26WhatStartPrice%3D90000%26WhatEndPrice%3D30000000%26WhatStartBed%3D%26WhatStartBath%3D0%26DaysAgo%3D%26WhatStartIntArea%3D%26WhatSortDirection1%3DDESC%26start%3D0%26WhatNumber%3D10'), $array);
and get value like this
$array['type'] //out put "basic"