With api I get a multidimensional array php. I want to extract data from it with a simple code php.
I extract data from an array this way:
var_dump ($obj['product']);
echo $prise = ($obj['product']['return']['ProductsItem']['0']['Article']);
echo $Brand = ($obj['product']['return']['ProductsItem']['0']['Brand']);
echo $Currency = ($obj['product']['return']['ProductsItem']['0']['Currency']);
echo $Name = ($obj['product']['return']['ProductsItem']['0']['Name']);
echo $StockItem = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['0']['Price']);
echo $StockItem = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['0']['TransferTime']);
echo $StockItem1 = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['0']['Count']);
echo $StockItem2 = ($obj['product']['return']['ProductsItem']['0']['Stock']['StockItem']['1']['Count']);
How do I simplify the code? How can the data in the array change?
<?php
foreach ($obj['product']['return']['ProductsItem'] as $productsitem) {
$article = $productsitem['Article'];
$brand = $productsitem['Brand'];
$currency = $productsitem['Currency'];
$name = $productsitem['Name'];
foreach ($productsitem['Stock']['StockItem'] as $stockitem) {
$price = $stockitem['Price'];
$transfertime = $stockitem['TransferTime'];
$count = $stockitem['Count'];
}
}
Related
I have an associative array in php that looks like this
incSched={"62":"10878","63":"10878","64":"10878","65":"10878","66":"28416","67":"28416","68":"28416","69":"28416","70":"28416"}
How do I access the value for "62"?
I've tried
$amt = $incSched[$j];
$amt = $incSched["$j"];
$amt = $incSched[strval($j)];
they all return an empty string.
Here is the full loop so you can see what $j is:
$startAge = 53;
for ($i=0; $i<count($incTableIds); $i++) {
$tableId = $incTableIds[$i];
for ($j=$startAge; $j<101; $j++) {
$incSched = $incomeSchedules[$tableId];
echo "incSched=" .json_encode($incSched) ."<br>";
$amt = $incSched["$j"];
if ($amt == "") $amt = 0;
if ($j>$startAge) $amt = $incSchedules[$tableId][$j-1]; //use previous value
echo "Income: tableId=$tableId, age=$j, amt=$amt<br>";
$incSchedules[$tableId][$j] = $amt;
}
}
Here is the output when the loop hits 62
Income: tableId=1, age=62, amt=0
amt should be 10878, not 0. Any ideas?
You are using the JSON Format but without the quotes. In order to decode the JSON format, you'll have to convert your value into a JSON string. Try this below code:
// $incSched = '[{"62":"10878"},{"63":"10878"},{"64":"10878"},{"65":"10878"},{"66":"28416"},{"67":"28416"},{"68":"28416"},{"69":"28416"},{"70":"28416"}]';
$incSched = '{"62":"10878","63":"10878","64":"10878","65":"10878","66":"28416","67":"28416","68":"28416","69":"28416","70":"28416"}';
$incSched = json_decode($incSched, true);
foreach ($incSched as $key => $value) {
echo $key . '=>'. $value . '<br />';
}
In the above, I have given you both the valid formats of JSON.
Hope this helps.
I have a problem with array push.I cant get the all data from the array array.I want to get like this format.
[["username","average"],["aa",2.34],["bb",6.7],["hh",9.8]]
here is my code
while($acc_rs = mysql_fetch_array($acc_qry))
{
$acc_cnt = $acc_rs['Total_login'];
$time_stamp = $acc_rs['last_logged'];
$avg_login = $acc_rs['avg'];
$name = $acc_rs['name'];
$ji = array();
$sal = array("username","average");
$kk = array($name,$avg_login);
array_push($ji,$sal,$kk);
}
array_push($da,$new,$average);
$result = array(array('username', 'average'));
while ($row = mysql_fetch_assoc($acc_qry)) {
$result[] = array($row['name'], $row['avg']);
}
echo json_encode($result);
how can I keep post variables through all the functions in php class?
I'm accessing post variables in a array in function. But when I try to access those in another function it retrvies only the last element in array.
foreach ( $questionAry as $questionID )
{
$questionName = $this->htmlID_questionID . $questionID;
$question_answer = $_POST[$questionName];
$_SESSION['qusID'] = $questionID;
$_SESSION['qusanswer'] = $question_answer;
}
I want to access $questionID and $question_answer in another function. I tried to do it through session, but I can only access last value.
Your loop is wrong...these guys are getting overwritten on every iteration
$_SESSION['qusID'] = $questionID;
$_SESSION['qusanswer'] = $question_answer;
You need to use an indexed loop, something like so...
foreach ( $questionAry as $key => $questionID )
{
$questionName = $this->htmlID_questionID . $questionID;
$question_answer = $_POST[$questionName];
$_SESSION['qusID'][$key] = $questionID;
$_SESSION['qusanswer'][$key] = $question_answer;
}
EDIT: based on conversation below, try something like so.
function getvalues() {
$questions = array();
$jsonEncoded = ( get_magic_quotes_gpc() ) ? stripslashes($_POST[$this->htmlID_questionIDAry]) : $_POST[$this->htmlID_questionIDAry];
$jsonDecoded = json_decode($jsonEncoded);
$questionAry = explode(",",$jsonDecoded->list);
$instr = "";
foreach ( $questionAry as $key => $questionID ) {
$questions[$key]['name'] = $this->htmlID_questionID . $questionID;
$questions[$key]['answer'] = $_POST[$questionName];
}
return $questions;
}
$parsedQuestions = getvalues();
name_of_other_function($parsedQuestions);
function name_of_other_function($parsedQuestions){
print_r($parsedQuestions);
}
Hi all Im trying to query a database and store the results ($searchResults[]) as an array :
<?php
if(isset($_POST['indexSearchSubmit']))
{
foreach($_POST['industryList'] as $selected)
{
$_POST['industryList'] = $selected;
$locationListResults = $_POST['locationList'];
$results = mysqli_query($con,"SELECT * FROM currentListings
WHERE location = '$locationListResults' AND industry = '$selected'");
$searchResults = array();
while($row = mysqli_fetch_array($results))
{
$searchResults[] = $row['industry'];
$searchResults[] = $row['location'];
$searchResults[] = $row['title'];
$searchResults[] = $row['description'];
}
}
mysqli_close($con);
}
?>
the problem im getting is when I try to echo the result:
<?php
echo $searchResults[0];
?>
its only bringing back 1 result not displaying all the results in the arrray as i want it to.
Could anybody please point out what it is im doing wrong.
Any help would be greatly appreciated
Do like this
<?php
print_r($searchResults); // Prints all array elements
?>
Alternatively, you can make use of a for loop to echo all elements too..
foreach($searchResults as $k=>$v)
{
echo $v;
echo "<br>";
}
Your code puts your data into 1D array. You probably want sth else so instead of this:
$searchResults[] = $row['industry'];
$searchResults[] = $row['location'];
$searchResults[] = $row['title'];
$searchResults[] = $row['description'];
do this:
$tmp = array();
$tmp['industry'] = $row['industry'];
$tmp['location'] = $row['location'];
$tmp['title'] = $row['title'];
$tmp['description'] = $row['description'];
$searchResults[] = $tmp;
or just this (thanks to Barmar):
$searchResults[] = $row;
This way you store your data as 2D array. So every row you obtain remains in one subarray.
To print the row (which is now in 2D array) iterate over subarrayw:
foreach($one_of_searchResult_rows as $k => $v)
{
// do whatever you need with $k and $v
}
I think you want a 2d array. Try this code snippet :
$searchResults = array();
while($row = mysqli_fetch_array($results))
{
array_push($searchResults,$row);
}
This should push each row as an associative array in every cell of your final searchResuts array. You could then access the values like:
echo $searchResults[0]['industry']; //and so on
echo $searchResults[0]; //Should print out the first match/row of the sql result
Lets make it simple :
$serach_result=mysqli_fetch_all ($result,MYSQLI_NUM);
//you should use print_r while trying to print an array
print_r($search_result[0]);
Reference : mysqli_fetch_all
I need to build a multidimensional array from records retrieved from my database.
Here is the code I have:
<?php
/*
...some code...
*/
foreach($data['colleges'] as $college)
{
$college_temp[] = $college->name;
$college_temp[] = $college->abbrev;
$college_temp[] = $college->long_name;
$college_temp[] = $college->long_abbrev;
$college_temp[] = $college->url;
$college_temp[] = $college->description;
}
All the records from the database are going one after the other in the array. I need to optimize this using a multidimensional array.
$college_temp = array();
foreach($data['colleges'] as $college)
{
$college_temp[] = $college;
}
// Echo the first one's name
echo $college_temp[0]['name'];
// Echo the second one's url
echo $college_temp[1]['url'];
$college_temp = array();
$i = 0;
foreach($data['colleges'] as $college)
{
$college_temp[$i]['name'] = $college->name;
$college_temp[$i]['abbrev'] = $college->abbrev;
$college_temp[$i]['long_name'] = $college->long_name;
$college_temp[$i]['long_abbrev'] = $college->long_abbrev;
$college_temp[$i]['url'] = $college->url;
$college_temp[$i]['description'] = $college->description;
$i++;
}
// var_dump($college_temp); // uncomment to check array contents