I have an array at present that looks like this:
Array
(
[0] => Array
(
[language] => English
)
[1] => Array
(
[language] => Arabic
)
[2] => Array
(
[language] => Bengali
)
)
What I'd like to do is change it so it looks like this:
Array
(
[language] => Array
(
[0] => English
[1] => Arabic
[2] => Bengali
)
)
I also have an array that looks like this:
Array
(
[id] => 3
[name] => lethalMango
[joined] => 2010-01-01 00:00:00
)
And I'd like to change it to:
Array
(
[user] => Array
(
[id] => 3
[name] => lethalMango
[joined] => 2010-01-01 00:00:00
)
)
I've tried a number of methods without much success but I'm sure there is an more efficient way.
FIRST :
$result = array();
foreach($array as $value){
$result['language'][]= $value['language']
}
SECOND :
$result['user'] = $array;
Second
$result = array('user'=>$array);
Related
I am new to PHP and Arrays, I am trying to get the values from an array. But no matter how I'm trying to do it, I can't get the value. What am I doing wrong?
The Array:
Array ( [playerinfo] => Array ( [rank] => Godfather [cash] => € 8,520,530 [weapon] => M-16 (29000) [health] => Array ( [width] => 100 [color] => green ) [wealth] => Too rich to be true [protection] => Bulletproof Humvee [plf] => Huge [plane] => Concorde [crew] => None [pbf] => Large [ship] => None ) [character] => Array ( [crime] => Array ( [0] => 120 [1] => 69 ) [gta] => Array ( [0] => 400 [1] => 70 ) [drugs] => Array ( [0] => 120 [1] => 2528 ) [airport] => Array ( [0] => 2700 [1] => 2529 ) [oc] => Array ( [0] => 86400 [1] => 1442364 ) [tr] => Array ( [0] => 10800 [1] => 1640016011 ) [plf] => Array ( [0] => 7200 [1] => 6712 ) [kill] => Array ( [0] => 3600 [1] => 1640019611 ) ) )
The way I tried to get the info:
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
foreach ($AccData as $playerinfo) {
echo $playerinfo['playerinfo']['rank'].'<br/>';
echo $playerinfo['character']['gta'].'<br/>';
}
EDIT:
The json string
{"playerinfo":{"rank":"Boss","cash":"€ 5,923,712","weapon":"M-16 (4500)","health":{"width":"100","color":"green"},"wealth":"Too rich to be true","protection":"Bulletproof Humvee","plf":"Huge","plane":"Concorde","crew":"None","pbf":"Large","ship":"None"},"character":{"crime":[120,122],"gta":[400,369],"drugs":[120,2582],"airport":[2700,2582],"oc":[86400,1640020450],"tr":[10800,1640016850],"plf":[7200,3935],"kill":[3600,1640020450]}}
Anyone knows how to do this ? For example I need the Concorde from plane in a variable and the time values from gta in a variable. And some more from this string.
So your first Part is okay and you rank you can just display like the first part as well
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['plane'].'<br/>';
echo $AccData['character']['gta'][0].'<br/>';
but the character is on the same level as playerinfo so you need to access it from AccData. also gta is an array like health, so you have to specify which value you want to show, the first so 0 or second which is 1
how i can get WP_Widget_Archives from array,
This is my array:
$control = Array
(
[name] => Archives
[id] => archives-6
[callback] => Array
(
[0] => WP_Widget_Archives Object
(
[id_base] => archives
[name] => Archives
[widget_options] => Array
(
[classname] => widget_archive
[description] => A monthly archive of your site’s Posts.
)
[control_options] => Array
(
[id_base] => archives
)
[number] => 8
[id] => archives-8
[updated] =>
[option_name] => widget_archives
)
[1] => form_callback
)
[params] => Array
(
[0] => Array
(
[number] => 6
)
)
[width] => 250
[height] => 200
[id_base] => archives
)
i have try with this code
`echo '<pre>'; print_r(array_keys($control['callback'])); echo '</pre>';`
but I get result like this
Array
(
[0] => 0
[1] => 1
)
where I think the result will be like this
$result = Array
(
[0] => WP_Widget_Archives Object
[1] => form_callback
)
so i can write $result[0] for get WP_Widget_Archives, please help me and thank you for your help :)
Probably you misunderstood array_key function. It will give you keys of the array not value. In your case you require value which is an object 'WP_Widget_Archives', so you can directly use $control['callback'][0].
I have an array, which looks like this:
Array
(
[sku_0017768] => Array
(
[0] => 0f359ce5bf6dc855f160c9b89b4aada0
)
[sku_0017766] => Array
(
[0] => ff5a47bd699bcb52944d0727a5b443b6
[1] => 0f359ce5bf6dc855f160c9b89b4aada0
)
[sku_0017723] => Array
(
[3] => a3c83832b4089d83d164b3cac596fc7e
)
[sku_0017767] => Array
(
[0] => fb91d14d405be52ce989acd2918ea1a0
)
[sku_8402350] => Array
(
[0] => a3c83832b4089d83d164b3cac596fc7e
[1] => fb91d14d405be52ce989acd2918ea1a0
[2] => ff5a47bd699bcb52944d0727a5b443b6
[3] => d14afce16e477663ebe51fdd65cd5010
)
[sku_8402200] => Array
(
[0] => d14afce16e477663ebe51fdd65cd5010
)
)
Now, this array represents a list of SKU's with one or more 'combination codes'. So, in this example 'sku_0017768' matches product sku_0017766 because they share the same combination code.
sku_8402350 has multiple combination as you can see.
What I need to have now is a new array, where the index(key) of the combination code needs to be same among the other products within the combination.
Output should look like this:
Array
(
[sku_0017768] => Array
(
[0] => 0f359ce5bf6dc855f160c9b89b4aada0
)
[sku_0017766] => Array
(
[0] => 0f359ce5bf6dc855f160c9b89b4aada0
[1] => ff5a47bd699bcb52944d0727a5b443b6
)
[sku_0017723] => Array
(
[0] => a3c83832b4089d83d164b3cac596fc7e
)
[sku_0017767] => Array
(
[0] => fb91d14d405be52ce989acd2918ea1a0
)
[sku_8402350] => Array
(
[0] => fb91d14d405be52ce989acd2918ea1a0
[1] => ff5a47bd699bcb52944d0727a5b443b6
[2] => d14afce16e477663ebe51fdd65cd5010
[3] => a3c83832b4089d83d164b3cac596fc7e
)
[sku_8402200] => Array
(
[2] => d14afce16e477663ebe51fdd65cd5010
)
)
The difficulty I have is that when a key is already in use, the key of the other products within the combination should increase (with 1) as well, until there is an empty key which the combination can use.
Can anybody help me pointing me in the right direction? Feel free to ask if anything is not clear.
I am reading a CSV file with Chinese characters,
and when i use print_r() it give me that type of array
Array ( [0] => PK!ȣ�4v�[Content_Types].xml ��(��T�n�0�W�?D�V��CUU]�-�L �IRP�h$.�����%�鮲�"�21J�"�{m�*�����H����z������f�����f�$ ) Array ( [0] => ORb^B�0�?)|��e\ɠ�Z��d����b2~�Bm [1] => %�;��*Y'�����**krE [2] => Tn��C2�Ear�>�T�b��4�T�4DÌqDl���?�t4�����*f�;+�#{�졗��F��io{S��mA�g�`�2k?Ly���&`Cwvݙ|��^z��v*u�Jw�}�\�Y�%����ACB$���qsk�MmQ6��-<���+ց�����_������ݥ㸋�G�|����g6 �����PK!�U0#�L�_rels/.rels ��(����N�0��H�C���nH���LH�!T�$����$#����Jc�����?[���iTb/Nú(A�3b{�jx��V�b"gi��aW��l_x���b���������#b4O��r��0Q�ahѓ�eܔ�=��P-<��j{�>�<���4Mox/�}b�N�#�;�v�Cf��ۨ�B�I�����"c�&�\O���8q"K��H��<ߊs#��.�h����<⧄�Md�a��T_��PK!�>����xl/_rels/workbook.xml.rels �(����J�0����nӮ""��E��j}��L��m2㟾���ۅe���f���2���k�&�WP%�&��w ) Array ( [0] => ޚ���[=� ) Array ( [0] => &$���W�4�K��H"�xR��㣔d�����NҨ9��ɨ�Aw(7ey/���O�� ) Array ( [0] => ��ނh�����m�| ) Array ( [0] => �}D�g"$�4��F�Y��.2#���5�9��鳔�Y]b��d��#��%�s�"�ݚ0�tB��)���[����ȓ�����PK!�ك�N"xl/workbook.xml�QAN�0�#�k��I�Z5����P�ce�Mcձ-�!��荓=;����ruj%�B�V���Js�|l^��8�gR+ [1] => ��V��Ͳ������� ) Array ( [0] => h�7J]�`��DT���m���3w �o%͒dF[&� ) Array ( [0] => � ]ע�']u-*?�X�̇�]#��rY���aƼ�6�}�#$s�����i��ǫ���c'dd�$Z��|��c�:�7��E=�M�l;c[���}!9�⺏�!���j'�o��9���������q�G~0|3�D �Ƙ��j�U~/��am1�up��.v��8 [1] => �hTLV�[<��l��������PK!���xl/sharedStrings.xml�Uێ�0}G� [2] => ���V [3] => J�*a�V�R)��ٵ'����d��A�'���nʶ��>�3ǚ��$�zhr�J��r6��B�*�_o�o(�Ȕ`�V�� Xz�>[���*���{E���2;�(ǔڴݧ�"�`���6ѫ��"j�T�p�+L��%���z��ח4��LcL�H�YaG[`��Z-z��Y�j�� j����k���p!�{�;�f5>��K�ƫY�=p_� [4] => �� w )�$�zŏ��A��e~CE��f`���4�� �5�q�"�o4O>e��7Z�/8k��`IQ�`&5�n���2:*c�C���� ȸ4�?�}Ua}Ft'�y Ȫ�F�ie�����8(/�G�#�G��r$ �ȡ`�:r��9����.9y89;�<���)��40��3P!�K���vY��;�]�5�%a������n�ﺱ����m7�3+�ד��s�}�������Ou�������PK!;m2K�B#xl/worksheets/_rels/sheet1.xml.rels�����0E��Cx{�օCS7"�U�b��ۗ���{�e���p��6��<�f� [1] => Ժ��ch��{�-�A�8�� -<�a�.��NNJ�ǐX��Q$��~�ٱ� ��>��I�y0���Ь�jm�_�/N�� [2] => �}W�:=RY��}�H9�E��bA�w��k}�m���� ��PK!�%��Sxl/theme/theme1.xml�YOo�6��w tom'�u�ر��M�n�i��XS�#�I}��úa���0l+��t�&[����HJ��K��ՇD"|���#u�ڃ��C"$�q۫]�z��>�8h{w��K� ) Array ( [0] => �c�xL�ޜH�����]ś*$A�>����J%����a��ACM��ʈ���J����&M�;��4B�e� t�Y�>c~4$��� ) Array ( [0] => &�^������ ) Array ( [0] => �L1�bma]���u��t���(gZ��[Wvr���2���u{���`�M� [1] => E���F��� [2] => ���2�n�Q�����%�[�N��Je�D �>֗��f}{����7����v��t�d��%|�J�Yw�2O��ڡ�~J=�L8�-�o|���(��<�4� �ժX��}.��#����'d�}��.�F�b�o\��C�\Ҽ��MT��0��z�����S�����ώ�t�����--g�.����~����?�~����xY����'���y92h!ы/����ɋ�>����%�m�GE��FD�[��t3�q%'#q��Sg�v � ) Array ( [0] => �9fe�q�wW#�(^��wd�b�h �a��8g.J pC�*Xx8��r�bV�`|Xƻ�cǵ�YU3J��ݐ8b�3+��(�������Q��u���K>Q�ELKM2�#'��vi~����vl�wu8+�z��HH�J���:�)��� ~��L��\�E\O*�t#G�1��l�m��~C�*u��G.R(:-�ys^D��i7�QR��8 [1] => b?�SQ���*��q7C�;��+�}��ݧ�;4pDZ��� �K(�N��h�wŘQ��6�㶷 [SYJ�(��p��»�g�>�X_�x���wu�{���\>k�]X���y�}�钣�M�26PsFnJ�'K� [2] => �}����䇦$�Ǵ�;�#`� �>�*�8���i"�LI%\������x�ӕ=6��������u=� r2f� ��3c�� ������(��:�jZ�3s��L�s��*��U��ܚЅ �]��M8�k�p6���������x�"]$C<&�����>�'e�b. vJ|��y�X����ɾ��8�Ȯ��]�7�R�/�=�� [3] => .&'��Q�k5��q��&p���(�K��a�ݐ�� �S��d��L17 jpSa����S!��� 3���5'+�Z�zQ ) Array ( [0] => �H )�7 �5)���k�dB|UtvaD�ξ�����p|�Fl&0�_�*�3�n'LE�/p���m���&]����8fI��r�S4�d7y��`� ) Array ( [0] => �n���ί�I�R���3U�~��c�nrF:_�*�P����}���-p�Tp�l�r��ۜ�4LZéO� ) Array ( [0] => �� ) Array ( [0] => !�P�L��B���]�$K ��*�++��6�5���v�ꦚ�e��NƟ��f�(�MN1ߜ��6����&3(��a��d��E [1] => �U�z�<�{���E�Uϲ���V���)�9�Z[��4^kd�5���!J���?��Q�3�qBo�C~���M����m<�.�vp�����IYӦ���Z�Y_p���=al-�Y�}Nc�͙���ŋ4vja��vl����'S�&�A�8�|�*~x������1%M0�g%����<���ҭ���PK!5��J? xl/styles.xml�[[��F~���`Y�c��V�.]���m�lԾ0�_�1���;��� [2] => �b��fm�|�;�9g����E���O7A��λ�i�� �gK/ ) Array ( [0] => �}~٢�R�(���.�Hf.�~rfĦ�i#4$�l�"�A%�.�����F'IV���k�'�bZ�֫1Y�qq`�kdU�䤫��t]Do��&������ yw���M3۵�~��I�x�7h�M��#������ټ�c�i�^3�&\�V0uO��K�{�ɝ�m<�P��Ϧύ�d��5����O_ȲU�(����aX-l���2�� [1] => ���ʼnQ���X�X�p+�ݙ_�Ro߱XiT�I�`A [2] => VS�)J����N��\��*�#��;����q8m��t8l�r�i��K��A]�7m̦E2p�"Y�Y#�����p8tz��`���aF���/�f�132���{���uW6���A�3���r��4mӮ�۫M^h�*[�����L�i���#�W���o8��{U`�ɫM^e=��*�c���#�W�������C�^h���j����s�$]��\5z��K��&��_fع����f��Β [3] => K" [4] => o��^��V9��{b$z�hS���5��0i����s��Pe�V�N�i�����76 ���E����d��f$۞W\�!��s;_�$i� ) Array ( [0] => �)�`�f�V��(CBqD:��5��: ) Array ( [0] => #Tu�ԩ�\�%�v�o캃v��.��z#�xMĜshϳCj [1] => zv��MK�!�̍G�>lY���z�P���:�qS�q�ŧ�\�䍽����Q�zT���_(�����:��ni��ȍ�hZ��%��+� [2] => �R����A��2��:�?o�����&�ƮR��=����`G>뽘9��4��y�n����1>�>�H��5�#�a'e{\#{�Z�.Z�#Δ�7����Ġ>ŧI������( �`�J�"]9���ҕ" ) Array ( [0] => �2�� ])R�+G ) Array ( [0] => �Е" ) Array ( [0] => ���)zBW�9�ʑ���)PЕ!O�7N�-�m�7Q����k����>*-���[O����S�q�+�VJ�Iw����֩���1�]j����n�n3i����� [1] => `У�c�j�b�j<����Bl�o��m�V>�F�9�npw�1�X�mr ) Array ( [0] => �̱ [1] => �9���X#(�(c X0�X�c����PE ) Array ( [0] => [1] => �'r���0���m�U��S�G���He^"�#�m*_���HY_��ʗbq?R��X�\���HEY�B�]�U�ѡ�α������Q�UJ�*�D [2] => �G9Vm�X���X%�/���T�^����Q��b�u�~�m�U��ی*G���9 ) Array ( [0] => ��� %�(�kr�ۊQ��p���(�w��= ��f��m�T����o�Ż���Q���:�b/Kҽ��� ) Array ( [0] => NvzW�$�l$#X8U!�'^q�۳�Rq�#�a�� �������蜍�P��s9���I�C��V�s)�n��A��_ȑ#g $U���m�zU��S�R4�3=Pa�)"%�|p�X>��iD��!$j���k� fd y^w�*��K��i��&Z*<^��+�7E�o/�i�HS�M�шon��_��s!����#�~�]�Kof/c�b��! [1] => �_}�'����#=��YL�����K����PK!����<�xl/worksheets/sheet1.xml��[s�0��;�����ܤ��&��8�7d� F�����>Dz��$��#����U�EQ&2Y��e��"'�"d����)�b��L��(������F��R���2dK����R�xّ�ȴ2�Ŋ+}Y [2] => �2/��V��u�?�O2��0(l��y���>V"S�$�H���/�I^��0N�V��S�yȠ7�G���a��c"6ek�(�6����5�(�_��:i�>�w�w}��zt�M]�HY?R-�#��ŧ��Ӽ~7��^�ݭ�ow0���N [3] => ��#Uwrs*��R��w{s�G�Bn Io��y��7��V��.T;Y���Qw��jѧv��zX;jk��m���q[�6ik}�U3$#�H$�ڢG="�zB"!�DB����+ !#��D�J�#�;���=B P����� ����O#*{��T�>A��}� ) Array ( [0] => �OY���)+T�>e��ߧ�P� ) Array ( [0] => % ��P� ) Array ( [0] => e ��P� ) Array ( [0] => � ��P� ) Array ( [0] => � ���Շ��d������4�xg��;�+G���!{h��-c���l��-'fK�I�����Y�̖s���l�4[�̖k���l�5[�̖��ro�<�-�/�T�Of˳��b���-�<�E �"�`I��$X� [1] => R ��\�E0�"�`M��&X� [2] => � ��|�E#�"�`Ѫ�"���iY��#�qɋE��N��"��t��S4=J=� S}�Ϝ7��\m�����7�v|�̥T��U�΄��Y$���;���PO�^a��!+�q��v�����PK!�vA�� 'xl/printerSettings/printerSettings1.bin�a�a(f(e�c(g��)�#V1Pԃ!�A���!�Ke(b��%#�C �`F&�;.�����b)#��!� ��� ���Dc�d5q������|#'+�10��F���b�7�'$��9X��� 3T *ƯS1�ili ) Array ( [0] => &6 �|:z�A���� #��.tqd>(�#�D#���#���LL-H�d=�� Gln�Sd�·ɡ�&>4r����Ԁ� [1] => l`j��PK!&i�^EMdocProps/core.xml �(���]K�0���C�}��u:CۉʮV�Br���h�o�uu�^��ɓ�R [2] => v��>�yit�H��47B�M���e �G����w����%V�`��0���J�'��p� C ) Array ( [0] => t�$���^�R���~���[�C8�;/'�m͇ۤ}�_WOè���]q#�~? �aկr-A�v��B��5�7�b�I�Q� �U)w���������^�*K� [1] => N�qJj��삒���~5 ���?ƼNg4��3rb< ) Array ( [0] => ����P}��PK!Ie\o�docProps/app.xml �(��R�J1��Ò{�m����"=��Qbv��&Kf��~����nՓ9��{�yy���o����.�LLƩH�ې;���z�8� ��Lŭ��P�j���𘉒��I�����1Þ�"ĭ!n�F��p��m����鵄=��!�߂�W�5�_�<����5֊w.��N��jiM�Lׅ��< �L�¸�Z54k�R� �Oc*�w��.�Dc�3�xYK뛮�j��_C���PI&�îr���ғ���9��0pnq�|.&��'Cǝ��oo��7~�|g�rH��v��?=9���z�1��Z�&B����#�9�X�"���ȏ�߀�������t��Qr0S��7���PK-!ȣ�4v[Content_Types].xmlPK-!�U0#�L�_rels/.relsPK-!�>����pxl/_rels/workbook.xml.relsPK-!�ك�N"�xl/workbook.xmlPK-!��� ) Array ( [0] => xl/sharedStrings.xmlPK-!;m2K�B#\xl/worksheets/_rels/sheet1.xml.relsPK-!�%��S^ xl/theme/theme1.xmlPK-!5��J? xl/styles.xmlPK-!����<� xl/worksheets/sheet1.xmlPK-!�vA�� '�xl/printerSettings/printerSettings1.binPK-!&i�^EM� docProps/core.xmlPK-!Ie\o�O#docProps/app.xmlPK&�% )
I encoded that CSV file with encode in UTF-8 without BOM.
For encoding, I just open csv file in notepad++ and then encode with encode in UTF-8 without BOM and then save it into CSV
Any kind of help will be appreciate.
I just spent half a day trying to figure this out and noticed this is still open; so for anyone looking for the answer I found changing the encoding type of my .csv files fixed it. To do this, I opened the file in excel, and saved to replace the file, this time selecting the UTF-8 CSV encoding option. I believe you can also change the file encoding type in apps such as Notepad++ if you don't have an office.
Hope this helps! Sorry if I missed anything, it's my first time posting on this site :D
I am using PHP 5.5.12.
I have an array like:
Array
(
[0] => Array
(
[user_id] => 3
[medicine_id] => 1
[time] => Array
(
[0] => stdClass Object
(
[event_type] => before_breakfast
[time] => 07:00:00
)
[1] => stdClass Object
(
[event_type] => after_breakfast
[time] => 07:30:00
)
)
)
[1] => Array
(
[user_id] => 3
[medicine_id] => 2
[time] => Array
(
[0] => stdClass Object
(
[event_type] => before_lunch
[time] => 13:00:00
)
[1] => stdClass Object
(
[event_type] => after_lunch
[time] => 14:00:00
)
)
)
[2] => Array
(
[user_id] => 3
[medicine_id] => 3
[time] => Array
(
[0] => stdClass Object
(
[event_type] => before_dinner
[time] => 20:00:00
)
[1] => stdClass Object
(
[event_type] => after_lunch
[time] => 21:00:00
)
)
)
)
I want to json_encode() the field time of each root level.
I tried using:
foreach ($user_medicine_times as $user_medicine_key => $user_medicine_value) {
$user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}
and:
foreach ($user_medicine_times as $user_medicine_key => &$user_medicine_value) {
$user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}
But using print_r($user_medicine_value), it returns the same array.
I want the result to be as follows:
Array
(
[0] => Array
(
[user_id] => 3
[medicine_id] => 1
[time] => "[{"event_type":"before_breakfast","time":"07:00:00"},{"event_type":"after_breakfast","time":"07:30:00"}]"
)
[1] => Array
(
[user_id] => 3
[medicine_id] => 2
[time] => "[{"event_type":"before_lunch","time":"13:00:00"},{"event_type":"after_lunch","time":"17:00:00"}]"
)
[2] => Array
(
[user_id] => 3
[medicine_id] => 3
[time] => "[{"event_type":"before_dinner","time":"20:00:00"},{"event_type":"after_lunch","time":"17:00:00"}]"
)
)
How can I achieve this result?
I have read your question earlier and prepared the answer but you removed it before i paste the answer. Anyways here is the solution
function outer(&$val, $key) {
$val['time'] = json_encode($val['time']);
}
array_walk($your_array, 'outer');
print_r($your_array);
You can replace your foreach loop's content with something like this:
foreach ($user_medicine_times as $user_medicine_key => $user_medicine_value) {
$user_medicine_times[$user_medicine_key]['time'] = json_encode($user_medicine_value['time'], true);
}
Maybe the json encode fails because your time array contains an stdClass Object. Try to convert this like that :
$result = array();
foreach ($user_medicine_value['time'] as $value) {
$result['event_type'] = $value->event_type;
$result['time'] = $value->time;
}
$user_medicine_value['time'] = $result;
Because, in every iteration, the value is not being saved anywhere,
You have two options here, either make new array having time key with json_encode() or pass the value by reference as shown below.
foreach ($user_medicine_times as $user_medicine_key => &$user_medicine_value) {
^
$user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}