I have an array inside a php file in JSON format. I want to display the most frequent element of the name object:
<?php
$mydata = '[{"data":[{"name":"amr selim","phone":"12345"},
{"name":"ame selim","phone":"12345"},
{"name":"\u0639\u0645\u0631\u0648 \u0633\u0644\u064a\u0645","phone":"12345"},
{"name":"Amr Selim","phone":"12345"},{"name":"3mr","phone":"12345"},
{"name":"x","phone":"12345"},{"name":"\u0639\u0645\u0631\u0648\u0633\u0644\u064a\u0645","phone":"12345"},
{"name":"mr ","phone":"12345"},
{"name":"Amr sleem","phone":"12345"},
{"name":"\u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"12345"},{"name":"\u0639\u0645\u0631\u0648 \u0633\u0644\u064a\u0645\u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"12345"},{"name":"Tv l Pc","phone":"12345"},{"name":"\u0639\u0645\u0631\u0648 \u0633\u0644\u064a\u0645","phone":"12345"},{"name":"\u0625\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"12345"},{"name":"Amr Selim","phone":"12345"},{"name":"Tv pc","phone":"12345"},{"name":"\u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"0012345"},{"name":"Amr Selim","phone":"0012345"},{"name":"Efham Computer","phone":"0012345"},{"name":"Tv 3l Pc","phone":"0012345"},{"name":"\u0627\u0641\u0647\u0645","phone":"0012345"},{"name":"Amr Selim","phone":"12345"},{"name":"\u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"+12345"},{"name":"\u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"+12345"},{"name":"\u0645. \u0639\u0645\u0631\u0648 \u0633\u0644\u064a\u0645 \u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"12345"},{"name":"\u0645. \u0639\u0645\u0631\u0648 \u0633\u0644\u064a\u0645 \u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"12345"},{"name":"Amr Selim","phone":"12345"},{"name":"amr Selim","phone":"12345"},{"name":"amr Selim","phone":"12345"}],"info":[{"lastid":"437397286","usernum":"12345","cantry":"EG"}]}]';
$task_array = json_decode($mydata,true);
print_r(array_count_values($task_array[0]["data"]));
?>
I'm trying to get the most frequent text in the name object which in this case will be "amr selim".
I tried using this code but I get an error: "Warning: array_count_values(): Can only count STRING and INTEGER values!"
I don't know where to start, how to show the most frequent names in the array. can you help me
You should usearray_count_values
function as follows.
array_count_values(array_column($task_array[0]["data"], 'name'));
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.