How can I use array_search with array_chunk? - php

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);

Related

Array values to single array using foreach loop in PHP

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
)

Put multiple arrays in one large associative 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 :/

Extract The Specific parameter value from array

I have a array like this
[0] => Array
(
[0] => LBLdss_COLLEsdfCTIONS_RsdfsdEPORT_TITfsdfLE
[1] =>
[2] =>
[3] => Array
(
[Administration] => Array
(
[bidsflldsf6] => Array
(
[0] => themes/Care2012/images/Payments
[1] => LsdfBL_OPsddfD_BIsfdfsLLING_SsdfdsUMsdfMARY_TITLE
[2] => LsdfsdBL_OPDfdsfd_BILfdsLING_dsfdsSUMMARY
[3] => ./index.php?module=Rsdfepfdsforts&action=reposfdfdsrtsel&rname=Opdpasfdfypdf
)
[bilhghjgl_pat_reg] => Array
(
[0] => themsdfsdes/Casfdfre2aasd012/imasdfges/Pasdymesddfnts
[1] => LBL_sdfsPAT_RsdfEG_TsdfITLE
[2] => LBL_PdfsdAT_sfdREG_TdsfdITLE_DsdfsETAIL
[3] => ./index.php?module=Rexcvpofdsrts&action=reportsel&rname=Pacxvtregcollxcvectionpdf
)
)
)
[4] =>
)
Now i need to extract value of rname from this index [3] => ./index.php?module=Rexcvpofdsrts&action=reportsel&rname=Pacxvtregcollxcvectionpdf (which is Pacxvtregcollxcvectionpdf in this case)and have to save it
I can try explode function but it is heavy for me since my array size is large
please help in resolving this
Thanks
Try placing a foreach for ciclying the array and formulate a regular expression for extract your param. Like this:
foreach($youvar as $text) {
preg_match('$rname=(.+?)$', $text, $rname[])
}
After that you can try a print_r on $rname and adapt to you.
Try below :-
$a = explode("rname=", index[3]);
echo $a[1;]
Code
$array = array(
'./index.php?module=Rsdfepfdsforts&action=reposfdfdsrtsel&rname=Opdpasfdfypdf',
'./index.php?module=Rexcvpofdsrts&action=reportsel&rname=Pacxvtregcollxcvectionpdf',
);
function getRname($str){
//return null if not found
preg_match('/rname\=([a-zA-Z]+)($|&)/', $str , $matches);
return $matches[1];
}
foreach($array as $str){
echo "Rname: " . getRname($str). "<br/>";
}
Result
Rname: Opdpasfdfypdf
Rname: Pacxvtregcollxcvectionpdf
Try it?

removing duplicates from an array in php

I want to remove duplicate elements from an array in php. Following is the structure of the array
Array
(
[0] => Array
(
[0] => xllga#hotmail.com
[1] => bounce#abc.com
[2] => 20120416135504.21734.qmail#abc.com
[3] => xllga#hotmail.com
[4] => info#abc.com
[5] => info#abc.com
[6] => xllga#hotmail.com
[7] => xllga#hotmail.com
)
)
How to go about it ?
Try array_unique.
Code :
<?php
$arr = array_unique($arr);
?>
array array_unique ( array $array [, int $sort_flags = SORT_STRING
] )
Takes an input array and returns a new array without duplicate values.
Try array_unique():
$newArray = array_unique($oldArray);
From the docs:
[array_unique()] Takes an input array and returns a new array without duplicate values.
http://php.net/manual/en/function.array-unique.php
$new_unique_array = array_unique($your_array[0]);
Hope that helps,
Stefan
No array_unique() solution. not so smart:)
array_keys(array_flip($array));
If use your array, $array = $yourArray[0];

calculations between two Multidimensional arrays

I have this code:
$id = new matrix(array(0=>array(1,0.5,3), 1=>array(2,1,4), 2=>array(1/3,1/4,1)));
$soma = $id->times($id)->sumRows();
That outputs this:
matrix Object ( [numbers] => Array ( [0] => Array ( [0] => 12.75 [1] => 22.3333333333 [2] => 4.83333333333 ) ) [numColumns] => 3 [numRows] => 1 )
and:
$total = $id->times($id)->sumRows()->sumTotal($id);
That outputs this:
matrix Object ( [numbers] => Array ( [0] => Array ( [0] => 39.9166666667 ) ) [numColumns] => 3 [numRows] => 1 )
Now, i am trying to make:
foreach ($soma as $value){
$final = (int)$value/(int)$total;
print_r ((int)$final);
}
The output will be 000.
Must be:
12.75/39.9166666667 = 0,3269230769230769
22.3333333333 / 39.9166666667 = ...
and so on
Thanks!
Just some ideas, without really knowing much about the matrix class...
All those (int)s should probably be (float)s, as you seem to want a non-int answer.
$value is itself an object, so you'll probably need to use $value['numbers'][0][0 or 1 or 2]. Same goes for $total.
the issue is solved :
documentation:
get_data($..)

Categories