Lets say I have an Array like this:
print_r($request_params);
Array (
[chat_id] => 120613381
[text] => Array (
[0] => saadat
[1] => saadat
[2] => saadat
[3] => saadat
[4] => saadat
[5] => saadat
[6] => donya
[7] => donya
[8] => donya
[9]
)
)
As you can see inside of [text] I have multiple values. Now I want to count that but I don't know how to access that part.
My try was this but it returns 0:
echo $num3 = count($request_params[2]["text"]);
So what is your suggest..
You don't need [2]:
echo count($request_params['text']);
I think you are looking for array_count_values.
It will return an associative array with the count of each name.
$counts = array_count_values($request_params['text']);
Var_dump($counts);
This should return for example:
[saadat] => 6
[donya] => 3
Related
I'm working on a php issue with an associative array and attempting to sort it. What I have looks something like this:
[2021-07-16T10:45:23-04:00] => Array
(
[0] => 0.0.1.222|ed7e434b79e54834440101fc07cc0981|Apples
[1] => 0.0.0.133|631dad007000b9ed59d41653176ac04b|Apples
[2] => 0.0.1.177|278670a3934717e4c005e02b9a4c1bab|Apples
[3] => 0.0.0.111|83b5ed42d2593aa37ad8c428e4f76288|Oranges
)
[2021-07-16T10:45:24-04:00] => Array
(
[0] => 0.0.1.222|ed7e434b79e54834440101fc07cc0981|Apples
[1] => 0.0.1.222|ed7e434b79e54834440101fc07cc0981|Apples
[2] => 0.0.1.177|278670a3934717e4c005e02b9a4c1bab|Apples
[3] => 0.0.1.177|278670a3934717e4c005e02b9a4c1bab|Apples
[4] => 0.0.0.133|631dad007000b9ed59d41653176ac04b|Oranges
[5] => 0.0.0.133|631dad007000b9ed59d41653176ac04b|Apples
[6] => 0.0.0.111|83b5ed42d2593aa37ad8c428e4f76288|Apples
)
I want to sort it so it looks like this:
[2021-07-16T10:45:23-04:00] => Array
(
[0] => 0.0.0.111|83b5ed42d2593aa37ad8c428e4f76288|Oranges
[1] => 0.0.0.133|631dad007000b9ed59d41653176ac04b|Apples
[2] => 0.0.1.177|278670a3934717e4c005e02b9a4c1bab|Apples
[3] => 0.0.1.222|ed7e434b79e54834440101fc07cc0981|Apples
)
[2021-07-16T10:45:24-04:00] => Array
(
[0] => 0.0.0.111|83b5ed42d2593aa37ad8c428e4f76288|Apples
[1] => 0.0.1.177|278670a3934717e4c005e02b9a4c1bab|Apples
[2] => 0.0.1.177|278670a3934717e4c005e02b9a4c1bab|Apples
[3] => 0.0.0.133|631dad007000b9ed59d41653176ac04b|Oranges
[4] => 0.0.0.133|631dad007000b9ed59d41653176ac04b|Apples
[5] => 0.0.1.222|ed7e434b79e54834440101fc07cc0981|Apples
[6] => 0.0.1.222|ed7e434b79e54834440101fc07cc0981Apples
)
I've thought about exploding along the | and then taking the 0.0.0.111 and comparing it to everything else. What I tried look like this:
foreach ($test_array as $info) {
asort($info);
}
I realized though that I comparing the whole: 0.0.1.222|ed7e434b79e54834440101fc07cc0981|Apples
instead of the first part 0.0.1.222. Thank you for any help.
Try array_map('sort', $yourMainArray); or run your own callback on the map or even a simple loop and sort would do it.
I have an arbitrary number of arrays all containing the same format of data. There are 2 separate for loops looping through two separate SQL query results and adding them to 2 separate arrays.
Once I have all the information in both arrays, I am walking through them and joining them together to make a longer array.
However, as I am writing this array to a csv file, The information needs to be in order in the array so it writes it in order to the csv file. How can I do this?
Array 1
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
)
//etc
Array 2
[1] => Array
(
[0] => RTGH707321222
[1] => THIS
[2] => IS
[3] => TEXT
)
[2] => Array
(
[0] => RTGH707321220
[1] => SOME
[2] => WORDS
[3] => HERE
)
//etc
Joining the arrays together
array_walk($array2, function($values, $key) use (&$array1) {
$array1[$key] = array_merge($array1[$key], $values);
} );
After The array Merge - print_r($array1)
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
[6] => RTGH707321222
[7] => THIS
[8] => IS
[9] => TEXT
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
[6] => RTGH707321220
[7] => SOME
[8] => WORDS
[9] => HERE
)
//etc
So this is working fine. However, I would like to move some of these indexes around so that they are in a different order. I have looked into array_splice() but I am not sure if this is the correct method to use.
What I want it to look like
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => RTGH707321222
[2] => TEXT
[3] => THIS
[4] => 18.36
[5] => 98.46
[6] => Foo
[7] => 32.63
[8] => IS
[9] => Bar
)
//etc
As you can see, all the information is still the same. The values have just been moved to different indexes. How can I sort the array so that it looks like the above. Can anyone point me in the right direction? Thanks.
This is a simpler method using array_replace() and an ordering array.
No extra loop, no temporary swapping variables.
Code: (Demo)
$array1=[
1=>['2017-07-21 00:00:00','Foo','Bar',32.63,18.36,98.46],
2=>['2017-07-21 00:00:00','Foo','Bar',29.74,148.68,178.42]
];
$array2=[
1=>['RTGH707321222','THIS','IS','TEXT'],
2=>['RTGH707321220','SOME','WORDS','HERE']
];
$order=[0=>'',6=>'',9=>'',7=>'',4=>'',5=>'',1=>'',3=>'',8=>'',2=>''];
array_walk($array2, function($values, $key) use (&$array1,$order) {
$array1[$key] = array_replace($order,array_merge($array1[$key], $values));
});
var_export($array1);
we can use swap technice here like,
<?php
foreach ($arr as $key => $value) {
$swap = $value[1];
$arr[$key][1] = $value[6];
$arr[$key][6] = $swap;
$swap = $value[9];
$arr[$key][9] = $value[2];
$arr[$key][2] = $swap;
$swap = $value[7];
$arr[$key][7] = $value[3];
$arr[$key][3] = $swap;
}
print_r($arr);
?>
$arr is your array.
This is my array () ...not much English'm using google translator.
I'm printing this array with print_r (). but what I deceo is sort of form this down
Array
(
[0] => Array
(
[0] => 606125999550609
[1] => Patricia
[2] => Michelle
)
[1] => Array
(
[0] => 724417787635260
[1] => Nasshy
[2] => Green
)
[2] => Array
(
[0] => 1121064174618668
[1] => Luisanna
[2] => Rodriguez
)
[3] => Array
(
[0] => 1057585894278115
[1] => Libane
[2] => Heredia
)
)
Basically what I need is to sort this array as follows......
So I do not know how to sort follows in PHP...
Array
(
[0] => 606125999550609
[1] => 724417787635260
[2] => 1121064174618668
[3] => 1057585894278115
[4] => Patricia
[5] => Nasshy
[6] => Luisanna
[7] => Libane
[8] => Michelle
[9] => Green
[10] => Rodriguez
[11] => Heredia
)
This isn't so much "sorting", its' more of a manipulation/restructure. Using a loop to regenerate your array would be the option, but if you can modify the data from where it comes from, then that's always recommended.
$new = array();
array_map(function($obj) use(&$new) {
foreach($obj as $i => $elem) {
$new[$i][] = $elem;
}
}, $array);
In the example above, we're using array_map() to apply our function() {... that runs the loop of each element, applying it to our $new array.
All you need to do is pass your $array in as you see above.
Example/Demo
I have two array as following
Array
(
[0] => 641
[1] => 622
[2] => 100
[3] => 6431
)
Array
(
[0] => 64.1
[1] => 62.2
[2] => 10
[3] => 643.1
)
How can I make it as following
Array
(
[0] => 641
[1] => 64.1
[2] => 622
[3] => 62.2
[4] => 100
[5] => 10
[6] => 6431
[7] => 643.1
)
It's as simple as
$result=array_merge($array1,$array2);
Note: Your values wont be in the order you presented though. If that is important then you need to loop through your arrays to build a new array accordingly.
Ummm ok here is that version as well
if(count($array1)==count($array2))
{
for($i=0;$i<count($array1);$i++)
{
$result[]=$array1[$i];
$result[]=$array2[$i];
}
}
print_r($result);
Fiddle
Manual
you can use array_merge() function merges one or more arrays into one array.
example:
array_merge(array1,array2,array3...)
Array ( [kanye] => Array ( [0] => Kanya [1] => Janaye [2] => Kayne [3] => Kane [4] => Kaye ) [wst] => Array ( [0] => ST [1] => St [2] => st [3] => EST [4] => West ) )
Array
(
[0] => Kanya
[1] => Janaye
[2] => Kayne
[3] => Kane
[4] => Kaye
)
Array
(
[0] => ST
[1] => St
[2] => st
[3] => EST
[4] => West
)
I've got those two arrays inside one array. The top array holds them both, then below is each one individually. When I am displaying the individual arrays, how do I echo their name?
So the first would be kanye, then list the contents, etc.
Hope that makes sense. I know it will be a simple bit of code but it's stumping me.
You can use a foreach statement to get the key value pair of the array:
$outer_arr = array('kanye' => array('Kanya', 'Janaye', 'Kayne', 'Kane'));
foreach($outer_arr as $key => $val) {
print($key); // "kanye"
print_r($val); // Array ( [0] => Kanya [1] => Janaye [2] => Kayne [3] => Kane )
}
If you just need to get the keys, you can use array_keys
$myArray = array(
"Kanye" => array("Kane", ...)
"West" => array("Wst", ...)
);
print_r(array_keys($myArray));
/*
array (
0 => Kanye
1 => West
)
*/
How about just print_r on the outer array?