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".
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 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.
I have an array called $reads when I do an var_dump($reads), I get the below array result.
I am trying to get first item of first array with var_dump($reads[0][0]). I get a Message: Error rendering view: [home.uploaded] Undefined offset: 0
array(161) {
[0]=>
array(4) {
["517a5745e8505"]=>
string(29) "Ngee Ann Poly_Keywords report"
["517a5745e86fe"]=>
string(0) ""
["517a5745e882e"]=>
string(0) ""
["517a5745e89b5"]=>
string(0) ""
}
[1]=>
array(4) {
["517a5745e8505"]=>
string(7) "Keyword"
["517a5745e86fe"]=>
string(6) "Clicks"
["517a5745e882e"]=>
string(11) "Impressions"
["517a5745e89b5"]=>
string(3) "CTR"
}
[2]=>
array(4) {
["517a5745e8505"]=>
string(18) "accounting diploma"
["517a5745e86fe"]=>
string(1) "2"
["517a5745e882e"]=>
string(3) "364"
["517a5745e89b5"]=>
string(5) "0.55%"
}
[3]=>
array(4) {
["517a5745e8505"]=>
string(11) "polytechnic"
["517a5745e86fe"]=>
string(4) "1940"
["517a5745e882e"]=>
string(5) "42995"
["517a5745e89b5"]=>
string(5) "4.51%"
}
[4]=>
array(4) {
["517a5745e8505"]=>
string(15) "tourism diploma"
["517a5745e86fe"]=>
string(1) "1"
["517a5745e882e"]=>
string(3) "156"
["517a5745e89b5"]=>
string(5) "0.64%"
}
Try this
var_dump($reads[0]["517a5745e8505"]);
for what you want as per comments do this, put you array in a $arr variable and follow what I am doing.
$firstelementvalues = array();
$i = 0;
foreach ($arr as $key=>$val) {
$x = 0;
foreach ($val as $value) {
if ($x == 0) {
$firstelementvalues[] = $value;
$x = 1;
}
}
$i++;
}
print_r($firstelementvalues);
Output is
Array
(
[0] => Ngee Ann Poly_Keywords report
[1] => Keyword
[2] => accounting diploma
)
Because there is no value in array with 0 offset so please try like
var_dump($reads[161][0]);
I think that you need to use loop (for,foreach) if you would like to display AND use the data.
var_dump is: Arrays and objects are explored recursively with values indented to show structure.
It's because your array doesn't have element [0][0].
If you want to select first element in second dimension array you can use current:
$lev1 = current($yourArray);
$lev2 = current($lev1);
Your reads array has no numeric keys in the second dimension. You could do something like this, if you have no clue about the keys:
$read = $reads[0];
// I am getting all keys now, because I guess you also want to process the rest of that data
$readKeys = array_keys($read);
var_dump($read[ $readKeys[0] ] );
I'm looking making a regex in php that gets data from the following format:
"1,2,3;7,1,3;1" returns an $matches array with "(1,2,3,7,1,3,1)"
"1" returns an $matches with "(1)"
"1;1;3;5;7;10;999" returns an $matches array with "(1,1,3,5,7,10,999)"
"1,1,1;2,5;3,4" doesn't pass since numbers are repeating within semicolon boundaries
"2,3,4;5,;" doesn't pass since it doesn't satisfy the format.
(Quotes in the examples are there to make them easier to read; they should not appear in the real results.)
The format is digit numbers separated by either commas or semicolons and within semicolons they don't repeat each other. It should not accept any other format.
I tried /(^(\d{1,3})$)|(([0-9]+)([,|;]{1}[0-9]+)+)/ but it didn't work.
I also tried /[0-9]+([,|;]{1}[0-9]+)+/ but it didn't worked either. When I got the $matches array it didn't have the values I needed as depicted above.
I'm doing this in PHP 5.2.
Thanks.
This particular problem has too much logic for regular expressions to be practical; this is how you could solve it with regular code:
// reduction function - keeps merging comma separated arguments
// until there's a duplicate or invalid item
function join_unique(&$result, $item)
{
if ($result === false) {
return false;
}
$items = explode(',', $item);
$numbers = array_filter($items, 'is_numeric');
if (count($items) != count($numbers)) {
return false;
}
$unique = array_unique($numbers);
if (count($unique) != count($numbers)) {
return false;
}
return array_merge($result, $numbers);
}
// main function - parse a string of comma / semi-colon separated values
function parse_nrs($str)
{
return array_reduce(explode(';', $str), 'join_unique', array());
}
var_dump(parse_nrs('1,2,3;7,1,3;1'));
var_dump(parse_nrs('1'));
var_dump(parse_nrs('1;1;3;5;7;10;999'));
var_dump(parse_nrs('1,1,1;2,5;3,4'));
var_dump(parse_nrs('2,3,4;5,;'));
Output:
array(7) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(1) "7"
[4]=>
string(1) "1"
[5]=>
string(1) "3"
[6]=>
string(1) "1"
}
array(1) {
[0]=>
string(1) "1"
}
array(7) {
[0]=>
string(1) "1"
[1]=>
string(1) "1"
[2]=>
string(1) "3"
[3]=>
string(1) "5"
[4]=>
string(1) "7"
[5]=>
string(2) "10"
[6]=>
string(3) "999"
}
bool(false)
bool(false)
See also: array_reduce() array_unique()
It is impossible to do this in a single step. First you will need to check for the requirement of numbers repeating within a semicolon boundary, and then if it passes that checks split the string.
For example:
if (!preg_match('/\b(\d+),[^;]*\b\1\b/', $string)) {
$matches = preg_split('/[,;]/', $string);
} else {
$matches = NULL;
}
Ideone: http://ideone.com/Y8xf1N
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