I have query like this
SELECT * FROM table WHERE id ='1'
but in column id store data like this
array (size=2)
0 => string '1' (length=1)
'id' => string '1' (length=1)
array (size=2)
0 => string '1,2' (length=3)
'id' => string '1,2' (length=3)
array (size=2)
0 => string '1,2,3' (length=5)
'id' => string '1,2,3'
Result of my query is show only 1 row but what I need is show every row that has value 1 in it.
plz help me with it
I think you can use LIKE %% to achieve this
SELECT * FROM table WHERE id LIKE '%1%'
Pull from the table as the current data, then:
$NewArr = array();
foreach ($Array as $values){
$NewArr[] = (int) #values;
}
Related
This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 3 years ago.
I have an array whith the following content:
array (size=5)
0 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.facebook.com' (length=24)
2 => string '4' (length=1) // order number
1 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.twiiter.com' (length=23)
2 => string '7' (length=1) // order number
2 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.instagram.com' (length=25)
2 => string '9' (length=1) // order number
3 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.linkedin.com' (length=24)
2 => string '2' (length=1) // order number
4 =>
array (size=3)
0 => string '1' (length=1)
1 => string 'https://www.pinterest.com' (length=25)
2 => string '1' (length=1) // order number
I want to sort this array based on the number in the above code. (where is a written comment).
How can I do this?
So far I have written the following code but I do not know how to make it properly.
$arrMerge = array_merge($facebookURL, $twitterURL, $instagramURL, $linkedinURL, $pinterestURL);
$splitArr = array_chunk($arrMerge, 3);
You can use array_multisort with array_column
array_multisort(array_column($arr, 2), SORT_ASC,$arr)
You can use SORT_ASC OR SORT_DESC as you required
Live DEMO
You can use usort.
usort($data, function($a, $b) {
return $a[2] - $b[2];
});
You might also want to type-cast the result to an integer before-hand as it looks like your data is treated as a string.
usort($data, function($a, $b) {
return (int)$a[2] - (int)$b[2];
});
This is also a possible duplicate of this question.
I have $monTab, an array with nested arrays like this in php :
array (size=12)
0 =>
array (size=2)
'mon' => string '2018-01-01 00:00:00' (length=19)
'nb_argus' => string '29' (length=2)
1 =>
array (size=2)
'mon' => string '2018-02-01 00:00:00' (length=19)
'nb_argus' => string '21' (length=2)
2 =>
I am simply trying to add this new pair of key value to each of the nested arrays :
'tx' => int '50' (length=2)
So i've built a for each like that :
foreach($monTab as $item) {
$item["tx"] = 50;
}
It doesnt work at all, var_dump($monTab) shows that nothing has happened !
the tx key is not added at all, the value is not added at all to my arrays !!
Due to the side effect of using pass by reference with foreach(...), using array_walk() or array_map() may be an idea.
array_walk($monTab, function(&$m){
$m['tx'] = 50;
});
I'm trying to replace this array:
$lts = ['20', '21'];
with an array from the database. I've tried a few things with no avail. Here's what I've got now:
$query = "SELECT id FROM members WHERE admin = '2'";
$lts = mysqli_fetch_all($con->query($query), MYSQLI_NUM);
When I var_dump it its giving me this:
array (size=2)
0 =>
array (size=1)
0 => string '20' (length=2)
1 =>
array (size=1)
0 => string '21' (length=2)
I need it to give me this:
array (size=2)
0 => string '20' (length=2)
1 => string '21' (length=2)
Any idea on how to get the latter array?
I just notice your returning array is same as the array you got,
never mind run this code
//run loop on your array after your query
for($i=0; $i<count($lts); $i++){
for($j=0; $j<count($lts[$i]); $j++){
$lts[$i]=$lts[$i][$j];
}
}
echo '<pre>';
print_r($lts);
I'm doing this query
public function getRecommendedVendors($user)
{
$q = $this->em->createQuery(
"
select cat.id
from Zgh\FEBundle\Entity\Category cat
inner join cat.users u
where u = :user
"
);
$q->setParameters(["user" => $user]);
var_dump($q->execute());
die;
return $q->execute();
}
Doing var_dump() returns:
array (size=2)
0 =>
array (size=1)
'id' => string '1' (length=1)
1 =>
array (size=1)
'id' => string '10' (length=2)
Where I want it to return because I use the result inside IN statement:
array (size=2)
0 =>
'id' => string '1' (length=1)
1 =>
'id' => string '10' (length=2)
What you experience is the default query behaviour: The first array “level” are the rows, the second “level” are the columns. Of course, you only have one column, therefore it looks a bit strange.
Instead of execute() you should use getScalarResult(). To learn more about this, read http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html#query-result-formats
I have a small tricky question with nested arrays. I am getting something like that from my database:
array
0 =>
array
'id' => string '81' (length=2)
'value' => string 'foobar' (length=6)
'created_at' => string '2012-02-18 22:09:57' (length=19)
'updated_at' => string '2012-02-18 22:09:57' (length=19)
1 =>
array
'id' => string '106' (length=3)
'value' => string 'barfoo' (length=6)
'created_at' => string '2012-02-19 15:11:47' (length=19)
'updated_at' => string '2012-02-19 15:11:48' (length=19)
What I want to achieve now is to extract a simple associative array, where one "column" becomes the key and one "column" becomes the value. For the case id / value, the result should then look like that:
array
81 => 'foobar'
106 => 'barfoo'
I know that I could do nested loops to foreach through all of the arrays, but I was wondering if there is a quicker and more native method. I was playing around with array_intersect, but it does not seem to deliver what I need.
Well, this one doesn't involve nested loops:
$result = array();
foreach($queryResult as $row) {
$result[$row['id']] = $row['value'];
}