how to pass $index to array value? - php

$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
print_r($suppliername);
print_r($vals);
foreach($Product as $index => $value)
{
if($vals[$index]>1)
{
echo $suppliername[$index]."+++"."Multiple Entries";
// Here i have to get S0001
}
else
{
echo $suppliername[$index]."+++"."Single Entry";
// Here i have to get S0002
}
}
how to pass $index to array value? I am trying to check value of the index is greater than or not in if condition. How to get this?

With your code now it couldn't work since your print_r($val) result was :
Array
(
[S0001] => 2
[S0002] => 1
)
The key of your $vals are the result of your $suppliername array, so try like this maybe :
$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
print_r($suppliername);
print_r($vals);
foreach($Product as $index => $value)
{
if($vals[$suppliername[$index]]>1)
{
echo $suppliername[$index]."+++"."Multiple Entries";
}
else
{
echo $suppliername[$index]."+++"."Single Entry";
}
}
The result :
S0001+++Multiple Entries
S0002+++Single Entry
S0001+++Multiple Entries
Is this what you are looking for?

I'm not sure i understood well what you wanted, but here it does what you want as result :
<?php
$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
echo "<pre>";
var_dump($suppliername);
echo "</pre>";
echo "<pre>";
var_dump($vals);
echo "</pre>";
foreach($Product as $index => $value)
{
if($index>1)
{
echo "<br />" . $suppliername[$index]."+++"."Multiple Entries";
// Here i have to get S0001
}
else
{
echo "<br />" . $suppliername[$index]."+++"."Single Entry";
// Here i have to get S0002
}
}

Related

how to get multidimensional array output

I have 3 arrays
$description[$cat_id][$kit_id][$item_id] = $description ;
$description2[$cat_id][$kit_id][$item_id] = $description2 ;
$qty_total[$cat_id][$kit_id][$item_id] = $qty;
foreach ($qty_total AS $key => $val) {
echo "Cat: ".$key."<br />";
foreach ($val AS $kkey => $kval) {
echo " Kit: ".$kkey."<br />";
foreach ($kval AS $ikey => $ival) {
echo " Item: ".$ikey." - ".$ival."<br />";
}
}
}
This brings only qty.
My question is how to get qty, description, description2 for given [cat_id][kit_id][item_id]?
You need to use keys of all foreach() loop to access other variables value
foreach ($qty_total AS $key => $val) {
foreach ($val AS $kkey => $kval) {
foreach ($kval AS $ikey => $ival) {
echo $ival;
echo PHP_EOL;
echo $description2[$key][$kkey][$ikey];
echo PHP_EOL;
echo $description[$key][$kkey][$ikey];
}
}
}
Sample output: https://3v4l.org/9uYfT

Not able to parse nested Json array in PHP while print_r function is printing all values properly

I am trying to parse a json array string. Though print_r prints all the values properly but while fetching the records I failed.
I must be doing something very terribly wrong. Could you please help to correct it.
So here is my code
<?
$str ='
{
"tenant_view_details": [
{
"status":"S",
"tenant_general":[
{"tenant_id":"6003","code":"ICI001","type":"BANK","category":"SM","gstn":"IWSDFS7372","pan":"KSAH9876AS","cin":"ASHED456","name":"ICICI Bank","address_line_1":"23, Hertz Plaza, Rajiv Chowk","address_line_2":" ","address_city":"New Delhi","address_distict ":"New Delhi","address_state ":"DL","address_state_code ":"07","address_pin ":"119923","contact_name ":"contact_name","contact_phone ":"1234567890","contact_std_code ":"11","contact_landline ":"1234567890","contact_email_id ":"contact#email.com"} ],
"tenant_bank": [
{ "bank_name ":"xyz","bank_account_no ":"12345","account_type ":"abc","ifsc_code ":"xx123","address_line_1 ":"qwer","address_line_2 ":"23","address_city ":"abc","address_district ":"xyz","address_state_name ":"asd","address_state_code ":"02","address_pin ":"123456" } ],
"tenant_agreement": [
{ "category ":"OM","tenancy_type ":"EXT","echarge_type ":"FIX","total_tenancy_value ":"32900.00","energy_charge_value ":"16543.00","rev_share_pct ":"0.05","start_dt":"10-1-2018","end_dt":"31-12-2020","attachment_path ":"na","change_value ":"na","latest_flag ":"Y","w_status ":"20","status ":"ACT"} ],
"tenant_sites": [
{"tenant_id":"6003","site_master_id":"1020","tn_site_code":"UPWMORA00069473","tn_site_name":"Deendayal Nagar","tn_cluster":"CLUSTER-03","circle_code":"UW","tower_type":"MAST","site_type":"ID","tenancy_type":"EXT","echarge_type":"FIX","primary_tenant":"N","latest_flag ":"Y"}, {"tenant_id":"6003","site_master_id":"1019","tn_site_code":"UPWGHAZ00069467","tn_site_name":"DJ College Newari Road, Modina","tn_cluster":"CLUSTER-03","circle_code":"UW","tower_type":"MAST","site_type":"ID","tenancy_type":"EXT","echarge_type":"FIX","primary_tenant":"N","latest_flag ":"Y"}, {"tenant_id":"6003","site_master_id":"1018","tn_site_code":"UPWMORA00069464","tn_site_name":"Bazar Makhbra","tn_cluster":"CLUSTER-03","circle_code":"UW","tower_type":"RTT","site_type":"OD","tenancy_type":"EXT","echarge_type":"FIX","primary_tenant":"N","latest_flag ":"Y"}, {"tenant_id":"6003","site_master_id":"1016","tn_site_code":"UPWGHAZ00069454","tn_site_name":"Hoshdarpur Garhi","tn_cluster":"CLUSTER-03","circle_code":"UW","tower_type":"RTT","site_type":"OD","tenancy_type":"EXT","echarge_type":"FIX","primary_tenant":"N","latest_flag ":"Y"}, {"tenant_id":"6003","site_master_id":"1011","tn_site_code":"UPWMUZN00069430","tn_site_name":"Sikanderpur","tn_cluster":"CLUSTER-03","circle_code":"UW","tower_type":"RTT","site_type":"ID","tenancy_type":"EXT","echarge_type":"FIX","primary_tenant":"N","latest_flag ":"Y"}, {"tenant_id":"6003","site_master_id":"1008","tn_site_code":"UPWALIG00069299","tn_site_name":"Mukund Vihar","tn_cluster":"CLUSTER-03","circle_code":"UW","tower_type":"RTT","site_type":"","tenancy_type":"EXT","echarge_type":"FIX","primary_tenant":"N","latest_flag ":"Y"} ]
}
]
}
';
$dataset = json_decode($str, true);
//print_r ($dataset);
foreach ($dataset['tenant_view_details'] as $newdata) {
$general = $newdata['tenant_general'];
$bank = $newdata['tenant_bank'];
$agreement = $newdata['tenant_agreement'];
$sites = $newdata['tenant_sites'];
}
//prinitng data
echo "<br/>";
print_r($general);
echo "<br/>";
print_r($bank);
echo "<br/>";
print_r($agreement);
echo "<br/>";
foreach($general as $general_data){
(isset($general_data['tenant_id']) && !empty($general_data['tenant_id']))? $tenant_id=$general_data['tenant_id'] : $tenant_id="not set";
echo "<br/>tenant_id::" . $tenant_id;
}
foreach($bank as $bank_data){
(isset($bank_data['bank_name']) && !empty($bank_data['bank_name']))? $bank_name=$bank_data['bank_name'] : $bank_name="not set";
echo "<br/>bank_name:" . $bank_data['bank_name'];
}
foreach($agreement as $agreement_data){
(isset($agreement_data['category']) && !empty($agreement_data['category']))? $category=$agreement_data['category'] : $category="not set";
(isset($agreement_data['tenancy_type']) && !empty($agreement_data['tenancy_type']))? $tenancy_type=$agreement_data['tenancy_type'] : $tenancy_type="not set";
(isset($agreement_data['echarge_type']) && !empty($agreement_data['echarge_type']))? $echarge_type=$agreement_data['echarge_type'] : $echarge_type="not set";
echo "<br/>category:" . $category;
echo "<br/>tenancy_type:" . $tenancy_type;
echo "<br/>echarge_type:" . $echarge_type;
}
?>
Your problem is that in your $bank and $category arrays, all the keys have trailing spaces on them. If you change the references to include those spaces, your code works fine. See https://3v4l.org/GX5iC
Alternatively you can trim the array keys, using something like this code inside your foreach loops:
$bank_keys = array_map('trim', array_keys($bank_data));
$bank_data = array_combine($bank_keys, array_values($bank_data));
You could also create a recursive function to trim the entire $dataset value. For example:
function trim_keys($array) {
foreach ($array as $key => $value) {
echo "trimming key '$key' to '" . trim($key) . "'\n";
unset($array[$key]);
$array[trim($key)] = $value;
if (is_array($value))
$array[trim($key)] = trim_keys($value);
else
$array[trim($key)] = $value;
}
return $array;
}
Then, by using
$dataset = trim_keys($dataset);
Your code will work fine. Demo on 3v4l.org

Print multidimentional array from query result

I have an array:-
$resolution = array();
foreach ($report as $key => $val) {
$queryToGetRes = "SELECT resolution,ticket_no FROM techzilla.bugs WHERE bug_id = '$val' ";
$sqlResult = mysql_query($queryToGetRes) or die (mysql_error());
while ($resolutionAns = mysql_fetch_array($sqlResult)) {
$resolution[$resolutionAns['resolution']][] = $resolutionAns['ticket_no'];
}
}
I need to print the array which stores the resolution and ticket no.
try something like this:
echo "<pre>" . print_r($resolution, true) . "</pre>";
For debuging purpuses you should use
var_dump($resolution);
If you want to use it
foreach ($resolution as $r => $ids){
echo "Resolution : $r\n";
foreach ($ids as $id) {
echo "$id\n";
}
}

array result using foreach statement display in json in php

I am getting the result from web service SOAP client. I have created an array to get the result and display it using json format. I am getting few of my results properly. I have SerialEquipment parameter which is array and i need to get the result using foreach loop. I am doing an mistake there. I dont know how can i assign my $vehiclResult array in this for each statement. So that all the results at last i will collect and display using json using vehicleResult array.My mistake is in the foreach loop.
structure for SerialEquipment parameters:
Code:
$vehicle = getVehicleValuation();
$Serial=$vehicle['SerialEquipment'];
$vehiclResult = array(
'WE_Number' => $vehicle['WE Number'] ."<br>",
'Vehicle Type'=> $vehicle['Vehicle Type'] . "<br>",
'HSN' => $vehicle['HSN'] . "<br>",
'TSN' => $vehicle['TSN'] . "<br>"
);
foreach($Serial as $key => $obj) {
if(!isset($vehiclResult[$key]))
$vehiclResult[$key] = array();
$vehiclResult[$key]['SerialEquipment'] = $key. "<br>";
$vehiclResult[$key]['Code'] = $obj->Code. "<br>";
$vehiclResult[$key]['Desc Short'] = $obj->Desc_Short. "<br>";
$vehiclResult[$key]['Desc Long'] = $obj->Desc_Long. "<br>";
foreach($obj->Esaco as $key2 => $obj2) {
if($obj2->EsacoMainGroupCode === null){
// doesn't contain Esaco
continue;
}
else{
if(!isset($vehiclResult[$key][$key2]))
$vehiclResult[$key][$key2] = array();
$vehiclResult[$key][$key2]['esaco'] = $key2. "<br>";
$vehiclResult[$key][$key2]['EsacoMainGroupCode'] = $obj2->EsacoMainGroupCode. "<br>";
$vehiclResult[$key][$key2]['EsacoMainGroupDesc'] = $obj2->EsacoMainGroupDesc. "<br>";
$vehiclResult[$key][$key2]['EsacoSubGroupCode'] = $obj2->EsacoSubGroupCode. "<br>";
$vehiclResult[$key][$key2]['EsacoSubGroupDesc'] = utf8_decode($obj2->EsacoSubGroupDesc). "<br>";
$vehiclResult[$key][$key2]['EsacoGroupCode'] = $obj2->EsacoGroupCode. "<br>";
$vehiclResult[$key][$key2]['EsacoGroupDesc'] = utf8_decode($obj2->EsacoGroupDesc). "<br>";
}
}
}
$result = array(
'vehicle' => $vehiclResult
);
echo json_encode($result);
die();
}
You need to check if your array have the key so:
if(!isset($vehiclResult[$key]))
if not, you need to create it:
$vehiclResult[$key] = array(); // as an array
Also, you don't really need to make a description of your "item". You can Parse your JSON on the result page to output some text.
You can do something like.
Do something like:
foreach($Serial as $key => $obj) {
if(!isset($vehiclResult[$key]))
$vehiclResult[$key] = array();
$vehiclResult[$key]['serial'] = $key;
$vehiclResult[$key]['code'] = $obj->Code;
$vehiclResult[$key]['short_desc'] = $obj->Desc_Short;
$vehiclResult[$key]['long_desc'] = $obj->Desc_Long;
foreach($obj->Esaco as $key2 => $obj2) {
if($obj2->EsacoMainGroupCode === null){
// doesn't contain Esaco
continue;
}
else{
if(!isset($vehiclResult[$key][$key2]))
$vehiclResult[$key][$key2] = array();
$vehiclResult[$key][$key2]['esaco'] = $key2;
$vehiclResult[$key][$key2]['EsacoMainGroupCode'] = $obj2->EsacoMainGroupCode;
$vehiclResult[$key][$key2]['EsacoMainGroupDesc'] = $obj2->EsacoMainGroupDesc;
$vehiclResult[$key][$key2]['EsacoSubGroupCode'] = $obj2->EsacoSubGroupCode;
$vehiclResult[$key][$key2]['EsacoSubGroupDesc'] = utf8_decode($obj2->EsacoSubGroupDesc);
$vehiclResult[$key][$key2]['EsacoGroupCode'] = $obj2->EsacoGroupCode;
$vehiclResult[$key][$key2]['EsacoGroupDesc'] = utf8_decode($obj2->EsacoGroupDesc);
}
}
}
$result = array(
'vehicle' => $vehiclResult
);
echo json_encode($result);
die();
If you would keep your "text" and your <br> code, do the samething but add what you want to output after the "="
EDIT
** A HAVE CHANGE THE CODE PREVIOUSLY..
if you want to test your $vehiclResult, try something like:
foreach($vehiclResult as $key=>$value){
if(!is_array($value))
var_dump($value);
else {
foreach($value as $key2=>$value2){
if(!is_array($value2))
var_dump($value2);
else {
foreach($value2 as $key3=>$value3){
var_dump($value3);
}
}
}
}

foreach array displaying result

I have been looking at this code etc for so long that I am now confusing myself - not good
I have foreach of
foreach($sort_order as $sort)
{
echo '<pre>';
var_dump($sort['sorder']);
echo '</pre>';
}
That gives me a result of:
string(2) "20"
string(2) "10"
How can I return this so I can do value="<?php echo $someValue; ?>"
Assuming that 'sorder' is a key in your array I would try the following:
foreach($sort_order as $key => $sort) {
echo '<pre>';
if($key == "sorder") {
echo $sort[$key];
}
echo '</pre>';
}
It looks like you're only displaying they key. I'm assuming your array is of type associative.
To traverse associative arrays in PHP, follow this:
foreach($array as $key => $value)
{
echo "[" . $key . "]" . " = " . $value . "<br />;
}
I'm not sure what are you trying to do but...
<?php
$val = array();
foreach($sort_order as $sort) {
$val[] = $sort['sorder'];
}
?>
<p>value = <?php echo $val[0]; ?></p>
<p>value = <?php echo $val[1]; ?></p>

Categories