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.
Related
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);
contains the following i am trying get array_unique value from multidimensional associative array
Here, i am only showing only sample array which is similar to this.
$array = ['games'=>[['vollyball', 'football'], ['vollyball', 'football'], ['rubby', 'chess']]];
Here is tried so for
foreach ($array as $key => &$value) {
$value = array_unique($value);
}
echo "<pre>";
print_r($array);
echo "</pre>";
exit;
Here i am expecting output is,
$array = ['games'=>[['vollyball', 'football'], ['rubby', 'chess']]];
Here the array should be same even after removing duplicate from multidimensional array.
Thanks for your time and suggestions.
you could try the following:
$a=array_values(array_unique(array_merge(...$array['games'])));
This assumes that all your usable values are below $array['games'].
Edit:
Here is another way, using array_walk:
array_walk($array['games'],function($itm){global $res; $res[json_encode($itm)]=$itm;});
echo json_encode(array_values($res));
I don't like the global $res array very much, but this is a way forward, I believe. In the callback function of array_walk() I add all values to an associative array ($res). The keys are JSON representations of their actual values. This way I will overwrite identical values in the associative array $res and will end up with a set of unique values when I apply the array_values() function at the end to turn it back into a non-associative array.
The result is:
[["vollyball","football"],["rubby","chess"]]
Here is a little demo you can check out: http://rextester.com/JEKE60636
2. edit
Using a wrapper function I can now do without the global variable $res and do the operation in-place, i. e. removing duplicate elements directly from the source array:
function unique(&$ag){
array_walk($ag,function($itm,$key) use (&$ag,&$res) {
if (isset($res[json_encode($itm)])) array_splice($ag,$key,1);
else $res[json_encode($itm)]=1.;
});
}
unique($array['games']);
echo json_encode($array)
This will result in
{"games":[["vollyball","football"],["rubby","chess"]]}
see here: http://rextester.com/YZLEK39965
IN PHP i have the following arrays.
And i have this array:
What i want to do is append the second array to the first one,
i tried doing it like so:
end($array1);
foreach($array2 as $key => $value ){
$array1[$key] = $value;
}
With this result:
My question is; How do i put the the values and keys of the second array into the first one?
Answer: due to the answer of Manikiran i now got the following array as a result:
array_merge() is the function you are looking for. Try the following code:
$new_array=array_merge($array1,$array2);
For more information, check out the manual
I need to remove a element from an associative array with an string structure.
Example:
$array = array(
"one"=>array("Hello", "world"),
"two"=>"Hi"
)
I want to create a function that removes the elements like this:
function removeElement($p) {
// With the information provided in $p something like this should happen
// unset($array["one"]["hello"])
}
removeElement("one.hello");
Your base array is associative, the inner array (key one) is not, its a indexed array, which you can not access via ["hello"] but rather [0].
You can remove the hello value by using the unset function, but the indexes will stay as they are:
$array = ['Hello', 'World']; // array(0: Hello, 1: World)
unset($array[0]); // Array is now array(1: World)
If you wish to keep unset and keep the array indexes in order, you can fetch the values using the array_values function after unset:
unset($array[0]);
$array = array_values($array); // array(0: World)
Or you could use array_splice.
When it comes to using a string as key for multidimensional array with a dot-separator I'd recommend taking a look at laravels Arr::forget method which does pretty much exactly what you are asking about.
This would be a static solution to your question, in any case, you need to use explode.
function removeElement($p, $array) {
$_p = explode('.', $p);
return unset($array[$_p[0]][$_p[1]]);
}
But bear in mind, this doesn't work if you have more in $p (like: foo.bar.quux)
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