Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How i can combine ad array of values into an array with double combination string without duplications ?
For example, if i have something like:
array('one','two','tre','four','five');
I want to obtain an array of combinations like this ('one / two') just one time and not also ('two / one').
In this way i wanto to get something similar to:
array('one/two', 'one/tre', 'one/four', 'one/five', 'two/tree','two/four' .......
Suggestions ?
You can do it with this code. It won't show two/one, three/two, etc (that's how I understood it):
<?php
$array = array('one','two','tre','four','five');
$newArray = [];
foreach ($array as $el) {
foreach ($array as $el2) {
if ($el === $el2) continue;
$newArray[] = $el."/".$el2;
}
array_shift($array); // remove the element we just went through
}
print_r($newArray);
Demo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have two array i want to match a key of first array with another array and if the values of both key matches then add the value of second array to array 1
Array 1
[{"currency":1,"amount":23},{"currency":1,"amount":30},{"currency":2,"amount":40},]
Array 2
[{"currency_id": 1,"currency_symbol":$},{"currency_id":2,"currency_symbol":€}]
Desired output is:
[{"currency":$,"amount":23},{"currency":$,"amount":30},{"currency":€,"amount":40}]
The Code i am using is:
foreach($a1 as $key) {
foreach($a2 as $cKey){
if($a1['currency']==$a2['currency_id']){
$a1['currency_symbol'] = $a2['currency_symbol'];
echo $a1['currency_symbol'];
}
}
}
foreach($arr1 as $k=>$key) {
foreach($arr2 as $cKey){
if($key['currency']==$cKey['currency_id']){
$arr3[$k]['currency'] = $cKey['currency_symbol'];
$arr3[$k]['amount'] = $key['amount'];
}
}
}
var_export($arr3);
You only need to assign variable and print outside loop.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi i have this certain php function. But i cannot figured it out how to output this one. This is the question // Given an array, display each item in the array without using a loop. (Do not use built in functions to do this, like PHP's print_r; use a recursive function to implement)
and this is the code
<?php
function print_array(array $input)
{
}
?>
can someone share ideas on this? Any help is muchly appreciated.
you can use implode
$your_array = array("abc", "5", "xyz")
$text = implode(" ", $your_array); // implode with space you can use any other on this place
echo $text;
OUTPUT : abc 5 xyz
FUNCTION :
function print_array($your_array)
{
$text = implode(" ", $your_array); // implode with space you can use any other on this place
echo $text;
}
Recusive Method :
but only used when the key of the array is numeric starting with 0, 1, 2 ....
print_array($your_array);
function print_array($your_array, $index=0)
{
if(isset($your_array[$index]))
{
echo $your_array[$index];
$index+=1;
print_array($your_array, $index)
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Sorry for my english..
in mysql has row name iurl.. There is data: 1365269423.jpg,1365270586.jpg,1365270666.jpg,1365270683.jpg
i get its as:
<?php $s=mysql_query("select iurl from points where id='".$_GET['id']."' ");
if($s){
$array = array();
while($t=mysql_fetch_array($s)) {
$array[] = $t['iurl'];
}
print_r($array);
?>
it gives me result: Array ( [0] => 1365269423.jpg,1365270586.jpg,1365270666.jpg,1365270683.jpg )
And i`m need get it and print like a link
how can i do it?
Thanks..
You can use explode(); to split the string into an array and then loop over to print each item:
$images = explode(",", $t["iurl"]);
foreach ($images as $image) {
echo "{$image}";
}
I think you are asking to have result or in your case a .jpg file be the href of your link? If so do this:
......
The result you get from your database as you already figured out is an Array
The only thing you need to do is loop trough the array.
ps: consider using mysqli more info at php.net http://www.php.net/manual/en/book.mysqli.php
<?php
$s=mysql_query("select iurl from points where id='".$_GET['id']."' ");
while ($row = mysql_fetch_object($s)) {
echo '<img="http://yourhost.com/images/'.$row->image.' alt=""/>';
}
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
{"prereqs":{"prereq":{"type":"prereq_check","value":"submerging_island_feature_enabled"}},"divisions":{"division":[{"items":{"item":[{"name":"rhino_shell","rarity":"common"},{"name":"walrus_wavy","rarity":"special"},{"name":"hippo_fancyshell","rarity":"rare"},{"name":"rhino_jellyfish","rarity":"superRare"}]},"name":"rubyCount_30"},{"items":{"item":[{"name":"walrus_clam","rarity":"common"},{"name":"hippo_nautical","rarity":"special"},{"name":"giraffe_coral","rarity":"rare"},{"name":"elephant_starburst","rarity":"superRare"}]},"name":"rubyCount_40"},{"items":{"item":[{"name":"giraffe_waverider","rarity":"common"},{"name":"pony_sea","rarity":"special"},{"name":"magicdeer_seadeer","rarity":"rare"},{"name":"pony_seaprincesscorn","rarity":"superRare"}]},"name":"rubyCount_50"},{"items":{"item":[{"name":"bigcat_crystallion","rarity":"common"},{"name":"magicdeer_midnightdeer","rarity":"special"},{"name":"horse_ofthesea","rarity":"rare"},{"name":"horse_wingedsea","rarity":"superRare"}]},"name":"rubyCount_60"}]},"crafting":{"recipes":{"recipe":[{"name":"qdke"},{"name":"sb1p"},{"name":"cb8v"}]}},"listEndDate":"07/13/2015","currencyItem":{"name":"healingpotionbottle"},"feed":{"throttleTime":"21600"},"name":"submerging_island"}
To get you going my 2 cents. First off, Welcome, please read How to ask a good question
First you need to decode the json string in to an array. with that array you can get the values.
<?php
$json = '{"prereqs":{"prereq":{"type":"prereq_check","value":"submerging_island_feature_enabled"}},
"divisions":{"division":[{"items":{"item":[{"name":"rhino_shell","rarity":"common"},
{"name":"walrus_wavy","rarity":"special"},{"name":"hippo_fancyshell","rarity":"rare"},
{"name":"rhino_jellyfish","rarity":"superRare"}]},"name":"rubyCount_30"},
{"items":{"item":[{"name":"walrus_clam","rarity":"common"},{"name":"hippo_nautical","rarity":"special"},
{"name":"giraffe_coral","rarity":"rare"},{"name":"elephant_starburst","rarity":"superRare"}]},"name":"rubyCount_40"},
{"items":{"item":[{"name":"giraffe_waverider","rarity":"common"},{"name":"pony_sea","rarity":"special"},
{"name":"magicdeer_seadeer","rarity":"rare"},
{"name":"pony_seaprincesscorn","rarity":"superRare"}]},"name":"rubyCount_50"},
{"items":{"item":[{"name":"bigcat_crystallion","rarity":"common"},{"name":"magicdeer_midnightdeer","rarity":"special"},
{"name":"horse_ofthesea","rarity":"rare"},
{"name":"horse_wingedsea","rarity":"superRare"}]},"name":"rubyCount_60"}]},"crafting":{"recipes":{"recipe":[{"name":"qdke"},
{"name":"sb1p"},{"name":"cb8v"}]}},"listEndDate":"07/13/2015","currencyItem":{"name":"healingpotionbottle"},"feed":{"throttleTime":"21600"},"name":"submerging_island"}';
//decode the json
$decoded = json_decode($json, true);
// uncomment if you want it to be easier to read
// echo "<pre>";
// print_r($decoded);
// echo "</pre>";
//if you want the singe value, i.e. name of the first item.
echo "If you want a sinle value:<br>";
echo $decoded['divisions']['division'][0]['items']['item'][0]['name'] . "<br>";
//to get all names from one item you need to use a foreach() loop.
echo "<br>if you want all names from one item:<br>";
foreach($decoded['divisions']['division'][0]['items']['item'] AS $value){
echo $value['name'] . "<br>";
}
//to get all names we need to use 2 foreach loops because this is nested in multiple arrays
echo "<br>if you want all names from all items:<br>";
foreach($decoded['divisions']['division'] AS $value){
foreach($value['items']['item'] AS $value){
echo $value['name'] . "<br>";
}
}
?>