Order by value of array multidimensional - php

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.

Related

Replacing array keys with values within the same array

I need to replace the keys of my array with keys within the same array in PHP.
My array looks like this at the moment :
array (size=7)
0 =>
array (size=2)
'id' => int 978
'nbUnitsSold' => string '33.00000' (length=8)
1 =>
array (size=2)
'id' => int 979
'nbUnitsSold' => string '9.00000' (length=7)
2 =>
array (size=2)
'id' => int 980
'nbUnitsSold' => string '4.00000' (length=7)
3 =>
array (size=2)
'id' => int 981
'nbUnitsSold' => string '237.00000' (length=9)
4 =>
array (size=2)
'id' => int 983
'nbUnitsSold' => string '5.00000' (length=7)
5 =>
array (size=2)
'id' => int 984
'nbUnitsSold' => string '19.00000' (length=8)
6 =>
array (size=2)
'id' => int 991
'nbUnitsSold' => string '2.00000' (length=7)
I want it to look like this :
array (size=7)
978 =>
array (size=1)
'id' => int 978
'nbUnitsSold' => string '33.00000' (length=8)
979 =>
array (size=1)
'id' => int 979
'nbUnitsSold' => string '9.00000' (length=7)
980 =>
array (size=1)
'id' => int 980
'nbUnitsSold' => string '4.00000' (length=7)
981 =>
array (size=1)
'id' => int 981
'nbUnitsSold' => string '237.00000' (length=9)
983 =>
array (size=1)
'id' => int 983
'nbUnitsSold' => string '5.00000' (length=7)
984 =>
array (size=1)
'id' => int 984
'nbUnitsSold' => string '19.00000' (length=8)
991 =>
array (size=1)
'id' => int 991
'nbUnitsSold' => string '2.00000' (length=7)
Is there any function in PHP that could help without looping into the whole array ?
Thank you !
Just use array_column() with the third parameter as the column you want as the index...
$indexedArray = array_column($input,null,"id");

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 replace values of array elements based on the lookup from another array

I have an array $fruits which is as following:
array (size=4)
0 =>
array (size=2)
'fruit' => string 'banana' (length=5)
'origin' => string 'cat1' (length=4)
1 =>
array (size=2)
'fruit' => string 'orange' (length=6)
'origin' => string 'cat2' (length=4)
2 =>
array (size=2)
'fruit' => string 'apple' (length=5)
'origin' => string 'cat3' (length=4)
3 =>
array (size=2)
'fruit' => string 'lemon' (length=5)
'origin' => string 'cat4' (length=4)
and I have another array $origins which has the old origins and new origins of the fruits:
array (size=5)
0 =>
array (size=2)
'oldOrigin' => string 'cat1' (length=5)
'newOrigin' => string 'new1' (length=4)
1 =>
array (size=2)
'oldOrigin' => string 'cat2' (length=5)
'newOrigin' => string 'new2' (length=4)
2 =>
array (size=2)
'oldOrigin' => string 'cat3' (length=5)
'newOrigin' => string 'new3' (length=4)
3 =>
array (size=2)
'oldOrigin' => string 'cat4' (length=5)
'newOrigin' => string 'new4' (length=4)
4 =>
array (size=2)
'oldOrigin' => string 'cat5' (length=5)
'newOrigin' => string 'new5' (length=4)
The question is how to replace the origin values in the first array with the newOrigin values, by looking up on the value of oldOrigin.
so the result will be:
$fruits :
array (size=4)
0 =>
array (size=2)
'fruit' => string 'banana' (length=5)
'origin' => string 'new1' (length=4)
1 =>
array (size=2)
'fruit' => string 'orange' (length=6)
'origin' => string 'new2' (length=4)
2 =>
array (size=2)
'fruit' => string 'apple' (length=5)
'origin' => string 'new3' (length=4)
3 =>
array (size=2)
'fruit' => string 'lemon' (length=5)
'origin' => string 'new4' (length=4)
thanks in advance for the help, i am using php 5.
Something like this ?
foreach($fruits as $fruit)
{
foreach($origins as $origin)
{
if($origin['oldOrigin'] == $fruit['origin'])
{
$fruit['origin'] == $origin['newOrigin'];
}
}
}

How to combine one array in php

How to my array combine :
$visitor_tracvking_data = json_encode($array);
print_r($visitor_tracvking_data);
Output print_r this:
array (size=6)
0 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'direct' => string '261' (length=3)
1 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'social_media' => string '3' (length=1)
2 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'search' => string '3' (length=1)
3 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'chrome' => string '168' (length=3)
4 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'firefox' => string '68' (length=2)
5 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'netscape' => string '31' (length=2)
How to out put this type:-
array (size=2)
0 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'direct' => string '261' (length=3)
'social_media' => string '3' (length=1)
'search' => string '3' (length=1)
1 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'chrome' => string '168' (length=3)
'firefox' => string '68' (length=2)
'netscape' => string '31' (length=2)
Just loop through the array and generate a new array with tracktitle key -
$new = array();
foreach($array as $key_top => $value) {
foreach($value as $key => $val) {
$new[$value['tracktitle']][$key] = $val;
}
}
return $new;
OR for json encoded string -
return json_encode($new);

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

Categories