I was wondering if someone could help me out.
Im extracting a string from my database which is formatted like so 4,3,1,7,38 etc etc ... its a dynamic array so it could be any number of numbers etc etc
What i need to do is extract each number and run it agains a function .. So for example each number represents a users id, i need to run each user id against a function and return a set result, so for each id i would neeed to run something like
$personsdetails = $this->random_model->get_user_details( $useridhere );
How would i go about getting each of the ids out of the array and running it over a loop to get each persons details?
Cheers
You can use explode and foreach over created array.
$userIds = explode(',', $yourstring);
foreach ($userIds as $userId) {
$personsdetails = $this->random_model->get_user_details( $userId );
}
$ids_raw = '1,2,3,4,5';
$ids = explode(',', $ids_raw);
foreach($ids as $id){
echo $id;
}
$var=explode(',',$database_value);
its comming in array
Because you're bringing in a single dimension array, I'd suggest using a for() loop (personal preference). The explode() function will split
a string by the first argument you give it. So in this case, explode is splitting the string in $userIDs by commas ','. Then we run each array element through your function.
$userIDs = '1,2,3';
$userIDs = explode(',',$userIDs);
for($x=0;$x<count($userIDs);$x++){
$personsdetails = $this->random_model->get_user_details($userIDs[$x]);
};
Related
Hi i have comma seperated id's in database.
I want to count/percentage them individually (GROUP BY) and wanna make pie chart. How to do it ?
Like, Example database picture is attached.
Id COUNT
1-----------6 OR some%
15----------3 OR some%
17----------2 OR some%
2-----------2 OR some%
6-----------2 OR some%
12----------1 OR some%
Try this
suppose you got data in $result
then
$result['count']=count(explode(",",$result['column_name']));
Beside the fact that your DB structure is a total mess you can try the following:
$arrData = [
"1","15,17","1","1","15,17","1","1","1,2","6,7,12,16","9,10","2,4,6,7","14,15,16"
];
$arrCounts = [];
array_walk($arrData, function($item, $key) use (&$arrCounts) {
foreach(explode(",",$item) AS $val)
{
if (!isset($arrCounts[$val])) $arrCounts[$val] = 0;
$arrCounts[$val]++;
}
});
ksort($arrCounts);
print_r($arrCounts);
Convert the variable containing the numbers seperated by a comma to an array with explode(), and count the items of the array.
$string = '1,2,3,4';
$array = explode(',',$string);
echo "String contains ".count($array)." items";
need to get only array values into an array.
i have array like.
and want to convert into another array like.
array('pic','picc','topic');
i have tried this but it gives me same result
$new_array = array();
foreach($tags as $val)
{
array_push($new_array, $val);
}
print_r($new_array);
You have to use implode() for your query and change the way you implement it:
$tags = implode("','", $array);
$this->db->query("UPDATE discuss_tags SET TotalPosts=TotalPosts-1 WHERE Name in ('".$tags."')");
The query will look like this:
UPDATE discuss_tags SET TotalPosts=TotalPosts-1 WHERE Name in ('pic','picc','topic')
PHP Manual: Implode
array('pic','picc','topic'); such an array only exists in pseudocode, not in PHP.
Arrays in PHP will always have numeric indexes unless you force string indexes.
Hard to phrase my question, but here goes. I've got a string like so: "13,4,3|65,1,1|27,3,2". The first value of each sub group (ex. 13,4,3) is an id from a row in a database table, and the other numbers are values I use to do other things.
Thanks to "Always Sunny" on here, I'm able to convert it to a multi-dimensional array using this code:
$data = '13,4,3|65,1,1|27,3,2';
$return_2d_array = array_map (
function ($_) {return explode (',', $_);},
explode ('|', $data)
);
I'm able to return any value using
echo $return_2d_array[1][0];
But what I need to be able to do now is search all the first values of the array and find a specific one and return one of the other value in i'ts group. For example, I need to find "27" as a first value, then output it's 2nd value in a variable (3).
You can loop through the dataset building an array that you can use to search:
$data = '13,4,3|65,1,1|27,3,2';
$data_explode = explode("|",$data); // make array with comma values
foreach($data_explode as $data_set){
$data_set_explode = explode(",",$data_set); // make an array for the comma values
$new_key = $data_set_explode[0]; // assign the key
unset($data_set_explode[0]); // now unset the key so it's not a value..
$remaining_vals = array_values($data_set_explode); // use array_values to reset the keys
$my_data[$new_key] = $remaining_vals; // the array!
}
if(isset($my_data[13])){ // if the array key exists
echo $my_data[13][0];
// echo $my_data[13][1];
// woohoo!
}
Here it is in action: http://sandbox.onlinephpfunctions.com/code/404ba5adfd63c39daae094f0b92e32ea0efbe85d
Run one more foreach loop like this:
$value_to_search = 27;
foreach($return_2d_array as $array){
if($array[0] == $value_to_search){
echo $array[1]; // will give 3
break;
}
}
Here's the live demo.
i have a problem i want to add slashes at the starting and the end of each string of my array.
This is an example of my actual array :
$patte = array();
$patte[0] = "httpd";
$patte[1] = "vsftpd";
$patte[2] = 'gohphp';
$patte[3] = 'abcdef';
i use this array for taking information into a DataBase so i can't place slashes now, or this is going to not working.
(mysql_query ... while mysql_fetch_array ...)
I need to rename these entry.
For this i use a second array, and with the command : "preg_replace" i can translate every strings like i want.
But preg_replace want me to add slashes in $patte
I want to obtain an array like this
$pattes = array();
$pattes[0] = "/httpd/";
$pattes[1] = "/vsftpd/";
$pattes[2] = '/gohphp/';
$pattes[3] = '/abcdef/';
Can you help me please.
I'm gonna have like 1000 line into this array.
Using array_map() you can apply callback to every element of your array :
function addSlashes($str)
{
return "/".$str."/";
}
$newArray = array_map("addSlashes", $patte);//array with the new values
Use array_map:
$pattes = array_map(function($str) {
return '/'.$str.'/';
}, $pattes);
I'm trying to get the result of my foreach loop into an url to do a simplexml_load_file with.
So it goes like this:
(...) //SimpleXML_load_file to get $feed1
$x=1;
$count=$feed1->Count; //get a count for total number of loop from XML
foreach ($feed1->IdList->Id as $item1){
echo $item1;
if($count > $x) {
echo ',';} //Because I need coma after every Id, except the last one.
$x++;
}
The two echo are just to see the result. It gives me something like:
22927669,22039496,21326191,18396266,18295747,17360921,15705350,15681025,15254092,12939407,11943825,11495650,10964843
I would like to put that in a url to make a simplexml_load_file just like that
$feed_url = 'http://www.whatevergoeshere'. $RESULT_OF_FOREACH . 'someothertext';
So it would look like:
$feed_url = 'http://www.whatevergoeshere22927669,22039496,21326191,18396266,18295747,17360921,15705350,15681025,15254092,12939407,11943825,11495650,10964843someothertext';
I've try to store it into an array or a function and then call it into the feed_url but it did not work the way I tried it.
I hope it's clear, I'll answer fast to questions if not.
Thanks.
It's really difficult to make out what you want, so I'm going to guess you want to store the list as a comma delimited string in a variable. the easiest way is to implode the array of ids
$ids = array();
foreach ($feed1->IdList->Id as $item1){
$ids[] = (string) $item1;
}
$RESULT_OF_FOREACH = implode(',', $ids);
$feed_url = 'http://www.whatevergoeshere'. $RESULT_OF_FOREACH . 'someothertext';