Hi I have this array called $allextensionsforque. See print_r below.
Array ( [0] => Local/101#from-queue/n,0 [data] => Local/101#from-queue/n,0 )
I'm trying a foreach like below but its not showing any data on the echo. Can anyone help?
$allextensionsforqueu = mysqli_query($conqueue,"SELECT data FROM queues_details WHERE id = '1' AND keyword ='member'");
$allextensionsforque = mysqli_fetch_array($allextensionsforqueu);
$foo = "";
foreach ($allextensionsforque as $extensionrow)
{
$extensionrowstriped = substr($extensionrow['data'],6,3);
$foo = "' " . $extensionrowstriped . " ' ,";
}
echo $foo;
You don't have an array in $extensionrow, since it's only an one dimensional array. So you just need to replace $extensionrowstriped = substr($extensionrow['data'],6,3); with $extensionrowstriped = substr($extensionrow,6,3);.
Check for errors using mysqli_error(). Here is a simple approach, but you may want to improve it for production use:
$allextensionsforqueu = mysqli_query($conqueue,"...") or die(mysqli_error($conqueue));
Next, append your data to $foo instead of overwritting it in each loop:
$foo .= "' " . $extensionrowstriped . " ' ,";
Finally, verify that $extensionrow contains the data that you expect. substr() will return false if a match is not found.
Related
I have an array with just a single element. The key and value looks like this:
Array ( [test] => 12342 )
Result i want is this inside an variable:
test = '12342'
I tried following:
$test = "'" . implode(" ", $metric) . "'";
print_r($test);
But this gives only gives me: '12342', i wanted the '=' after key and '' around the value (this is used for SQL later on). So the result should be test = '12342'. Does not look like implode would work here. I tried looking at http_query_builder but failed.
If this array is only ever going to contain one single item, then you don’t need to loop through the data, but you can use key and current instead:
$data = ['test' => 12342];
echo key($data) . " = '" . current($data) . "'";
key gets you the key of the “current” item, and current gets its value.
By just replying your question as it is using that unique example you would do it this way assuming there is only 1 value inside your array :
$yourArray = array('test' => '12342');
foreach($yourArray as $key => $value) {
$test = $key . " = '" . $value . "'";
}
print_r($test);
If there are multiple you would do this, as each key is unique :
$yourArray = array('test' => '12342', 'testing' => '24321');
$test = array();
foreach($yourArray as $key => $value) {
$test[$key] = $key . " = '" . $value . "'";
}
var_dump($test);
I have a return string from a 3rd party plugin that looks like a var_dump from an array. I'm trying to parse into a valid associative array. Looking at various examples and doing some tests with the code that follows. The last segment demonstrates the problem I'm having. I'm trying to parse out the data into a string and then programmatically create an array after my data string has been finished. When I do a print_r on $vegetables2, I get:
Array([0] => “Gourd”=”40 kilojoules”,”Artichoke”=”105 kilojoules”,”Cassava”=”550 kilojoules”)
And the echo $vegetables2["Artichoke"] produces no value. Can someone please guide me at the correct syntax to create the array equivalent to the first two examples?
//this works:
echo "From creating the entire array with a static string:<br/>";
$vegetables = array("Gourd"=>"40 kilojoules", "Artichoke"=>"105 kilojoules", "Cassava"=>"550 kilojoules");
echo "Artichoke: " . $vegetables["Artichoke"] . "<br/>";
//this works too
$vegetables1['Gourd'] = "40 kilojoules";
$vegetables1['Artichoke'] = "105 kilojoules";
$vegetables1['Cassava'] = "550 kilojoules";
echo "From creating one element at a time:<br/>";
echo "Artichoke: " . $vegetables1["Artichoke"] . "<br/>";
//this doesn't work
$strData = "\"Gourd\"=\"40 kilojoules\",";
$strData = $strData . "\"Artichoke\"=\"105 kilojoules\",";
$strData = $strData . "\"Cassava\"=\"550 kilojoules\"";
echo $strData ."<br/>";
$vegetables2 = array($strData);
print_r($vegetables2);
echo "Artichoke: " . $vegetables2["Artichoke"];
$strData = "\"Gourd\"=\"40 kilojoules\",";
$strData = $strData . "\"Artichoke\"=\"105 kilojoules\",";
$strData = $strData . "\"Cassava\"=\"550 kilojoules\"";
$dd=str_replace('"','',"$strData");
$ff=explode(',',$dd);
foreach ($ff as $c)
{
$xx=explode('=',$c);
$vegetables2["$xx[0]"]=$xx[1];
}
print_r($vegetables2);
echo "Artichoke: " . $vegetables2["Artichoke"];
This string is your input:
"Array ( [Gourd] => 40 kilojoules [Artichoke] => 105 kilojoules [Casava] => 550 kilojoules )"
use strpos() to locate "("
use strpos() to locate ")"
use substr() to get what is between ( and )
use explode to divide the result wherever "[" appears
use array() to start a new array
use foreach to reformat each piece
use explode to divide each piece wherever "=>" appears
use trim to remove excess characters (left and right)
use $data[$key] = $value to create a new array to your liking
I have a array like that :
$myarray= 'zzz,aaa,bbb' ;
and want use it in mysql IN clause like that :
"... WHERE ids IN ($myarray) " // didnt work
"... WHERE ids IN ('$myarray') " // didnt work
the error im getting is that the first value in that array zzz says that zzz is not a column name . so i understand that i must separate the values with quotes to be like that :
$myarray= ' "zzz","aaa","bbb" ' ;
But i have no clue to do that . any help would be much appreciated.
$myarray = 'zzz,aaa,bbb';
$myarray = implode("','",explode(',',$myarray));
$query = "..... WHERE ids IN ('$myarray')";
You need to explode() your string and then implode() it -
$myarray = 'zzz,aaa,bbb';
$realArray = explode(',', $myarray);
$stringForIn = "'" . implode("','", $realArray) . "'";
echo "WHERE ids IN ($stringForIn)";
Try that :
$myarray= 'zzz,aaa,bbb' ;
echo implode '"' . ('","', explode(',', $myarray)) . '"';
I've been searching and searching and can't find anything that works, but this is what I want to do.
This code:
try{
$timeout = 2;
$scraper = new udptscraper($timeout);
$ret = $scraper->scrape('udp://tracker.openbittorrent.com:80',array('0D7EA7F06E07F56780D733F18F46DDBB826DCB65'));
print_r($ret);
}catch(ScraperException $e){
echo('Error: ' . $e->getMessage() . "<br />\n");
echo('Connection error: ' . ($e->isConnectionError() ? 'yes' : 'no') . "<br />\n");
}
Outputs this:
Array ( [0D7EA7F06E07F56780D733F18F46DDBB826DCB65] => Array ( [seeders] => 148 [completed] => 10 [leechers] => 20 [infohash] => 0D7EA7F06E07F56780D733F18F46DDBB826DCB65 ) )
And I want that seeder count into a string such as $seeds. How would I go about doing this?
Something like this?
$seeds = $ret['0D7EA7F06E07F56780D733F18F46DDBB826DCB65']['seeders'];
you can user strval() to convert a number to a string.
$string = strval($number);
or you can cast it into a string:
$string = (string)$number;
in your context that would be:
$string = strval($ret['0D7EA7F06E07F56780D733F18F46DDBB826DCB65']['seeders']);
However that odd string is also the first index of the array so you could also do it like this:
$string = strval($ret[0]['seeders']);
or if you want ot use only indexes ('seeders' is also the first index of the second array):
$string = strval($ret[0][0]);
if you just want the number then it's easy too:
$num = $ret[0][0];
It's not clear if you're looking to assign the array value(s?) as a separate variable(s?) or just to cast it into a string. Here's a nice way to accomplish all the above options, by assigning each array key as a separate variable with the matching array value:
$ret_vars = array_pop($ret);
foreach ($ret_vars as $variable_name=>$variable_value) :
${$variable_name} = (string)$variable_value;
endforeach;
In your original example, this would end up populating $seeders, $completed, $leechers and $infohash with their matching string values. Of course, make sure these variable names are not used/needed elsewhere in the code. If that's the case, simply add some sort of unique prefix to the ${} construct, like ${'ret_'.$variable_name}
This question already has answers here:
How can I bind an array of strings with a mysqli prepared statement?
(7 answers)
Closed 6 months ago.
I need to select ids from database with arrays.
my english not good. I think the best way to show my codes
the form result print_r($malayalam); like this Array ( [0] => helo [1] => hi[2] => how)
I need to select its ids from table. my code is not correct. any way let me show you here
$results=mysql_query("SELECT ml_id FROM ml_table WHERE word = '$malayalam'");
$numrows=mysql_num_rows($results);
if($numrows!=0){
$ml_row = mysql_fetch_array($results);
$ml_id = $ml_row['ml_id'] ;
echo "Malayalam ID " . $ml_id . "<br />";
}
I need to add all my result in to another array.
is that possible ?
if u have any idea could you answer to me please
If I understood properly, the following is what you need
$results=mysql_query("SELECT * FROM ml_table WHERE word = '$malayalam'");
if(mysql_num_rows($results)>0){
$newArray = array(); //Create a new array
while($ml_row = mysql_fetch_array($results)) {
$ml_id = $ml_row['ml_id'] ;
$newArray[$ml_id] = $ml_row;
echo "Malayalam ID " . $ml_id . "<br />";
}
//$newArray is your new array accesible from id
}
You should write something like this:
$ids = array();
$in = '"' . implode('","', array_map('mysql_real_escape_string', $malayalam)) . '"';
$result = mysql_query('SELECT id FROM ml_table WHERE word IN(' . $in . ')');
while($row = mysql_fetch_assoc($result)) {
$ids[] = $row['id'];
}
$anotherArray = array_merge($ids, $anotherArray);
OR
$anotherArray += $ids;
$i=0;
foreach($malayalam as $key=>$value)
{
$results=mysql_query("SELECT * FROM ml_table WHERE word = '$value'");
$numrows=mysql_num_rows($results);
if($numrows!=0)
{
$ml_row = mysql_fetch_array($results);
$ml_id = $ml_row['ml_id'] ;
$ml_ad[$i]=$ml_id;
echo "Malayalam ID " . $ml_id . "<br />";
$i++;
}
}
Now when you printr($ml_ad); it will show all your id's in an array.
finally fainally found solution with the help of answers
$rArray = mysql_query("SELECT ml_id FROM ml_table WHERE word IN ('".implode("', '", $malayalam)."')");
if(mysql_num_rows($rArray)>0){
$temp_rows = array();
while(($row = mysql_fetch_array($rArray))) {
$temp_rows[] = $row['ml_id'];
}
}
the result of print_r($temp_rows) coming like this Array ( [0] => 123 [1] => 234 [2] => 312)
thank to all