php


How to find the Array with the longest string length inside a multi dimensional array [closed]


Hi guys !
I have an multi-dimension array like this :
$arr =
Array
(
[0] => Array
(
['id'] => 1
['title'] => one test
)
[1] => Array
(
['id'] => 4
['title'] => two test xyz
)
[2] => Array
(
['id'] => 2
['title'] => fdfdf dfdfdffdfdf
)
[3] => Array
(
['id'] => 3
['title'] => fddddđ fffffffff ssss
)
)
Is there any way let get longest string and its length in any $array['title'] element ?
I want to get "fddddđ fffffffff ssss" and its length.
Please tell me any way let : get longest string , get longest length string and both. Thanks .
I see a way : max(array_map('strlen', $ary));
<== but it use for single array ,
in this topic i want there is any way let just find in $arr['title']
Update :
but if i want to get :
longest length string + longest string inside $arr['title']
largest "id" value number inside $arr['id'] , largest "id" value number now is 4 inside $arr[1]['id']
<== and all them are assigned to a new array like :
array(
'id' => 4,
'title' => 'fddddđ fffffffff ssss'
)
How to i do ?
Look carefully my update before you answer , thank
$max = '';
$maxlen = 0;
foreach ($arr as $elm) {
$len = strlen($elm['title']);
if ($len > $maxlen) {
$maxlen = $len;
$max = $elm['title'];
}
}
echo "The longest string is \"$max\" and it is $maxlen chars long.\n";
$string="";
foreach($arr as $list)
{
$string=strlen(list['title'])>strlen($string)?ist['title']:$string;
}
echo $string;
echo strlen($string)
you can find largest string also string length try it
$maxlen = 0;
$idxMax = null;
foreach($arr as $idx => $elem) {
if(strlen($elem['title']) > $maxlen)
$idxMax = $idx;
}
printf('Longest string: "%s" (%d), at index %d',
$arr[$idxMax]['title'],
strlen($arr[$idxMax]['title']),
$idxMax);
To answer your updated question, use $arr[$idxMax]['id']. If you want the array containing the string and its index, simply assign it to a new variable:
$longestEntry = $arr[$idxMax];
To combine the highest ID with the longest string, use:
$maxlen = 0;
$maxId = $arr[0]['id']; // assuming array has at least one element
$idxMax = null;
foreach($arr as $idx => $elem) {
if(strlen($elem['title']) > $maxlen)
$idxMax = $idx;
$maxId = max($elem['id'], $maxId);
}
$max = array(
'id' => $maxId,
'title' => $arr[$idxMax],
);
This is a version that will iterate over a multidimensional array and return the array of longest string it finds. Adapted on Sean Bright's code to keep things simple.
$arr = array(
"0" => array(
'id' => 1,
'title' => "one test"
),
"1" => array(
'id' => 2,
'title' => "two test xyz"
),
"2" => array(
'id' => 3,
'title' => "fdfdf dfdfdffdfdf"
),
"3" => array(
'id' => 4,
'title' => "fddddđ fffffffff ssss"
)
);
function longest_string(&$arr, &$max='',&$maxlen=false){
foreach ($arr as $elm=>$val) {
$len = strlen($val);
if ($len > $maxlen) {
$maxlen = $len;
$max = $arr;
}
if( is_array($val) ){
longest_string($val, $max, $maxlen);
}
}
return $max;
}
echo var_dump(longest_string($arr1));
// array(2) { ["id"]=> int(4) ["title"]=> string(22) "fddddđ fffffffff ssss" }

Related Links

Post data with ajax or ajax(json) to php in same page
PHP search multidimensional array with two keys for value of 3rd key?
Logging into phpMyAdmin error
Predict PHP rand() by having many outputs of it
I'm getting Erreur: SQLSTATE[HY000] [2002] No such file or directory
How to published moodle to Azure web app
Slideshare API 500 response for upload slideshow (www.slideshare.net/api/2/upload_slideshow)
Configuring Xdebug for using zero-configuration debugging in Intellij
Set layout for Profile Builder generated Registration page
How to find the line of PHP code which is using most CPU
How to exit composite field validation when Type constraint returns violation?
How to install php_wincache extension for php 7.1 on Windows?
How to fix: Error- No databse selected
Json_encode is not working for null value
How to check if something is countable?
`Undefined index` error complaining about variable values in foreach iterating array

Categories

HOME
amazon-web-services
elasticsearch
entity-framework
flask
vagrant
add-on
haproxy
kivy
stacktrace.js
plpgsql
biztalk
iverilog
cryptography
azure-data-lake
hyperledger
save
native-base
scheduler
driver
lc3
game-maker
android-externalstorage
xbap
mailgun
vmware-workstation
scala-ide
susy-sass
ratio
morris.js
list-comprehension
onchange
acrobat
pyspark-sql
tilemill
d3.js-v4
matlab-app-designer
phpstorm-2016.1
listjs
advanced-filter
gnu-classpath
adminer
creation
keyboard-layout
ms-solver-foundation
djcelery
ejml
elasticsearch-aggregation
docker-ucp
daz3d
bit-shift
wysihtml5
spongycastle
ol3-google-maps
ilrepack
karabiner
gmaps.js
kendo-treeview
hmmlearn
qtextedit
rational-performance-test
xcode6.4
nsrunloop
seccomp
jenkins-workflow
bbc-micro
android-syncadapter
node-glob
rails-engines
utf-16
certificate-authority
voronoi
pack
grinder
cryptojs
smart-tv
lowercase
apache-commons-digester
dsa
highest
android-handler
unidata
tkx
sunos
famo.us
cakephp-2.2
webmethod
full-text-indexing
nachos
jack
ivalueconverter
armv6
grails-2.3
mp4parser
codahale-metrics
html-frames
mongo-jackson-mapper
django-filebrowser
icenium
handshake
towers-of-hanoi
system-information
server-name
acpi
drawimage
representation
page-curl
3des
mongrel
pinax
levels

Resources

Encrypt Message