I am trying to find countries with conditions set as a variable, it looks like this:
$conditions = '1,2,3';
$this->set('options',
$this->Agency->Country->find('list',
array(
'conditions' => array(
'Country.zone_id' => array($conditions)
)
)
)
);
This should result in finding all the countries with zone_id = 1 or 2 or 3.
But in this case only the first value in the $conditions is taken to an account, so in this case it works like Country.zone_id' => array(1). It returns only the countries with zone_id = 1. Why all the other are skipped?
I think the value is converted to an int which means the rest after "1" is ignored. If an array is supplied then it behaves like an IN clause in SQL with each array entry as a value to be checked against.
So if you have to use a string with each ID separated by a comma then try the following:
'conditions' => array(
'Country.zone_id' => explode(",", $conditions)
)
Otherwise if you have already an array of numerical ID's then you can assign it directly:
'conditions' => array(
'Country.zone_id' => array(1, 2, 3),
)
Related
{
"word": ["w1", "w2"],
"meaning": ["m1", "m2"],
"parts_of_speech": ["p1", "p2"],
}
This is the Data i have in Model which is now assigned to $data variable
In above $data each word index has corresponding Meaning in meaning array, and each word index has corresponding parts_of_speech in parts_of_speech array index. In this way always length of word,meaning,parts_of_speech arrays remain same
I have tried the following methods to insert into table
$this->db->insert('table_words', $data);
$this->db->insert_batch("table_words",$data);
but it happened errors
CodeIginter accepts array for insert in which key should be 'column name' and value value should be record which we want to insert. As i can understand you have to insert multiple rows, So you have to use batch insert.
Try below code :
$data = array(
array(
'word' => 'w1' ,
'meaning' => 'm1' ,
'parts_of_speech' => 'p1'
),
array(
'word' => 'w2' ,
'meaning' => 'm2' ,
'parts_of_speech' => 'p2'
)
);
$this->db->insert_batch('mytable', $data);
Your json should be like this
[{"word":"w1","meaning":"m1","parts_of_speech":"p1"},{"word":"w2","meaning":"m2","parts_of_speech":"p2"}]
I have a routine in my code that computes the differences between two arrays in order create an SQL UPDATE statement.
As soon the routine starts I create a copy of the input array in order to manipulate it while the input one is kept untouched. When I'm ready to create the UPDATE statement I compute the difference between them, using the modified array as leading one.
In every test I ran both arrays were filled with key=value pairs and all values were strings, even when they're integers in the database (PDO behavior) and until now everything worked flawlessly.
But right now I've found something strange. Two different actions, in two different Controllers, but with the same logic are producing different differences:
This one works as expected:
$input = array(
'tid' => '3',
'enabled' => '1'
);
$modified = array(
'tid' => '3',
'enabled' => 0,
'modified' => '2014-11-26 15:17:55'
};
$diff = array(
'enabled' => 0,
'modified' => '2014-11-26 15:17:55'
);
$input is the result of a query. $modified is a copy of the first array manipulated through class methods. When I'm ready to create the statement, $diff is computed in order to send to PDO (or other driver) the correct statement.
Here, array_diff() works. the tid index is present in both array and it's ignored. The enabled, a simple on/off flag, is different and it's included. The datetime representation too.
But look the variables of this other case:
$input2 = array(
'sid' => '1',
'finished' => '0'
);
$modified2 = array(
'sid' => '1',
'finished' => 1,
'modified' => '2014-11-26 15:21:58'
);
$diff2 = array(
'modified' => '2014-11-26 15:21:58'
);
The same as before but with different field names. The sid is ignored but the finished is ignored too while it shouldn't because it is not present in the $input.
By replacing array_diff() with array_diff_assoc(), as expected, everything works, but why?
From the docs:
array array_diff ( array $array1 , array $array2 [, array $... ] )
Compares array1 against one or more other arrays and returns the
values in array1 that are not present in any of the other arrays.
In your example, $modified2 has an entry 'finished' which has value 1. But $input2 also has value 1 for key 'sid'. Thus, using array_diff($modified2, $input2) will result in the removal of every entry with value 1, no matter what the key is.
Using array_diff_assoc, it will only check to see if $input2 has the entry 'finished' => 1, which it does not, so the entry will not be removed.
This is the php code:
$slavesites = array(
'Category1' => array('Anchor1', 'http://www.test1.com'),
'Category2' => array('Anchor2', 'http://www.test2.com')
);
foreach($slavesites as $category => $slavesite){
echo $category;
foreach($slavesite as $anc => $url){
echo $anc.'<br>';
echo $url.'<br>';
}
}
The problem is when I run the code, i get a "0" and "1":
Category10 **--- WHERE DOES THE 0 COME FROM?**
Anchor1
1 **---- WHERE DOES THE 1 COME FROM?**
http://www.test1.com
Category20 --- WHERE DOES THE 0 COME FROM?
Anchor2
1 ---- WHERE DOES THE 1 COME FROM?
http://www.test2.com
Ty!:)
Hope you can help...
second foreach iterates over array without proper indices set. that way default indices (0,1,2,...) are used and hence the number in output.
e.g. actually your definition is like this:
$slavesites = array(
'Category1' => array(0 => 'Anchor1', 1 => 'http://www.test1.com'),
'Category2' => array(0 => 'Anchor2', 1 => 'http://www.test2.com')
);
you should use 'list' instead of 'foreach' in the inner loop:
list($anc, $url) = $slavesite;
If you want to loop through your array like that, you have to store the elements as key-value pairs:
$slavesites = array(
'Category1' => array('Anchor1' => 'http://www.test1.com'),
'Category2' => array('Anchor2' => 'http://www.test2.com')
);
The 0 and the 1 are shown because you don't have keys defined and it therefores uses numerical keys.
$string = "php, photoshop, css";
I'm producing an array from the comma separated values above using the str_getcsv() function:
$array = str_getcsv($string);
Result:
Array ( [0] => php [1] => photoshop [2] => css )
How can I replace the key integers with a string tag for all elements like seen below?
Array ( [tag] => php [tag] => photoshop [tag] => css )
Edit: if not possible what alternative can I apply? I need the array keys to be identical for a dynamic query with multiple OR clauses
e.g.
SELECT * FROM ('posts') WHERE 'tag' LIKE '%php% OR 'tag' LIKE '%photoshop% OR 'tag' LIKE '%css%'
I'm producing the query via a function that uses the array key as a column name and value as criteria.
That is not possible. You can have only one item per key. But in your example, the string "tag" would be the key of every item.
The other way arround would work. So having an array like this:
array('php' => 'tag', 'photoshop' => 'tag', 'css' => 'tag');
This might help you, if you want to save the "type" of each entry in an array. But as all the entries of your array seems to be from the same type, just forget about the "tag" and only store the values in a numeric array.
Or you can use a multidimensional array within the numeric array to save the type:
array(
0 => array( 'type' => 'tag', 'value' => 'php' ),
1 => array( 'type' => 'tag', 'value' => 'photoshop' ),
2 => array( 'type' => 'tag', 'value' => 'css' )
);
But still using just an numeric array should be fine if all the entries have the same type. I can even think of a last one:
array(
'tag' => array('php', 'photoshop', 'css')
);
But even if I repeat myself: Just use an ordinary array and name it something like $tag!
BTW: explode(', ', %string) is the more common function to split a string.
To build SQL statement you might do something like this:
// ... inside you build function
if(is_array($value)){
$sql .= "'".$key."' LIKE '%."implode("%' OR '".$key."' LIKE '%", $value)."%'";
} else {
$sql .= "'".$key."' LIKE '%".$value."%'";
}
This might look confusing but it's much cleaner than runnig into two foreach-loops building the query.
That won't work. Your array keys have to be unique, or subsequent additions will simply overwrite the previous key.
As the others said, keys have to be unique. Otherwise, which element should be returned if you access $arr['tag']? If you now say "all of them", then create a nested array:
$array = array();
$array['tag'] = str_getcsv($string);
The value $array['tag'] will be another array (the one you already have) with numerical keys. This makes, because you have a list of tags and lists can be represented as arrays too.
Understanding arrays is very important if you want to work with PHP, so I suggest to read the array manual.
Assuming you know the size of your array beforehand
$tags = array("tag1","tag2","tag3");
$data = array("php","photoshop","css");
$myarray = array();
for ($i=0; $i<count($data); $i++) {
$myarray[$i] = array($data[$i], $tags[$i]);
}
Then
echo $myarray[0][0] . ", " . $myarray[0][1];
Outputs:
php, tag1
What do
$categories[$id] = array('name' => $name, 'children' => array());
and
$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);
mean?
Thanks a lot.
How should i format the output so i can learn the results that was returned?
You can format your code into tables by looping on the array using for or foreach. Read the docs for each if you don't have a grasp on looping.
2.What does
$categories[$id] = array('name' => $name, 'children' => array());
and
$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);
The first line assigns an associative array to another element of the $categories array. For instance if you wanted the name of the category with ID of 6 it would look like this:
$categories[6]['name']
The second line does something similar, except when you are working with an array in PHP, you can use the [] operator to automatically add another element to the array with the next available index.
What is the uses of .= ?
This is the concatenation assignment operator. The following two statements are equal:
$string1 .= $string2
$string1 = $string1 . $string2
These all have to do with nesting arrays.
first example:
$categories[$id] = array('name' => $name, 'children' => array());
$categories is an array, and you are setting the key id to contain another array, which contains name and another array. you could accomplish something similar with this:
$categories = array(
$id => array(
'name' => $name,
'children' => array()
)
)
The second one is setting the children array from the first example. when you have arrays inside of arrays, you can use multiple indexes. It is then setting an ID and Name in that array. here is a another way to look at example #2:
$categories = array(
$parentID => array(
'children' => array(
'id' = $id,
'name' => $name
)
)
)
note: my two ways of rewriting are functionally identical to what you posted, I'm just hoping this makes it easier to visualize what's going on.