php mysqli array issue - php

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);

Related

PHP array_merge result incorrectly on new line

I am trying to array_merge id’s from one array into another, it’s merging but putting the new values on a new array line:
IDs to be added: $attachment_ids
array (size=2)
0 => string '2620' (length=4)
1 => string '2621' (length=4)
IDs which already exist and will be added to: $existing_data
array (size=1)
0 => string '2589,2561,2432,2422' (length=19)
result of my array_merge:
$merged_data = array_merge($attachment_ids, $existing_data);
is
array (size=3)
0 => string '2620' (length=4)
1 => string '2621' (length=4)
2 => string '2589,2561,2432,2422' (length=19)
My expected result is:
array (size=1)
0 => string '2620,2621,2589,2561,2432,2422'
If you just need the 1 string with all of the values, you could use implode() with the result you have so far...
$merged_data = [ implode(",", $merged_data) ];
Shortly array merge with array_map
$result=array_map(null,$array1,$array2);
array_merge() requires 2 arrays. The existing data is all in a string. With explode, this data can be converted into an array. With implode() a list is created after array_merge().
$attachment_ids = ['2620','2621'];
$existing_data = ['2589,2561,2432,2422'];
$existing_data = explode(',',$existing_data[0]);
$result = [implode(',',array_merge($attachment_ids,$existing_data))];
//$result: array(1) { [0]=> string(29) "2620,2621,2589,2561,2432,2422" }
Update
Alternatively, strings can be chained manually.
$result = [implode(',',$attachment_ids).','.$existing_data[0]];

how to convert array in php [duplicate]

This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 5 years ago.
Hi i got this array from my database.
array (size=4)
0 =>
array (size=1)
0 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
1 =>
array (size=1)
0 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
And i need to do get like this
array (size=4)
0 =>
array (size=1)
email_1' => string 'denise#aaa.com' (length=18)
1 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
I tried with array_merge and all. But no idea how to archive this?
Do it like below:-
$final_array = array();
foreach($original_array as $key=>$val){
$final_array[$key][] = $val[0]['email_1'];
}
print_r($final_array);
Output:-https://eval.in/848213
In php it is possible to do like this
foreach ($yourArray as $arr){
$result[] = $arr[0];
}
You can get also your desired output like this:
$result = array_map('array_collapse',$yourArray);
For this just assign like this to its first element.
foreach($yourArray as $array){
$array = $array[0];
}

php select number in string

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;
}

PHP find and return neighbor value in multidimensional array

I have a multidimensional array like so:
array (size=4)
0 =>
array (size=2)
'term' => string 'news-article' (length=12)
'count' => int 139
1 =>
array (size=2)
'term' => string 'industry-resource' (length=17)
'count' => int 37
2 =>
array (size=2)
'term' => string 'editorial' (length=9)
'count' => int 33
3 =>
array (size=2)
'term' => string 'bulletin' (length=8)
'count' => int 12
and I'm trying create a function that searches for a term and returns it's neighboring value, count.
My inclination was to use array_search(), however using this returns false, I'm guessin because it's only searching the first layer of the array (0,1,2,3).
I'm not so much looking for an exact answer but a nudge in the right direction. I'm guessing it will require looping through the array, but I do not know how to approach getting the neighboring count value once the term value is located. Any help is appreciated!
You can just loop through the array and access them directly.
$search_term = "news-article";
$count = 0;
foreach($array as $element) {
if($element['term'] == $search_term) {
$count = $element['count'];
break;
}
}

Doctrine - query returns nested arrays instead of one

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

Categories