PHP. array_column() analogue for SimpleXMLElement object - php

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?

Related

How to convert array of arrays into object in PHP?

I have an array of arrays like this one below and I want to convert it to object array.
array (size=3)
'declaration' =>
array (size=99)
'GO_IMPZONK_ID' => string '130334' (length=6)
'ID' => string '19802862' (length=8)
'CUSTE' => string '10100' (length=5)
'DCLEXP' => null
'DCL_BROKER_CODE' => string '' (length=0)
'RLCCODE' => string '' (length=0)
'items' =>
array (size=1)
0 =>
array (size=50)
'GO_IMPDCL_ID' => string '19802862' (length=8)
'TARIFYEAR' => string '85' (length=2)
'extensions' =>
array (size=6)
0 =>
array (size=5)
'GO_IMPDCL_ID' => string '19802862' (length=8)
'TOTVALUE' => string '0.00' (length=4)
'EXPDATE' => string '2004-03-20' (length=10)
1 =>
array (size=5)
'GO_IMPDCL_ID' => string '19802862' (length=8)
'TOTVALUE' => string '0.00' (length=4)
'EXPDATE' => string '2004-03-20' (length=10)
I did try casting it like this but it only makes Object with arrays inside.
$obj=(object)$array;
what can i do to have nesting multi level Objects from my array?
i improvised another way to do so:
$ar=[
'a'=>[
'field1'=>52,
'field2'=>52,
'field3'=>52,
],
'b'=>[
'field1'=>52,
'field2'=>52,
'field3'=>52,
]
];
function ToObj($data) {
if (gettype($data) == 'array')
return (object)array_map("ToObj", $data);
else
return $data;
}
$ObjectResult = array_map("ToObj", $ar);

multidimensional array unset not working

Here is my array;
var_dump($contact['poco']['tags']);
array (size=5)
0 =>
array (size=3)
'tag' => string 'boy' (length=3)
'color' => string '#332409' (length=7)
'id' => string '57160583b0e6df19598b4568' (length=24)
1 =>
array (size=3)
'tag' => string 'girl' (length=4)
'color' => string '#2e2f15' (length=7)
'id' => string '57160589b0e6df1d598b4567' (length=24)
2 =>
array (size=3)
'tag' => string 'zebra' (length=5)
'color' => string '#646604' (length=7)
'id' => string '57160592b0e6df7b588b4567' (length=24)
3 =>
array (size=3)
'tag' => string 'potential duplicate' (length=19)
'color' => string '#f00' (length=4)
'id' => string '57161d9db0e6df0f5c8b456b' (length=24)
4 =>
array (size=3)
'tag' => string 'no phone numbers' (length=16)
'color' => string '#5833d2' (length=7)
'id' => string '5716059ab0e6df7b588b456d' (length=24)
I just want to unset/remove one that have the following tags;
$smartTags = ['potential duplicate', 'no emails', 'no phone numbers'];
So I end up with;
array (size=3)
0 =>
array (size=3)
'tag' => string 'boy' (length=3)
'color' => string '#332409' (length=7)
'id' => string '57160583b0e6df19598b4568' (length=24)
1 =>
array (size=3)
'tag' => string 'girl' (length=4)
'color' => string '#2e2f15' (length=7)
'id' => string '57160589b0e6df1d598b4567' (length=24)
2 =>
array (size=3)
'tag' => string 'zebra' (length=5)
'color' => string '#646604' (length=7)
'id' => string '57160592b0e6df7b588b4567' (length=24)
I have tried;
$smartTags = ['potential duplicate', 'no emails', 'no phone numbers'];
foreach ($contact['poco']['tags'] as $key => $tag) {
if (in_array($tag, $smartTags)) {
unset($contact['poco']['tags'][$key]);
}
}
But it doesn't do anything. I might be having trouble because of the multi-dimensionalness of this array...
What is the correct syntax?
Try up with this.
foreach ($contact['poco']['tags'] as $key => $tag) {
if (in_array($tag['tag'], $smartTags)) {
unset($contact['poco']['tags'][$key]);
}
}

Order by value of array multidimensional

I have array like this
array (size=6)
0 =>
array (size=1)
0 =>
array (size=5)
'document_id' => string '231' (length=3)
'document_title' => string 'Renstra 2014-2018 Distamben COVER' (length=33)
'document_date' => string '2014-10-15 14:09:00' (length=19)
'site' => string 'Propinsi Nusa Tenggara Barat' (length=28)
'url_download' => string 'http://ntbprov.sip-ppid.net/index.php/document/download/231' (length=59)
1 =>
array (size=1)
0 =>
array (size=5)
'document_id' => string '7' (length=1)
'document_title' => string 'Data Menara 2014' (length=16)
'document_date' => string '2015-01-08 13:05:00' (length=19)
'site' => string 'Kabupaten Bima' (length=14)
'url_download' => string 'http://bimakab.sip-ppid.net/index.php/document/download/7' (length=57)
how to sort by document_date? I try with usort() but not working.
I am Newbie.

Reorder array keys after unset element

I have an array like
array (size=1)
'images' =>
array (size=3)
0 =>
array (size=4)
'image_name' => string 'image name' (length=10)
'image__seo_name' => string 'image-name' (length=10)
'image_path' => string 'C:/wamp/www/folder/images/products/image.png' (length=45)
'image_original_name' => string 'altinorfozotel7.png' (length=19)
1 =>
array (size=4)
'image_name' => string 'image name 2' (length=12)
'image_seo_name' => string 'image-name-2' (length=12)
'image_path' => string 'C:/wamp/www/folder/images/products/image2.png' (length=46)
'image_original_name' => string 'altinorfozotel3.png' (length=19)
2 =>
array (size=4)
'image_name' => string 'image name 3' (length=12)
'image_seo_name' => string 'image-name-3' (length=12)
'image_path' => string 'C:/wamp/www/folder/images/products/image3.png' (length=46)
'image_original_name' => string 'altinorfozotel4.png' (length=19)
I unset array['images'][0] key and need to reorder array keys in array['images'] array
I tried to use array_values($array['images']) but 'images' array key changed to 0 index.
$array['images'] = array_values($array['images']);
should work

SimpleXMLElement array only returns one element

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?

Categories