SimpleXMLElement array only returns one element - php

I have the following Simple XML structure var_dump'ed:
var_dump($data) results in:
object(SimpleXMLElement)[269]
public 'columns' =>
object(SimpleXMLElement)[283]
public 'column' =>
array (size=11)
0 =>
object(SimpleXMLElement)[274]
public '#attributes' =>
array (size=2)
'name' => string 'test' (length=10)
'display' => string 'test ID' (length=11)
1 =>
object(SimpleXMLElement)[273]
public '#attributes' =>
array (size=2)
'name' => string 'blah' (length=8)
'display' => string 'blah' (length=8)
....
....
....
public 'row' =>
array (size=6)
0 =>
object(SimpleXMLElement)[270]
public '#attributes' =>
array (size=11)
'test' => string '3445543' (length=8)
'blah' => string 'Some Text' (length=13)
1 =>
object(SimpleXMLElement)[279]
public '#attributes' =>
array (size=11)
'test' => string '3445543' (length=8)
'blah' => string 'Some Text' (length=13)
2 =>
object(SimpleXMLElement)[278]
....
....
....
I shortened the output as you can see just to save room. But the row array contains 6 elements as you can see.
Now if I do this:
echo count($data->row); I get 6 as expected.
If I do this:
var_dump($data->row); I get this...
object(SimpleXMLElement)[270]
public '#attributes' =>
array (size=11)
'test' => string '3445543' (length=8)
'blah' => string 'Some Text' (length=13)
It prints out one element. No array.
Why would the count of the array return the right value but fetching the array only gets the first element of the array? How do I get all of the elements?

Related

PHP. array_column() analogue for SimpleXMLElement object

object(SimpleXMLElement)[803]
public 'row' =>
array (size=13)
0 =>
object(SimpleXMLElement)[797]
public '#attributes' =>
array (size=4)
'codeonimage' => string '01' (length=2)
'name' => string 'Крышка' (length=12)
'oem' => string '13711251885' (length=11)
'ssd' => string '$HgsQRnNPF0d$' (length=174)
public 'attribute' =>
array (size=2)
0 =>
object(SimpleXMLElement)[813]
public '#attributes' =>
array (size=3)
'key' => string 'amount' (length=6)
'name' => string 'Количество' (length=20)
'value' => string '1' (length=1)
1 =>
object(SimpleXMLElement)[814]
public '#attributes' =>
array (size=3)
'key' => string 'end_of_production' (length=17)
'name' => string 'end_of_production' (length=17)
'value' => string 'Не производтся с: 19871116' (length=40)
...
I'm trying to take all the 'oem' values from each SimpleXML element like:
array_column($simpleXMLObject->row, 'oem');
... and of course I get an error:
array_column() expects parameter 1 to be array, object given
There is another option with a full search. But maybe there is some more pretty way to do this?

PHP Arrays: How to add 'key & value' to an existing array

I do not know how to add a key and value to the existing array. My array goes like this. Initially I have tried adding using array_push() but it added not as I needed it.
I have given my output after I gave the 'var_dump'.
array (size=5)
0 =>
array (size=3)
'id' => int 7
'title' => string 'Pongal' (length=6)
'start' => string '2016-05-16' (length=10)
1 =>
array (size=3)
'id' => int 8
'title' => string 'big day' (length=7)
'start' => string '2016-05-04' (length=10)
2 =>
array (size=3)
'id' => int 9
'title' => string 'marriage day' (length=12)
'start' => string '2016-05-19' (length=10)
3 =>
array (size=3)
'id' => int 10
'title' => string 'Karthiks bday' (length=14)
'start' => string '2016-06-11' (length=10)
4 =>
array (size=3)
'id' => int 12
'title' => string 'Election date announced' (length=23)
'start' => string '2016-06-01' (length=10)
Now, I'd like to insert array('sample_key' => 'sample_value') after all the elements of each array.
How can I do it? This is I want the result to be like this:-
array (size=5)
0 =>
array (size=4)
'id' => int 7
'title' => string 'Pongal' (length=6)
'start' => string '2016-05-16' (length=10)
‘color’ => ‘red’
1 =>
array (size=4)
'id' => int 8
'title' => string 'big day' (length=7)
'start' => string '2016-05-04' (length=10)
‘color’ => ‘red’
2 =>
array (size=4)
'id' => int 9
'title' => string 'marriage day' (length=12)
'start' => string '2016-05-19' (length=10)
‘color’ => ‘red’
3 =>
array (size=4)
'id' => int 10
'title' => string 'Karthiks bday' (length=14)
'start' => string '2016-06-11' (length=10)
‘color’ => ‘red’
4 =>
array (size=4)
'id' => int 12
'title' => string 'Election date announced' (length=23)
'start' => string '2016-06-01' (length=10)
‘color’ => ‘red’
Note that I have added 'color' => 'red' to all the indexes
Just do this: Working demo
using the & you can change the main array, and just use $val['color'] = 'red' to add a new key , value pair in the array.
foreach($arr as $key => &$val){
$val['color'] = 'red';
}
Note that the 'write-back' feature of the ampersand persists even after the loop has finished: resetting $val to a new value will change the last element in $val, which is often unexpected. There are three ways around this class of bug:
Avoid write-back and just use the full array expression to write values inside the loop;
Don't re-use the $val variable in the same scope, even for another foreach() loop;
Use unset() on the $val variable to disconnect it from the array it will write back to.
foreach($arr as $key => $row){
$arr[$key]['color']="red";
}

Email grouped array results

So I've got a series of arrays that look like this:
object(Illuminate\Support\Collection)[214]
protected 'items' =>
array (size=1)
0 =>
array (size=2)
'data' => string 'test'
'user' => string 'testmail#testing.com'
object(Illuminate\Support\Collection)[215]
protected 'items' =>
array (size=1)
0 =>
array (size=2)
'data' => string 'Awesome test'
'user' => string 'tester#mail.com'
object(Illuminate\Support\Collection)[220]
protected 'items' =>
array (size=2)
0 =>
array (size=2)
'data' => string ' A string'
'user' => string 'different.tester#mail.com'
1 =>
array (size=2)
'data' => string 'Another string again'
'user' => string 'different.tester#mail.com'
I'm wanting to loop through each array and send the data values to the relevant user.
So different.tester#mail.com would receive 'A string' and 'Another string again' for example.
I'm totally stuck on this and would welcome any feedback!
Thanks

Removing an item from a multidimensional array in php by value

I have a multidimensional array $array that looks like this:
array (size=3)
0 =>
array (size=1)
0 =>
object(stdClass)[500]
public 'id' => int 2
public 'first_name' => string 'Mary' (length=4)
public 'last_name' => string 'Sweet' (length=5)
1 =>
array (size=1)
0 =>
object(stdClass)[501]
public 'id' => int 9
public 'first_name' => string 'Joe' (length=3)
public 'last_name' => string 'Bob' (length=3)
2 =>
array (size=1)
0 =>
object(stdClass)[502]
public 'id' => int 1
public 'first_name' => string 'Shag' (length=4)
public 'last_name' => string 'Well' (length=4)
I would like to be able to remove one of the items in the array by searching for values (not indexes).
So, I would like to remove the item in the array that has the first_name property of 'Joe'.
So if I removed it, the array would look like this:
array (size=2)
0 =>
array (size=1)
0 =>
object(stdClass)[500]
public 'id' => int 2
public 'first_name' => string 'Mary' (length=4)
public 'last_name' => string 'Sweet' (length=5)
1 =>
array (size=1)
0 =>
object(stdClass)[502]
public 'id' => int 1
public 'first_name' => string 'Shag' (length=4)
public 'last_name' => string 'Well' (length=4)
How would I be able to do this? Thanks.
Yes you could just use a foreach in this case. It will work just fine. Just use your search string needle and add an if inside the loop comparing the objects property firstname and the needle:
$first_name_search = 'Joe';
foreach ($array as $key => $value) {
$value = reset($value); // get that index 0 which is nested
if($value->first_name == $first_name_search) {
unset($array[$key]);
}
}

How to take two random entries from a multidimensional array in PHP

I am looking for a way to take a multidimensional array and choose 2 random entries from the top level to create a new multidimensional array.
For example, if I have an array $data that looks like the below:
array (size=3)
0 =>
array (size=1)
0 =>
object(stdClass)[500]
public 'id' => int 2
public 'first_name' => string 'Mary' (length=4)
public 'last_name' => string 'Sweet' (length=5)
1 =>
array (size=1)
0 =>
object(stdClass)[501]
public 'id' => int 9
public 'first_name' => string 'Joe' (length=3)
public 'last_name' => string 'Bob' (length=3)
2 =>
array (size=1)
0 =>
object(stdClass)[502]
public 'id' => int 1
public 'first_name' => string 'Shag' (length=4)
public 'last_name' => string 'Well' (length=4)
How do I cut it up so that I take two of the three random entries, to get something like $data2:
array (size=2)
0 =>
array (size=1)
0 =>
object(stdClass)[500]
public 'id' => int 2
public 'first_name' => string 'Mary' (length=4)
public 'last_name' => string 'Sweet' (length=5)
1 =>
array (size=1)
0 =>
object(stdClass)[502]
public 'id' => int 1
public 'first_name' => string 'Shag' (length=4)
public 'last_name' => string 'Well' (length=4)
Use array_rand(). You could get more sophisticated depending on what you are doing, but here is the basic idea:
$randkeys = array_rand($data, 2);
$data2 = array($data[$randkeys[0]], $data[$randkeys[1]]);
See demo

Categories