I have been trying to count the last leaf node elements in a array.
Im thinking along the lines of:
Getting a simple array of leaf elements from the testArray using: 'array_walk_recursive'.
Count elements in the new array using: 'array_count_values'.
Im unsure how to get a simple list array from 'array_walk_recursive', i just get a long string of values....or is there a better way of achieving this result?
DESIRED RESULT:
flammable = 1
irritant = 2
toxic = 3
PHP:
$testArray = Array
(
[0] => Array
(
[0] => toxic
[1] => irritant
[3] => flammable
)
[1] => Array
(
[0] => toxic
[1] => irritant
)
[2] => Array
(
[0] => toxic
)
);
array_walk_recursive($testArray, function(&$value)
{
echo 'string = '.$value;
print_r(newArray); //How can i get this new array list?
});
$counts = array_count_values($newArray); //and use this to count values?
Try this, the numbers should show up in the $groups array.
$groups = array();
array_walk_recursive($testArray, function(&$value) use (&$groups)
{
if (isset($groups[$value])) {
$groups[$value]++;
} else {
$groups[$value] = 1;
}
});
print_r($groups);
Related
I am working with php and arrays, I have multiple arrays like following
Array
(
[0] => Array
(
[wallet_address] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[1] => Array
(
[wallet_address] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[2] => Array
(
[wallet_address] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
and so on....
And i want to make them in single array with comma like following way
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
How can i do this ?Here is my current code but not working,showing me same result(0,1,2 keys),Where i am wrong ?
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[]=$arr;
}
echo "<pre>";print_R($set);
The original array is an Assoc array and therefore the wallet_address needs to be addressed specifically in a loop. Or you could use the array_column() builtin function to achieve the same thing.
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[] = $arr['wallet_address'];
}
echo "<pre>";print_r($set);
Or
$new = array_column($GetUserFollower, 'wallet_address');
print_r($new);
RESULT
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
Your comments are making me think you want an array without a key, which is impossible. If you do this with the example you show in your comments
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
print_r($set);
You will see
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
I want to search and delete $srch_data data from the $list, but array_search() is not working. What's going wrong?
$srch_data = 'neha,neha#xyz.com';
$list = "gaurav,gaurav#xyz.com,neha,neha#xyz.com,ayush,ayush#xyz.com";
$arr = explode(',',$list);
$list_array = array_chunk($arr,2);
$pos = array_search($srch_data,$list_array);
echo $pos;
The problem is that you are searching with different things. Once you have used array_chunk(), your data is...
Array
(
[0] => Array
(
[0] => gaurav
[1] => gaurav#xyz.com
)
[1] => Array
(
[0] => neha
[1] => neha#xyz.com
)
...
and you are searching for
'neha,neha#xyz.com'
so this will not match.
If you converted your search string to an array as well, this will work...
$pos = array_search(explode(",", $srch_data),$list_array);
I am creating a set of arrays with the following loop:
$assessmentArr = explode("&", $assessmentData);
foreach($assessmentArr as $data) {
$fullArr = explode("_", $data);
// Break down to only archetype and value
$resultArr = explode("=", $fullArr[2]);
//print_r($resultArr);
}
Which produces the following results:
Array
(
[0] => community-support
[1] => 24
)
Array
(
[0] => money-rewards
[1] => 30
)
Array
(
[0] => status-stability
[1] => 15
)
Array
(
[0] => personal-professional-development
[1] => 32
)
Array
(
[0] => community-support
[1] => 9
)
Array
(
[0] => money-rewards
[1] => 12
)
Array
(
[0] => status-stability
[1] => 16
)
Array
(
[0] => personal-professional-development
[1] => 29
)
I need to combine these into one array, and where the [0] value matches, I need to add the [1] value together.
So I would like the final output to be something like:
Array
(
[community-support] => 33
[money-rewards] => 42
[status-stability] => 31
[personal-professional-development] => 61
)
I found this question: How to merge two arrays by summing the merged values which will assist me in merging and adding the values together, but I'm not sure how to go about it when the arrays aren't assigned to a variable. Is what I am trying to do possible or am I going about this the wrong way?
Don't make it complicated, just check if the results array already has an element with that key and if not initialize it otherwise add it. E.g.
(Add this code in your loop):
if(!isset($result[$resultArr[0]]))
$result[$resultArr[0]] = $resultArr[1];
else
$result[$resultArr[0]] += $resultArr[1];
Then you have your desired array:
print_r($result);
You could do it like this
$assessmentArr = explode("&", $assessmentData);
$finalArr = array();
foreach($assessmentArr as $data) {
$fullArr = explode("_", $data);
// Break down to only archetype and value
$resultArr = explode("=", $fullArr[2]);
if(array_key_exists($resultArr[1], $finalArr)){
$finalArr[$resultArr[0]] += $resultArr[1];
}else{
$finalArr[$resultArr[0]] = $resultArr[1];
}
}
First check, if the key already exists in the array, if so you add the value to the value in the final array. Otherwise you add the new index to the final array, with the value from resultArr as inital value.
... way too slow :/
Note, array_chunk is not my solution (It seems to me).
I have an array of about 150.000 elements
Array
(
[0] => Array
(
[name] => Danilo
[phone] => 33568
)
[1] => Array
(
[name] => Alessandro
[phone] => 392222
)
[2] => Array
(
[name] => Alex
[phone] => 3922
)
[3] => Array
(
[name] => Capa
[phone] => 392
)
)
And so on. I would split this array in several arrays, of (for example) 3.000 elements every one.
I saw array_chunk, but it returns a single array with several subarray.
I need several subarray to store them in a database and elaborate in future.
I'm getting crazy to write a snippet starting from that $temp and divide it into smaller array.
$size_chunks = 1;
$temp = array_chunk($recipients, $size_chunks);
foreach ($temp as $key=>$value)
{
if ($key<$size_chunks)
{
$to_store[] = $temp[$key];
}
//print_r($to_store);
// pseudo sql
// INSERT INTO table (sub_recipient) VALUES ($to_store);
$to_store = array();
}
So, every time that for loop end, reduce temp, store $to_store array and restart for others chunks.
Thank you very much.
PS in my example chunk==1 because starting array is small... ;)
With my example of chunk = 1, I need from starting array this 4 arrays:
Array
(
[0] => Array
(
[name] => Danilo
[phone] => 33568
)
)
Array
(
[0] => Array
(
[name] => Alessandro
[phone] => 39222
)
)
Array
(
[0] => Array
(
[name] => Alex
[phone] => 39222
)
)
Array
(
[0] => Array
(
[name] => Capa
[phone] => 392
)
)
Another explain
1 - With a starting array of 15.000 elements, and chunk of 3.000, I need in output (15.000 / 3.000) = 5 arrays. I will save them in database, so in DB I will have 5 rows (a row for every array).
2 - With a starting array of 4 elements, and chunk of 1, I need in output (4 / 1) = 4 arrays. I will save them in database, so in DB I will have 4 rows (a row for every array).
array_chunks() already does what you want, you just have to save it:
$chunks = array_chunk($array, $size_chunks);
foreach ($chunks as $chunk) {
// save $chunk to your database
}
$recipients = Array(
Array("fdbvfdb","dsacsdcds"),
Array("hrloo","dacdsc"),
Array("dcsdc","adcsd"),
Array("dcsdc","adcsd")
);
$total = count($recipients);//count 150.000 elements
$i=1;
for($i=0;$i<$total;$i++){
$O = array_slice($recipients,$i,1);
print_r($O);
//Your insert/Save code
}
you can use this code there is uses Array_Slice
I'm grabbing a some XML data and parsing it with PHP.
Most of the results come in multidimensional array but occasionally I'll get a result in a single array and it breaks my script.
I'm trying to format the single result to match the format of the results in the multidiminsonal array but I'm not having any luck.
Here is what I got:
Array
(
[name] => Steve Jobs
[id] => 3
)
Array
(
[0] => Array
(
[name] => Steve Jobs
[id] => 6
)
[1] => Array
(
[name] => Bill Gates
[id] => 8
)
)
I'm trying to format the single result to match the multidimensional format then flatten...
Array
(
[0] => Array
(
[name] => Steve Jobs
[id] => 3
)
[1] => Array
(
[name] => Steve Jobs
[id] => 6
)
[2] => Array
(
[name] => Bill Gates
[id] => 8
)
)
I've tried this:
$array_check = #array_keys($result[0]['name']);
if ($array_check[0] == "0") {
$result;
} elseif ($array_check[0] == "name") {
$ReWrite = array ([0] =>
array (['name']=>
array ($result[0]['name'])
));
$result = $ReWrite;
}
I thought that would do it but it's off...
Try this:
$array_check = #array_keys($result[0]['name']);
if (!isset($array_check[0])) {
$result[] = $array_check;
} else {
$result = $array_check;
}
var_dump($result);
If your first array was assigned to the variable $singleArray and your target results stored in $results, try this array_push($results, $singleArray);
Use this together with reset() it returns the first element of an array:
if(!is_array(reset($result))){
array_push($results, $result);
}
This will test if the array does not contain an array as an element, if it doesn't push the whole array to an aggregate array.
Edit: Try this loop:
for($i = 0; $i <= count($multi); $i++){
$arr = $multi[$i];
if(!is_array($arr)){
$multi[$i] = array($arr);
}
} var_dump($multi);