Reorder array keys after unset element - php

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

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?

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);

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";
}

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.

Categories