I've got something I think it is a 3 dimensional array:
$var = '1,Tony,186|2,Andrea,163|3,Peter,178|4,Sally,172';
So there are 2 arrays packed inside the variable. First is separated by | the second one by ,
What I need to do is: separate them and then check for an ID located before the name and give me single variables of the rest.
I tried it like this:
<?php
$personid = 3;
$var = '1,Tony,186|2,Andrea,163|3,Peter,178|4,Sally,172';
$array = explode('|',$var);
foreach($array as $values) {
$arr = explode(",",$values);
foreach($arr as $value) {
if($value[0] == $personid) {
$id = $value[0];
$name = $value[1];
$height = $value[2];
$killloop = 1;
}
}
if($killloop == 1) {
break;
}
}
echo 'ID: '.$id.'<br> Name: '.$name.'<br> Height: '.$height;
?>
But all i get then is:
ID: 3
Name:
Height:
Can anyone help out?
There is no need loop Into Second Array, You already have defined positions.
Always Use break statement to end a Loop immediately, Its a good practice
<?php
$personid = 3;
$var = '1,Tony,186|2,Andrea,163|3,Peter,178|4,Sally,172';
$array = explode('|',$var);
foreach($array as $values) {
$arr = explode(",",$values);
if(!empty($arr) && $arr[0] == $personid){
$id = $arr[0];
$name = $arr[1];
$height = $arr[2];
break;
}
}
echo 'ID: '.$id.'<br> Name: '.$name.'<br> Height: '.$height;
?>
Related
I am using this code which calls the period_id :
$cart = $this->GetUserCart();
$dataset = array();
while ($data = $cart->fetch_array()) {
$dataset[] = $data['period_id'];
}
so I need to implode the result so they can be separated by comma
$p = implode(",",$dataset);
So in the usercart function will call for example, 3 distinct period_id
so I need to finish like this :
$p='2,5,3';
Your code is correct. Maybe you just have to echo it.
$dataset = array();
while ($data = $cart->fetch_array()) {
$dataset[] = $data['period_id'];
}
$p = implode(',', $dataset);
echo $p; //you just need to echo it
I think if
$p = implode(",",$dataset);
if $dataset = array("1", "5", "3");
when you echo $p ; //output will be 1,5,3
put if you add this :
$p = "'".implode(",",$dataset)."'";
echo $p ; //output will be '1,5,3'
and that is what you need if i understand you so
$p = '1,5,3';
Below is my code:
$id = $_GET['id'];
$qty = $_GET['qty'];
$product_id=$_GET['product_id'];
This is how I receive in the browser
http://example.com/shopping_cart.php?id=17,18&qty=4,5&product_id=3
$_SESSION['test'][]= array('product_id'=>$product_id,array('id'=>$id,'qty'=>$qty));
//print_r($_SESSION['test']);
foreach($_SESSION['test'] as $item=>$value)
{
echo "Main Array ID=". $item;
echo "<br/>";
foreach($value as $v=>$v1)
{
if(is_array($v1))
{
echo "Sub Array ID=". $v;
echo "<br/>";
echo "size id=". $v1['id'];
echo "<br/>";
echo "Quantity=". $v1['qty'];
echo "<br/>";
}
}
}
Output:
Main Array ID=0
Sub Array ID=0
size id=12,13
Quantity=1,2
Main Array ID=1
Sub Array ID=0
size id=17,18
Quantity=4,5
Since size_id and quantity are in implode form, I mean they have a comma ',' in between the value. I need to explode them and use foreach to display one by one.
I mean something like this:
$size_id1=explode(',',$v1['qty']);
foreach($size_id1 as $size_id2)
{
echo $size_id2;
}
$qty1=explode(',',$v1['qty']);
foreach($qty1 as $qty2)
{
echo $qty2;
}
What I need is, I want to display matching size_id and qty. For example, instead of displaying:
size_id 1
size_id 2
Qty 1
Qty 2
It should display:
Size_id 1 Qty 1
Size_id 2 Qty 2
How can I achieve this?
$ids = $_GET['id']
$qtys = $_GET['qty']
$product_id = $_GET['product_id'];
$tmp_result = array();
$ids = explode(',',$ids );
$i = 0;
foreach($ids as $id)
{
$tmp_result[$i]['id'] = $id;
$i++;
}
$i = 0;
$qtys =explode(',',$qtys );
foreach($qtys as $qty)
{
$tmp_result[$i]['qty'] = $qty;
$i++;
}
$_SESSION['tmp_results'][$product_id] = $tmp_result;
$temp_arr = array();
$id_str = '17,18';
$qty_str = '4,5';
foreach(array_combine(explode(',', $id_str), explode(',', $qty_str)) as $k => $v) {
$temp_arr[]['id'] = $k;
$temp_arr[]['qty'] = $v;
}
diffrence betwen 2 implode fetch from database
<?php
$select_tbl=mysql_query("select * from user_reg where uid='". $_SESSION['user1']."'");
while($fetch=mysql_fetch_object($select_tbl))
{
$r=$fetch->specialised;
$i=explode(",",$r);
$length = count($i);
for ($x=0; $x<$length; $x++)
{
echo $i[$x]; }
}?><tbody>
<?php
mysql_connect("localhost","root","");
mysql_select_db("freelancer");
$query=mysql_query("select * from post_proj where status = 1 and payment = '' ") or die(mysql_error());
$j = 1;
while($result=mysql_fetch_array($query))
{
if($result['uid'] == "")
{$d2 = $result['proj_skill'];
$pantry_food1 = explode(",",$d2);
$count_total1 = count($pantry_food1);
for ($counter=0; $counter<$count_total1; $counter++){
$line1 = each ($pantry_food1);
// echo $pantry_food1[$counter];
} $rrt = array_intersect($i,$pantry_food1);
Hello can u give me the right code for this one...
$split_getCreatedfield = explode(",", "3,1,2");
$fieldsWithValue = explode("~","1->Samuel Pulta~2->21~3->Male~");
for($row=0;$row<count(fieldsWithValue);$row++){
$data = explode("->", $fieldsWithValue[$row]);
}
I want the output like this one
3 = 3 = Male
2 = 2 = 21
1 = 1 = Samuel Pulta
<?php
$split_getCreatedfield = explode(",", "3,1,2");
$fieldsWithValue = explode("~","1->Samuel Pulta~2->21~3->Male~");
$result = array();
foreach($fieldsWithValue as $key => $val){
if(trim($val) != ""){
$res = explode("->",$val);
$res_key = array_search($res[0],$split_getCreatedfield);
$result[$key][] = $split_getCreatedfield[$res_key];
$result[$key][] = $res[0];
$result[$key][] = $res[1];
}
}
krsort($result); /// Not really required
echo "<table>";
foreach($result as $vals){
echo "<tr><td>".$vals[0]."</td><td>=".$vals[1]."</td><td>=".$vals[2]."</td></tr>";
}
echo "</table>";
?>
output:
3 =3 =Male
2 =2 =21
1 =1 =Samuel Pulta
I would rather use preg_match_all(), like this:
$i = '3,2,1';
$s = '1->Samuel Pulta~2->21~3->Male~';
preg_match_all('/(\d+)->(.*?)(?:~|$)/', $s, $matches);
$fields = array_combine($matches[1], $matches[2]);
foreach (explode(',', $i) as $index) {
if (isset($fields[$index])) {
echo $index, ' = ', $index, ' = ', $fields[$index]. PHP_EOL;
}
}
The regular expression matches items like 1->Samuel Pulta and builds an array with the number as the key and whatever comes after it as the value.
Then, you simply iterate over the necessary indices and print their corresponding value from the $fields array.
I have a function:
function getDecision($num_players, $decisionType, $stage){
echo "in the function 1: ".$num_players."<br/>";
echo "in the function 2: ".$decisionType."<br/>";
echo "in the function 3: ".$stage."<br/>";
$x = mysql_query("
SELECT `decisionValue` FROM `teamdecision` WHERE `decisionType` = '$decisionType' && `period`= '$stage'
")or die($x."<br/><br/>".mysql_error());
$y = array();
$i="0";
while ($i<($num_players) && $row = mysql_fetch_assoc($x))
{
$y[$i] = $row['decisionValue'];
$i++;
}
return ($y);
}
Y will be populated with a number of values depending on the $num_players. I am calling the function as follows:
$value = array();
$name = array();
for ($j=0;$j<$num_players;$j++){
for ($i=0;$i<4;$i++){
$name[0] = "SUconsultant";
$name[1] = "marketT";
$name[2] = "sector";
$name[3] = "saleprice";
echo $name[$i]."<br/>";
$value[$i] = getDecision($num_players, $name[$i], $currentStage)."<br/>";
echo "Value = ".$value[$j][$i]."<br/>";
}
}
As you can see I am doing this wrong. I need to output the data located in $value however I am confused with how multi dimensional arrays work. I understand I need to read up on the subject, if possible can you point me to a suitable learning source?
So I changed up the code so that the following is being used to produce an output below:
$value = array();
$name = array();
for ($j=0;$j<$num_players;$j++){
for ($i=0;$i<4;$i++){
$name[0] = "SUconsultant";
$name[1] = "marketT";
$name[2] = "sector";
$name[3] = "saleprice";
echo $name[$i]."<br/>";
$value[$i] = getDecision($num_players, $name[$i], $currentStage)."<br/>";
echo "Value = ".$value[$j][$i]."<br/>";
}
}
I expected to output as follows:
$value[0] should contain $y which then contains each SUconsultant value. $value[1] should contain $y which then contains each marketT value and so on.
for ($i=0;$j<4;$i++){
should be
for ($j=0;$j<4;$j++){
And
$value[$i] = getDecision($num_players, $name[$i], $currentStage)."<br/>";
should be
$value[$j] = getDecision($num_players, $name[$j], $currentStage)."<br/>";
You have a $j in your first for loop, but setting and incrimenting $i in it. Maybe change them to $j as well.
EDIT
It actually looks like you have an issue with where you are returning the data:
echo "Value = ".$value[$j][$i]."<br/>";
This should be (it appears):
for ($f = 0; $f < $num_players; $f++){
echo "Value = ".$value[$i][$f]."<br/>";
}
Since you are currently on $value[$i] by the time you reach this point, then you need it to echo out one for each player.
This solution worked, after a lot of playing around it seems that the [] after of
the value array was also needed? At least I think so
$value = array();
$name = array();
for ($j=0;$j<$num_players;$j++){
for ($i=0;$i<4;$i++){
$name[0] = "SUconsultant";
$name[1] = "marketT";
$name[2] = "sector";
$name[3] = "saleprice";
echo $name[$i]."<br/>";
$value[] = getDecision($num_players, $name[$i], $currentStage);
//echo "Value = ".$value[$i]."<br/>";
echo "<br/><hr/>";
echo "Value = ".$value[$i][$j]."<br/><hr/>";
}
}
Output:
And now I have changed to:
$value[] = getDecision($num_players, $name[$i], $currentStage);
echo "Team ".$j." ".$name[$i]." = ".$value[$i][$j]."<br/>";
To get my eventual goal of:
while($row=mysql_fetch_assoc($query)) {
$id = $row['id'];
$col1 = $row['name1'];
$col2= $row['name2'];
$col3= $row['name3'];
${"$id"} = array("$col1","$col2","$col3");
${"$s".$i}[] = ${"$id"};
}
This is just a breif example of what i'm trying to accomplish, $i is incremented somewhere else. I'm trying to implode the arrays in the array. So below I have imploded the main array but how do I implode the other arrays?
for($i=0;$i<11;$i++) {
$array = ${"s" . $i};
$outcomes = implode("",$array); //implodes main array
}
Not quite sure what you are trying to achieve here, but does this help?
function recursive_echo ($arr, $spacing = 0) {
$padding = ($spacing) ? str_pad('', $spacing) : '';
foreach ($arr as $key => $val) {
if (is_array($val)) {
echo "{$padding}{$key}:<br />\n";
recursive_echo($val, $spacing + 2);
} else {
echo "{$padding}{$val}<br />\n";
}
}
}
This function just echoes out the data, but it illustrates how to recurse through a multi-dimensional array where the number of dimensions is unknown.