I have this line
foreach($giftmessage as $key=>$value) {
$giftmessagearray['order_giftmessage_'.$key] = $value;
}
I want to add somewhere in the middle (I guess before the $key or the $value?) something that will print to my template.
I have tried this:
foreach($giftmessage as $key=>$value) {
$customtext = "something to echo there";
$giftmessagearray['order_giftmessage_'. $customtext .$key] = $value;
}
But it doesn't work, actually it breaks my query.
Related
I am trying to check if a value already exists in an array with the in_array function but it is not working. When i assign the $companyalis variable to a string, lets say "Bay" it works. Please how can i solve this issue. Note: the $alias contains a value from an input field.
$companyalis = strtoupper($alias);
if (is_array($responses)) {
$data = [];
foreach ($responses as $val) {
$data[] = $val['alias'];
}
if (in_array($companyalis, $data)) {
echo "Alias is already defined";
}else {
echo "Alias does not exist";
}
}
}
If I understood your question correctly
$companyalis = strtoupper($alias);
if (is_array($responses)) {
$data = [];
foreach ($responses as $val) {
$data[] = $val['alias'];
}
if (in_array($companyalis, array_values($data))) {
echo "Alias is already defined";
} else {
echo "Alias does not exist";
}
}
$aliasList[] = "Other";
$aliasList[] = "manY";
$aliasList[] = "someTime";
$test = "Many";
if(in_array(strtolower($test),array_map('strtolower',$aliasList))) echo "Find";
else echo "Not Find";
I think this code can help you if I understood your question correctly.
The principle is to pass the search string in lowercase as well as all the elements of the array. Via the "strtolower" function applied thanks to "array_map" (thus on the whole array)
I have this array of objects:
$table=[{"count":"2","id_f":"2255"},{"count":"6","id_f":"5886"}];
I want to get the value of id_f of each object and check if this value exist in another array ,I tried with this but it gives me the wrong result:
foreach($table as $t){
if (in_array($t[$id_f],$array){
//dosomething}
}else{
//do something else
}
}
I also tried with this:
foreach($table as $t){
if (in_array($t->$id_f,$array){
//dosomething}
}else{
//do something else
}
}
I can't get the right result , I will appreciate any help.
Another approach without foreach loop:
<?php
$table=json_decode('[{"count":"2","id_f":"2255"},{"count":"6","id_f":"5886"}]');
$data = [10, 20, 2255];
array_walk($table, function($obj) use (&$data) {
if (in_array($obj->id_f, $data)) {
echo "+";
} else {
echo "-";
}
});
The output obviously is:
+-
You dont show a json_decode() anywhere in your code, thats the first thing to do with a JSON String, to decode it into a PHP data structure. In this case an array of objects.
$other_array = array('2255', '9999');
$table='[{"count":"2","id_f":"2255"},{"count":"6","id_f":"5886"}]';
$array = json_decode($table);
foreach ( $array as $obj ) {
if (in_array($obj->id_f, $other_array)) {
echo 'Found one ' . $obj->id_f . PHP_EOL;
} else {
echo 'No match for ' . $obj->id_f . PHP_EOL;
}
}
Results
Found one 2255
No match for 5886
There's no need for the dollar sign before your Object property name (actually, it won't work, except of course if $id_f is a real variable which has for value 'id_f', but somehow I doubt it) :
foreach ($table as $t) {
if (in_array($t->id_f, $array){
// do something
} else {
// do something else
}
}
it can be done like this:
to define the object array you can define like below with json string approach. or to define object is like this $table = new stdClass();
<?php
$table='[{"count":"2","id_f":"2255"},{"count":"6","id_f":"5886"}]';
$obj = json_decode($table);
$array=array("2555","2255");
foreach($obj as $t){
if (in_array($t->id_f,$array)){
//dosomething
}else{
//do something else
}
}
?>
I'm trying to convert JSON-Object like [{"text":"hallo"},{"text":"hello"}] into a string which should look like "hallo hello".
At the moment, I'm decoding the JSON-Object with json_decode($words, true);
The result is being sent to a function than, which looks like:
function assocToString($assoc)
{
$ergString="";
foreach($assoc as $key => $value)
{
if($ergString=="")
{
$ergString = $value;
}
else
{
$ergString .= $value;
}
$ergString .= " ";
}
return $ergString;
}
I get errors like "Array to string conversion" all the time, maybe someone of you could please as be as kind as to help me out?
This apparently mean that your array was an multi-dimension array. Try to put another foreach loop inside the existing one.
Use vardump or print_r to check how does the current data store in $assoc look like
foreach($assoc as $key)
{
foreach($key as $value){
if($ergString=="")
{
$ergString = $value;
}
else
{
$ergString .= $value;
}
$ergString .= " ";
}
}
Try this see wether working
I'm going crazy, spent a couple of hours trying different methods in replace values in arrays, but I can't get it to work.
foreach($potentialMatches as $potentialKey)
{
$searchKeywordQuery = "SELECT keyword, id FROM picture WHERE id='$potentialKey'";
$searchKeywords = mysql_query($searchKeywordQuery) or die(mysql_error());
while ($searchKeyWordsRow = mysql_fetch_array($searchKeywords))
{
$keyword = $searchKeyWordsRow['keyword'];
$pictureKeywordArray[$searchKeyWordsRow['id']]['keywords'] = explode(",", $keyword);
$pictureKeywordArray[$searchKeyWordsRow['id']]['match'] = 4;
}
}
foreach($pictureKeywordArray as $key = > $picValue)
{
foreach($picValue['keywords'] as $key = > $picIdValue)
{
if ($picIdValue == $searchIdKey)
{
echo $picValue['match'];
$picValue['match']++;
echo $picValue['match'];
}
}
}
foreach($pictureKeywordArray as $key = > $picValue)
{
echo $picValue['match'];
}
I'm novice as you can see, When I echo the picValue['match'] in the foreach loop it gives me a correct value after "++". But then below when I call the array again it gives me the value of 4 instead of 5 as intended. Thanks in advance for any help with this.
Cause you work with the item copy in first case try $pictureKeywordArray[$key]['match'] instead of $picValue['match']
In that second foreach you need to call it by reference:
foreach($pictureKeywordArray as $key => &$picValue)
{ //^-- `&` makes it by reference
foreach($picValue['keywords'] as $key => $picIdValue)
{
if ($picIdValue == $searchIdKey)
{
echo $picValue['match'];
$picValue['match']++; //now updates what you want it to update
echo $picValue['match'];
}
}
}
foreach works on a copy of the data. You must use a reference to modify the original:
foreach ($foo as $i => &$f)
{
$f++;
}
unset($f); // important to do this if you ever want to reuse that variable later
My current code:
$file = fopen("countries.txt","r");
$array = array();
while(!feof($file)) {
$array[] = fgets($file);
}
fclose($file);
Here is my foreach loop:
$str = "test";
foreach ($array as $key => $val) {
if ($val == $str) {
echo $val;
} else {
echo "not found";
}
}
I am wondering why it is only printing $val if it is the last value of the array.
For example, it works if the txt file looks like this
test1
test2
test3
test
but doesn't work if it looks like this
test1
test2
test
test3
The problem is, that you have a new line character at the end of each line, so:
test\n !== test
//^^ See here
That's why it doesn't work as you expect it to.
How to solve it now? Can I introduce to you the function: file(). You can read a file into an array and set the flag to ignore these new lines at the end of each line.
So putting all this information together you will get this code:
$array = file("countries.txt", FILE_IGNORE_NEW_LINES);
$str = "test";
foreach ($array as $key => $val) {
if ($val == $str) {
echo $val;
} else {
echo "not found";
}
}
When you compare strings - you should always use '==='.