Remove duplicates from multidimensional array using Foreach loop - php

Hi im a beginner in php i would like to ask how do i remove duplicates from multidimensional array using foreach loop. what do i have have to put inside the print_r part.
while($row =mysqli_fetch_assoc($result3))
{
$nodecontainer {"id"} = ($row ["left_skill"]);
foreach ($nodecontainer as $id => $leftskill)
{
print_r($nodecontainer);
}
}

You can do it like below:-
$nodecontainer = [];//define array
while($row =mysqli_fetch_assoc($result3)){
$nodecontainer[] = $row ["left_skill"]; //assign all values to array
}
$nodecontainer = array_unique($nodecontainer);//remove duplicate
print_r($nodecontainer); //check final array
Note:- BTW I will recommend you to use DISTINCT or GROUP BY inside query to remove duplicates. No need to do anything then at PHP end.

Seems pretty simple: extract all id values into their own array, run it through array_unique:
$uniquePids = array_unique(array_map(function ($i) { return $i['id']; }, $holder));

If you want to use PHP, use array_unique()

Related

How do I loop through multidimensional JSON array in PHP?

I want to write code to loop through a multidimensional array (4 or 5 deep) and echo all keys and values found and skip empty arrays.
$drugs = fopen("http://dgidb.org/api/v2/interactions.json?drugs=FICLATUZUMAB", "r");
$json_drugs = stream_get_contents($drugs);
fclose($drugs);
$data_drugs = json_decode($json_drugs,true);
foreach ($data_drugs as $key => $value)
...
Anyone, anyone, Ferris?
Your $data_drugs is no longer a json, after json_decode is an associative array.
You don't need any loop to see keys and values
$data_drugs = json_decode($json_drugs,true);
print_r($data_drugs);
/* or if you don't like inline */
echo'<pre>';
print_r($data_drugs);
echo'</pre>';
You can use var_dump($data_drugs) - keys and values with types, probably you don't need this
But if you want to display keys and values more ...fancy use a recursive function
function show($x){
foreach($x as $key=>$val){
echo"<p>$key : ";
if(is_array($val)){ echo". . ."; show($val);}
else{ echo"$val</p>";}}}
show($data_drugs);

how to get only array values into another array

need to get only array values into an array.
i have array like.
and want to convert into another array like.
array('pic','picc','topic');
i have tried this but it gives me same result
$new_array = array();
foreach($tags as $val)
{
array_push($new_array, $val);
}
print_r($new_array);
You have to use implode() for your query and change the way you implement it:
$tags = implode("','", $array);
$this->db->query("UPDATE discuss_tags SET TotalPosts=TotalPosts-1 WHERE Name in ('".$tags."')");
The query will look like this:
UPDATE discuss_tags SET TotalPosts=TotalPosts-1 WHERE Name in ('pic','picc','topic')
PHP Manual: Implode
array('pic','picc','topic'); such an array only exists in pseudocode, not in PHP.
Arrays in PHP will always have numeric indexes unless you force string indexes.

Merge multiple arrays by index

Im some array that's being returned by some query.. and the result is something like this:
array(array('balance_1'=> '-5', 'balance_2'=>'-21'), array('balance_1'=> '-21', 'balance_2'=>'21'), array('balance_1'=> '-50', 'balance_2'=>'40'))
i want to transform this into an array that looks something like this:
array(array(-5,11,-50), array(-21, 21, 40));
basicly i want to join all balance_1, all balance_2, all balance_3 into separated arrays.
any ideas? thanks
You'll just loop over the list, then collect the values. It's most simple if you reuse the existing keys to group:
foreach ($list as $row) {
foreach ($row as $key=>$value) {
$out[$key][] = $value;
}
}
This way you'll get an $out array, with [balance_1] or [balance_2] holding the value lists.
Loop though the array and use "array_key_exists" if the key exists add to the array, if it doesn't build a new array with your index.
For more can be found here:
http://www.php.net/manual/en/function.array-key-exists.php

Storing array within an array using PHP

foreach ($topicarray as $key=>$value){
$files = mysql_query("mysqlquery");
while($file = mysql_fetch_array($files)){ extract($file);
$topicarray[$value] = array( array($id=>$title)
);
}
}
The first foreach loop is providing me with an array of unique values which forms a 1-dimensional array.
The while loop is intended to store another array of values inside the 1-dimensional array.
When the while loop returns to the beginning, it is overwriting it. So I only ever get the last returned set of values in the array.
My array ends up being a two dimensional array with only one value in each of the inner arrays.
Feels like I'm missing something very basic here - like a function or syntax which prevents the array from overwriting itself but instead, adds to the array.
Any ideas?
Step 1. Replace $topicarray[$value] with $topicarray[$value][]
Step 2. ???
Step 3. Profit
Make $topicarray[$value] an array of rows, instead of one row. Also, don't use extract here.
foreach ($topicarray as $key => $value) {
$rows = array();
$files = mysql_query("mysqlquery");
while($file = mysql_fetch_array($files)) {
$rows[] = array($file['id'] => $file['title']);
}
$topicarray[$value] = $rows;
}
Also, you should switch to PDO or MySQLi.

Returning an array of objects in a php function

I have multiple rows getting returned from a database query.
I am able to get just a row at a time, but I want to put the rows in an array of objects like this:
$trailheads[] = new StdClass;
Loop
{
$trailheads[] = $trailhead; // Put each object into the array of objects
}
But when I try to loop through each array, I am having trouble extracting the values of each row. What is the right way to loop through the returned object and get the values?
The code that does not work is pretty basic:
$returned_obj->o->trailhead_name
But I am actually hoping be able to loop through each array element.
Maybe try casting to array in your for loop, this is untested and may not work as is but should get you on the right track:
foreach ($trailheads as (array) $key){
// $key is now an array, recast to obj if you want
$objkey = (object) $key;
}
i'm assuming your using mysql since you did not say...
you can use thsi function mysql_fetch_object()
<?php
$trailheads = array()
$result = mysql_query("select * from mytable");
while ($row = mysql_fetch_object($result, 'trailhead')) {
$trailheads[] = $row;
}

Categories